Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture all the errors thrown in Vue.JS SPA

I am creating a webapp and trying to capture all the errors thrown anywhere in the vue.js webapp.

I was looking at errorHandler but it only captures error during render or watchers, as stated:

Assign a handler for uncaught errors during component render and watchers. The handler gets called with the error and the Vue instance.

Getting cue from this question, I wrote following code:

window.onerror = function (errorMsg, url, lineNumber, column, errorObj) {
 console.log('Inside window.onerror')
 console.log(errorMsg, ' ', url, ' ', lineNumber, ' ', column, ' ', errorObj)
 return false
}

window.addEventListener('error', function (e) {
 console.log('Inside error Listener', e.message)
})

Above both gets called but I don't any details of the errors with these. in all the cases I get errorMessage as script error

What can be better way to get details of all the errors and send it to some centralised place like sentry.

like image 644
Saurabh Avatar asked Feb 17 '17 09:02

Saurabh


People also ask

How do I get Vue errors?

For error handling, we can assign a handler function to Vue. config. errorHandler . The handler gets called for any uncaught exceptions within any Vue instance (Vue components) throughout the application.

How do I debug a Vue error?

Double-click on the line number in your VSCode to add a breakpoint. Then, run npm run serve to serve your project. Go to the debug view in your editor and select your preferred browser configuration. Press 5 or click the green play button.

How do I get JavaScript errors?

Right-click anywhere in the webpage and then select Inspect. Or, press F12 . DevTools opens next to the webpage. In the top right of DevTools, the Open Console to view errors button displays an error about the webpage.

What is $t in Vue?

Vuex is a state management pattern for vue. js. $t is the injected method from vue. js or Vue.


1 Answers

You mentioned Sentry in your answer, and if your goal is getting errors logged there, all you need to add is Raven.js, and the Vue plugin.

Sentry has documentation on doing this here: https://docs.sentry.io/clients/javascript/integrations/vue/

like image 110
cdignam Avatar answered Sep 30 '22 17:09

cdignam