Subscribe on changes!

Better error handing for accessing properties on a undefined object and undefined directives

avatar
Apr 21st 2021

What problem does this feature solve?

Currently accessing properties on an undefined object or using a directive that is is not registered/undefined provides provides an error that breaks the page and provides little feedback on the issue. The exception that is raised reads "Uncaught TypeError: Cannot read property 'foo' of undefined". For a trivial application this is easy to find, however for larger ones it can be difficult to narrow down the issue. This issue is particularly bad with directives where the exception is simply "Cannot read property 'created' of undefined". This can make simple spelling errors harder to debug than they should, like v-claok instead of v-cloak where the 'a' and 'o' are transposed.

The following are examples of the issue:

let app = Vue.createApp({
  template: '<div v-claok>{{ foo }}</div>',
})
app.mount('#app');
"Cannot read property 'created' of undefined"
let app = Vue.createApp({
  template: '<div>{{ foo.bar }}</div>',
  data() {
      return {
          foo: undefined
      }
  }
})
app.mount('#app');
"Cannot read property 'bar' of undefined"

If possible while in development mode it would be more helpful to display a more descriptive error that includes the dom element and the and the failing bit of code. So for instances

Cannot read property bar of undefined on element <div>{{ foo.bar }}</div>

This would allow easier debugging similar to the template compilation errors that show up.

What does the proposed API look like?

Errors for accessing undefined variables would change from

Cannot read property 'bar' of undefined

to

Cannot read property 'bar' of undefined on element '<div>{{ foo.bar }}</div>'

or something a bit more descriptive.

It would also be nice if directives could have their own warning like

Undefined directive 'v-cloak' on element '<div v-claok>{{ foo }}</div>
avatar
Apr 22nd 2021

Vue already prints meaningful warning messages before the error occurs, e.g.

image

As shown in the pic above, you can see the problematic component(<MyComponent>) and the cause of the error(failed to resolve directive: claok).

avatar
Apr 22nd 2021

Woops, I can't believe I missed that message for the directives. Thanks for pointing it out.

What about accessing properties on an undefined object?

avatar
Apr 23rd 2021

Also to add a bit more perspective on the issue, I am rendering the majority of the html on the server and just mounting an app on top with minimal components. This allow things to be mostly intractable until the javascript loads and lowers the content layout shift. The problem is that that the only feed back in this case is Unhandled error during execution of render function at <App> where app could be basically anything within the page. If the element was provided, it would narrow down the issue. This could also help the narrow it down within the components like in your screen shot.

Something similar is already being done for v-model being used on something other than input, textarea and select. v-mode-error

If it's not possible thats fine, I just wanted to bring it up