Skip to content

Error when catch exceptions in Vue components #1012

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

Closed
Shulkin opened this issue Aug 22, 2017 · 8 comments
Closed

Error when catch exceptions in Vue components #1012

Shulkin opened this issue Aug 22, 2017 · 8 comments

Comments

@Shulkin
Copy link

Shulkin commented Aug 22, 2017

Do you want to request a feature or report a bug?

bug

What is the current behavior?

I'm using Chrome 60. I'm adding Sentry support to Vue SPA, using Raven 3.17.0.
I install Raven from npm and init it in my index.js, right before creating Vue application, i.e.

import Vue from 'vue'
import Raven from 'raven-js'
import RavenVue from 'raven-js/plugins/vue'

Raven.config('... our company dsn ...').addPlugin(RavenVue, Vue).install()

new Vue({
	el: '#app',
... etc.

Then I throw some exceptions in code, like

throw new Error('test exception')

Raven catches and log exceptions only in my main Vue instance methods.
Any other errors in components, modules, etc. cause this error in console

TypeError: Cannot read property '$root' of undefined
    at formatComponentName (eval at <anonymous> (client.js:3430), <anonymous>:8:11)
    at VueErrorHandler (eval at <anonymous> (client.js:3430), <anonymous>:28:28)
    at handleError (eval at <anonymous> (client.js:766), <anonymous>:513:25)
    at Array.eval (eval at <anonymous> (client.js:766), <anonymous>:654:11)
    at nextTickHandler (eval at <anonymous> (client.js:766), <anonymous>:599:16)
    at <anonymous>

For example:

new Vue({
	el: '#app',
	store,
	data() { return {...} },
	router,
	render: h => h(app),
	methods: {
		testMethod() {
			// debug exception for Sentry
                        throw new Error('test exception')
               }
}})

This works. Test error is logged in Sentry.

Same throw in any other components methods cause error.

What is the expected behavior?

Log any uncaught exceptions in code, don't cause errors.

Am I doing something wrong?

@kamilogorek
Copy link
Contributor

kamilogorek commented Sep 26, 2017

I created this simple example and it works just fine.

<!doctype html>
<html>
  <body>
    <div id="app">
      <clock-x></clock-x>
    </div>
    <script src="bundle.js"></script>
  </body>
</html>
import Vue from 'vue'
import Raven from 'raven-js'
import RavenVue from 'raven-js/plugins/vue'

Raven.config('https://[email protected]/1', {
  dataCallback: function (data) {
    console.log(data);
    return data;
  }
})
  .addPlugin(RavenVue, Vue)
  .install();

Vue.component('clock-x', {
  data() {
    return {
      currentTime: +new Date()
    }
  },
  template: `<div>
    {{ currentTime }}
    <button v-on:click="updateTime">Update time</button>
  </div>`,
  methods: {
    updateTime() {
      this.currentTime = +new Date()
      throw new Error('Time is irrelevant')
    }
  }
})

new Vue({
  el: '#app'
});

Can you please provide reproducible code example?

@kamilogorek
Copy link
Contributor

Please feel free to reopen if this is still the case.

@Dewep
Copy link

Dewep commented Oct 30, 2017

Hi,

I have the same problem. I have this problem only on IE (11 in my case), but I can't reproduce the bug on a jsfiddle.

Reproduction does not seem important, because it seems to happen during problems with Vue.js (therefore not a real RavenJS issue). The problem being that Vue.js still reports the problem (using the errorHandler method), but being an internal problem, the vm parameter is undefined (which then causes a problem with your library).

I edited your library to add some logs:

function vuePlugin(Raven, Vue) {
  // ...
  Vue.config.errorHandler = function VueErrorHandler(error, vm, info) {
    console.error('call::VueErrorHandler, error.stack:', error.stack)
    console.error('call::VueErrorHandler, vm:', vm)
    console.error('call::VueErrorHandler, info:', info)

    var metaData = {
      componentName: formatComponentName(vm),
      propsData: vm.$options.propsData
    };

    // ...
  };
}

And here is the result:

call::VueErrorHandler, error.stack: Error: Access is denied.
   at setAttr (eval code:6153:7)
   at updateAttrs (eval code:6107:7)
   at invokeCreateHooks (eval code:5509:7)
   at createElm (eval code:5400:11)
   at updateChildren (eval code:5652:11)
   at patchVnode (eval code:5732:29)
   at updateChildren (eval code:5629:9)
   at patchVnode (eval code:5732:29)
   at patch (eval code:5881:9)
   at Vue.prototype._update (eval code:2569:7)
   at updateComponent (eval code:2687:7)
   at get (eval code:3030:5)
   at run (eval code:3107:5)
   at flushSchedulerQueue (eval code:2873:5)
   at Anonymous function (eval code:1780:9)
   at flushCallbacks (eval code:1701:5)
   at run (eval code:66:13)
   at Anonymous function (eval code:79:30)
   at flush (eval code:18:9)

call::VueErrorHandler, vm: undefined

call::VueErrorHandler, info: nextTick

[Vue warn]: Error in config.errorHandler: "TypeError: Unable to get property '$root' of undefined or null reference"
TypeError: Unable to get property '$root' of undefined or null reference
   {
      [functions]: ,
      __proto__: { },
      description: "Unable to get property '$root' of undefined or null reference",
      message: "Unable to get property '$root' of undefined or null reference",
      name: "TypeError",
      number: -2146823281,
      stack: "TypeError: Unable to get property '$root' of undefined or null reference
   at formatComponentName (eval code:7:3)
   at VueErrorHandler (eval code:28:5)
   at globalHandleError (eval code:1670:7)
   at handleError (eval code:1664:3)
   at Anonymous function (eval code:1782:9)
   at flushCallbacks (eval code:1701:5)
   at run (eval code:66:13)
   at Anonymous function (eval code:79:30)
   at flush (eval code:18:9)",
      Symbol()_m.epeeaf52kuu: undefined
   }

[Vue warn]: Error in nextTick: "Error: Access is denied.
"
Error: Access is denied.

   {
      [functions]: ,
      __proto__: { },
      description: "Access is denied.
",
      message: "Access is denied.
",
      name: "Error",
      number: -2147024891,
      stack: "Error: Access is denied.

   at setAttr (eval code:6153:7)
   at updateAttrs (eval code:6107:7)
   at invokeCreateHooks (eval code:5509:7)
   at createElm (eval code:5400:11)
   at updateChildren (eval code:5652:11)
   at patchVnode (eval code:5732:29)
   at updateChildren (eval code:5629:9)
   at patchVnode (eval code:5732:29)
   at patch (eval code:5881:9)
   at Vue.prototype._update (eval code:2569:7)
   at updateComponent (eval code:2687:7)
   at get (eval code:3030:5)
   at run (eval code:3107:5)
   at flushSchedulerQueue (eval code:2873:5)
   at Anonymous function (eval code:1780:9)
   at flushCallbacks (eval code:1701:5)
   at run (eval code:66:13)
   at Anonymous function (eval code:79:30)
   at flush (eval code:18:9)",
      Symbol()_m.epeeaf52kuu: undefined
   }

image

I apologize for this formatting, but IE doesn't help me very much, and I can't reproduce this problem anywhere but on my project.

In any case, even if the error may be caused by Vue.js, RavenJS should not crash and handle the case of wrong parameters.

@kamilogorek
Copy link
Contributor

@Dewep what version of Vue are you using? We had a similar issue before, and we got a note that it should be fixed in Vue 2.5.0 – #1061 (comment)

@Dewep
Copy link

Dewep commented Oct 31, 2017

Hi,
Unfortunately, I'm using Vue 2.5.2:

{
  "dependencies": {
    // ...
    "raven-js": "3.19.1",
    "vue": "2.5.2",
    "vue-router": "3.0.1",
    "vuex": "3.0.0",
    // ...
  }
}

@kamilogorek
Copy link
Contributor

@Dewep you can verify if your issue has been fixed updating your package.json with:

"raven-js": "getsentry/raven-js#4c41fefbccf7c45f1fb52152773e80b0ef1de95f"

@Dewep
Copy link

Dewep commented Oct 31, 2017

Well, I'm not sure that's the perfect solution. As you would expect, RavenJS doesn't crash anymore, but the error is not reported. We would like to be informed when the application is dead for our customers. :)

@kamilogorek
Copy link
Contributor

@Dewep you're totally right. This should do now #1118 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants