Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add warnHandler to allow users to set a custom warn callback #5883

Merged
merged 1 commit into from
Jun 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/core/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type Config = {
performance: boolean;
devtools: boolean;
errorHandler: ?(err: Error, vm: Component, info: string) => void;
warnHandler: ?(msg: string, vm: Component, trace: string) => void;
ignoredElements: Array<string>;
keyCodes: { [key: string]: number | Array<number> };

Expand Down Expand Up @@ -62,6 +63,11 @@ export default ({
*/
errorHandler: null,

/**
* Warn handler for watcher warns
*/
warnHandler: null,

/**
* Ignore certain custom elements
*/
Expand Down
10 changes: 6 additions & 4 deletions src/core/util/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ if (process.env.NODE_ENV !== 'production') {
.replace(/[-_]/g, '')

warn = (msg, vm) => {
if (hasConsole && (!config.silent)) {
console.error(`[Vue warn]: ${msg}` + (
vm ? generateComponentTrace(vm) : ''
))
const trace = vm ? generateComponentTrace(vm) : ''

if (config.warnHandler) {
config.warnHandler.call(null, msg, vm, trace)
} else if (hasConsole && (!config.silent)) {
console.error(`[Vue warn]: ${msg}${trace}`)
}
}

Expand Down
36 changes: 35 additions & 1 deletion test/unit/features/debug.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Vue from 'vue'
import { formatComponentName } from 'core/util/debug'
import { formatComponentName, warn } from 'core/util/debug'

describe('Debug utilities', () => {
it('properly format component names', () => {
Expand Down Expand Up @@ -80,4 +80,38 @@ found in
<Root>`
).toHaveBeenWarned()
})

describe('warn', () => {
const msg = 'message'
const vm = new Vue()

it('calls warnHandler if warnHandler is set', () => {
Vue.config.warnHandler = jasmine.createSpy()

warn(msg, vm)

expect(Vue.config.warnHandler).toHaveBeenCalledWith(msg, vm, jasmine.any(String))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you can see here, I don't explicitly match the trace argument as I cannot spy the generateComponentTrace method without exporting it and wanted to avoid that without approval from a maintainer.


Vue.config.warnHandler = null
})

it('calls console.error if silent is false', () => {
Vue.config.silent = false

warn(msg, vm)

expect(msg).toHaveBeenWarned()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

toHaveBeenWarned currently still regards something being "warned" when it calls console.error (warnHandler is not set). Did we want to change this so it also tracks if a custom warnHandler has been called?

expect(console.error).toHaveBeenCalled()
})

it('does not call console.error if silent is true', () => {
Vue.config.silent = true

warn(msg, vm)

expect(console.error).not.toHaveBeenCalled()

Vue.config.silent = false
})
})
})
6 changes: 6 additions & 0 deletions types/test/vue-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ class Test extends Vue {
vm.testMethods();
}
};
config.warnHandler = (msg, vm) => {
if (vm instanceof Test) {
vm.testProperties();
vm.testMethods();
}
};
config.keyCodes = { esc: 27 };
}

Expand Down
1 change: 1 addition & 0 deletions types/vue.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export declare class Vue {
productionTip: boolean;
performance: boolean;
errorHandler(err: Error, vm: Vue, info: string): void;
warnHandler(msg: string, vm: Vue, trace: string): void;
ignoredElements: string[];
keyCodes: { [key: string]: number };
}
Expand Down