Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue.js: Nuxt error handling

Struggling a bit to set up error handling with vuex. There seems to be quite a few ways to do so and little documentation on proper error handling. I've been experimenting with four alternatives, though I haven't found a satisfying solution yet.


Alternative 1 - Catching and processing errors on component

in pages/login.vue:

export default {     methods: {         onLogin() {             this.$store.dispatch('auth/login', {                 email: this.email,                 password: this.password,             }).then(() => {                 this.$router.push('/home');             }).catch((error) {                 // handle error in component             });         },     }, } 

in store/auth.js:

export const actions = {     login({ commit }, { email, password }) {         return this.$axios.post('/api/login', {             email,             password,         }).then((res) => {             doSomething(res);         });     }, } 

PROS

  • Hmm.

CONS

  • Errors not handled and stored in vuex.
  • Introduces lots of boilerplate code in component methods.

Alternative 2 - Catching and processing errors in vuex

in pages/login.vue:

export default {     methods: {         onLogin() {             this.$store.dispatch('auth/login', {                 email: this.email,                 password: this.password,             }).then(() => {                 this.$router.push('/home');             });         },     }, } 

in store/auth.js:

export const actions = {     login({ commit }, { email, password }) {         return this.$axios.post('/api/login', {             email,             password,         }).then((res) => {             doSomething(res);         }).catch((error) => {             // store error in application state             commit('SET_ERROR', error);         });     }, } 

PROS

  • Error object is accessible with vuex from any component
  • Could use a reactive error component in layout, which is revealed when the error state changes.

CONS

  • I'm not sure if there is a way to track the source of the error, from which component it was thrown.

Alternative 3 - Catching errors using axios interceptors

in plugins/axios.js:

export default function({ $axios, store }) {     $axios.onError(error => {         store.dispatch('setError', error);     }); } 

in pages/login.vue:

export default {     methods: {         onLogin() {             this.$store.dispatch('auth/login', {                 email: this.email,                 password: this.password,             }).then(() => {                 this.$router.push('/home');             });         },     }, } 

in store/auth.js:

export const actions = {     login({ commit }, { email, password }) {         return this.$axios.post('/api/login', {             email,             password,         }).then((res) => {             doSomething(res);         });     }, } 

PROS

  • Global error handling
  • No need to catch errors in either vuex or component
  • No boiler-plate code

CONS

  • All exceptions are unhandled, meaning non-axios errors are uncaught.

Alternative 4 - Custom error plugin

I've been experimenting in implementing a custom plugin that catches all exceptions, but I'm not succeeding in making it work.

in plugins/catch.js:

export default (ctx, inject) => {     const catchPlugin = function (func) {         return async function (args) {             try {                 await func(args)             } catch (e) {                 return console.error(e)             }         }     };     ctx.$catch = catchPlugin;     inject('catch', catchPlugin); } 

in pages/login.vue:

export default {     methods: {         onLogin: this.$catch(async function () {             await this.$store.dispatch('auth/login', { email: this.email, password: this.password });             this.$router.push('/home');         }),     }, } 

PROS

  • No boilerplate.
  • All errors caught in plugin.

CONS

  • I cannot make it work. :(

My impression is that there is a lack of documentation on error handling in vue/nuxt. Could anyone get the fourth alternative to work? Would this be ideal? Any other alternatives? What is conventional?

Thank you for your time!

like image 854
nomadoda Avatar asked Jul 09 '18 13:07

nomadoda


2 Answers

The reason why option #4 is not working is because you're returning a function that never gets executed:

function catchPlugin(outerFunction) {    return function async innerFunction(args) {      try {        const data = await outerFunction(args);        return { data }       } catch (error) {        return { error }      }    } } 

Usage:

const execute = catchPlugin((args) => {   // do something })  execute('myArgument'); 

As you can see you need to execute the inner function as well, to make your example work:

onLogin: this.$catch(async function () {     await this.$store.dispatch('auth/login', { email: this.email, password: this.password });     this.$router.push('/home'); })(), // mind the () :-) 

But... I believe handling errors in components is not a bad thing, since this is tightly coupled to your view component. For instance, think about a login component, what we see these days is a global error handler (toastr) which will display a toast message if the username/password is incorrect. From my experience this is not the best behavior, it's a good starting point but better would be to add error messages close to the component displaying what exactly went wrong. Meaning you will always have to add error handling (UI related) in the component itself.

We're also struggling with this in our company with colleagues working on the same product. One is adding error handling, the other one is not.. The only solution, in my opinion, is to educate developers to always add proper error handling. The syntax with async/await is not that bad:

methods: {    async login (email, password) {       try {          await this.$store.dispatch('auth/login', { email, password })          // do something after login       } catch (error) {          // handle error       }    } } 

One last thing about your con: Errors not handled and stored in vuex.. Why is this a con? Do you need to have the error globally available? What I see a lot is people putting so much useless state in vuex that's only used in the component itself. It's not bad to have local component state. Since it's about login, this error should only be known in the login component.

like image 111
Dieterg Avatar answered Sep 19 '22 10:09

Dieterg


Use Promise in action

Example in vuex:

NEW_AUTH({ commit }) {     return new Promise((resolve, reject) => {       this.$axios.$get('/token').then((res) => {         ...         resolve();       }).catch((error) => {         reject(error);       })     })   } 

In page:

this.$store.dispatch('NEW_AUTH')    .then(() => ... )    .catch((error) => ... ) 
like image 24
Adriano Resende Avatar answered Sep 20 '22 10:09

Adriano Resende