Skip to content

Commit 9831b40

Browse files
lbennett-stackiyyx990803
authored andcommitted
Add warnHandler to allow users to set a custom warn callback, similar to errorHandler (#5883)
1 parent 69f946b commit 9831b40

File tree

5 files changed

+54
-5
lines changed

5 files changed

+54
-5
lines changed

Diff for: src/core/config.js

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export type Config = {
1616
performance: boolean;
1717
devtools: boolean;
1818
errorHandler: ?(err: Error, vm: Component, info: string) => void;
19+
warnHandler: ?(msg: string, vm: Component, trace: string) => void;
1920
ignoredElements: Array<string>;
2021
keyCodes: { [key: string]: number | Array<number> };
2122

@@ -62,6 +63,11 @@ export default ({
6263
*/
6364
errorHandler: null,
6465

66+
/**
67+
* Warn handler for watcher warns
68+
*/
69+
warnHandler: null,
70+
6571
/**
6672
* Ignore certain custom elements
6773
*/

Diff for: src/core/util/debug.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ if (process.env.NODE_ENV !== 'production') {
1515
.replace(/[-_]/g, '')
1616

1717
warn = (msg, vm) => {
18-
if (hasConsole && (!config.silent)) {
19-
console.error(`[Vue warn]: ${msg}` + (
20-
vm ? generateComponentTrace(vm) : ''
21-
))
18+
const trace = vm ? generateComponentTrace(vm) : ''
19+
20+
if (config.warnHandler) {
21+
config.warnHandler.call(null, msg, vm, trace)
22+
} else if (hasConsole && (!config.silent)) {
23+
console.error(`[Vue warn]: ${msg}${trace}`)
2224
}
2325
}
2426

Diff for: test/unit/features/debug.spec.js

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Vue from 'vue'
2-
import { formatComponentName } from 'core/util/debug'
2+
import { formatComponentName, warn } from 'core/util/debug'
33

44
describe('Debug utilities', () => {
55
it('properly format component names', () => {
@@ -80,4 +80,38 @@ found in
8080
<Root>`
8181
).toHaveBeenWarned()
8282
})
83+
84+
describe('warn', () => {
85+
const msg = 'message'
86+
const vm = new Vue()
87+
88+
it('calls warnHandler if warnHandler is set', () => {
89+
Vue.config.warnHandler = jasmine.createSpy()
90+
91+
warn(msg, vm)
92+
93+
expect(Vue.config.warnHandler).toHaveBeenCalledWith(msg, vm, jasmine.any(String))
94+
95+
Vue.config.warnHandler = null
96+
})
97+
98+
it('calls console.error if silent is false', () => {
99+
Vue.config.silent = false
100+
101+
warn(msg, vm)
102+
103+
expect(msg).toHaveBeenWarned()
104+
expect(console.error).toHaveBeenCalled()
105+
})
106+
107+
it('does not call console.error if silent is true', () => {
108+
Vue.config.silent = true
109+
110+
warn(msg, vm)
111+
112+
expect(console.error).not.toHaveBeenCalled()
113+
114+
Vue.config.silent = false
115+
})
116+
})
83117
})

Diff for: types/test/vue-test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ class Test extends Vue {
6363
vm.testMethods();
6464
}
6565
};
66+
config.warnHandler = (msg, vm) => {
67+
if (vm instanceof Test) {
68+
vm.testProperties();
69+
vm.testMethods();
70+
}
71+
};
6672
config.keyCodes = { esc: 27 };
6773
}
6874

Diff for: types/vue.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export declare class Vue {
7575
productionTip: boolean;
7676
performance: boolean;
7777
errorHandler(err: Error, vm: Vue, info: string): void;
78+
warnHandler(msg: string, vm: Vue, trace: string): void;
7879
ignoredElements: string[];
7980
keyCodes: { [key: string]: number };
8081
}

0 commit comments

Comments
 (0)