2.3.3
https://jsfiddle.net/50wL7mdz/40803/
Create a global error handler, something like this:
window.onerror = function (e) {
alert("Error caught");
};
inside a function of your vue methods, throw an exception. for example
throw "hello!";
alert box to be shown.
nothing. it seems Vue silence the error.
I want to use this to have a centralized error handling mechanism for user generated errors.
Use Vue.config.errorHandler
.
Vue.Config.errorHandler don't work. I have already tested that.
https://jsfiddle.net/50wL7mdz/40805/
As stated in the docs, this handle only catches errors during render:
If a runtime error occurs during a component’s render, it will be passed to the global Vue.config.errorHandler config function if it has been set.
https://jsfiddle.net/50wL7mdz/40809/
FYI: if an error is captured by Vue, it will always be sent to config.errorHandler
.
thanks yyx990803.
I made it work in jsFiddle (order seems to be important), but I am not able to make it work on my project! I am using components heavily (SPA) and it doesn't work when the error comes from components!! I will update this post when I could pinpoint the issue.
The same problem, not working on components.
The Vue.config.errorHandler is not catching errors inside of methods run after render.
https://jsfiddle.net/50wL7mdz/55247/
If you have a button that throws an error when you call a method Vue.config.errorHandler won't catch it and neither will windows.onerror
@codingfriend1 the same problem. have you figure it out?
No I haven't.
Use the errorHandler
and window.onerror
should catch all errors.
You have to set both window.onerror
and Vue.config.errorHandler
? throw 'strange'
indeed!
window.onerror
not work,vue version 2.5.13
@zhaoqize I same question. Can U find where is the problem?
@hstarorg , your example is not working...
@wcjr The example is working, check the console. Updated fiddle.
Vue.config.errorHandler
is all you need, if you execute js code outside of Vue (for any reason) you can use the native window.onerror
.
yep, I see both alerts getting thrown now, I presume it had something to do with changes in 2.6 maybe? Thanks for the heads-up @A1rPun
Most helpful comment
The Vue.config.errorHandler is not catching errors inside of methods run after render.
https://jsfiddle.net/50wL7mdz/55247/
If you have a button that throws an error when you call a method Vue.config.errorHandler won't catch it and neither will windows.onerror