Axios-module: Error handling in 5.0.0

Created on 7 Feb 2018  路  1Comment  路  Source: nuxt-community/axios-module

hi!

Migrations says: "Default error interceptor removed".
Is there any example with best practice how to handle exception now?

I saw documentation:

export default function ({ $axios, redirect }) {
  $axios.onRequest(config => {
    console.log('Making request to ' + config.url)
  })

  $axios.onError(error => {
    const code = parseInt(error.response && error.response.status)
    if (code === 400) {
      redirect('/400')
    }
  })
}

But I am not sure how to pass error to the nuxt page to print the stack trace etc.

Thanks!

This question is available on Nuxt.js community (#c86)
help wanted question

Most helpful comment

  • For development and printing stack track consol.error should be enough there.
  • To notify users about error, a toast for store dispatch may be used:
export default function ({ $axios, redirect, app, store }) {
  $axios.onError(error => {
    // with @nuxtjs/toast
    app.$toast.error('Error while making request: ' + error.response.message)

    // with a custom store action
    store.dispatch('error', error)
 })
}
````

- To handle error while making requests (For example inside a page) without a global handler, you can even forget about `onError` interceptor. I prefer using `.catch` instead of `try/catch`:

```js
// ...inside a method
this.$axios.$get('/api/foo/bar').catch(e => { ... })

>All comments

  • For development and printing stack track consol.error should be enough there.
  • To notify users about error, a toast for store dispatch may be used:
export default function ({ $axios, redirect, app, store }) {
  $axios.onError(error => {
    // with @nuxtjs/toast
    app.$toast.error('Error while making request: ' + error.response.message)

    // with a custom store action
    store.dispatch('error', error)
 })
}
````

- To handle error while making requests (For example inside a page) without a global handler, you can even forget about `onError` interceptor. I prefer using `.catch` instead of `try/catch`:

```js
// ...inside a method
this.$axios.$get('/api/foo/bar').catch(e => { ... })
Was this page helpful?
0 / 5 - 0 ratings

Related issues

monty086 picture monty086  路  3Comments

lyzs90 picture lyzs90  路  4Comments

ealves-pt picture ealves-pt  路  3Comments

artmarydotir picture artmarydotir  路  4Comments

tebaly picture tebaly  路  3Comments