nuxt-community / axios-module

Secure and easy axios integration for Nuxt 2
https://axios.nuxtjs.org
MIT License
1.19k stars 245 forks source link

Global error handler by Nuxt default error() function #463

Closed DengSihan closed 3 years ago

DengSihan commented 3 years ago

Thanks for what a remarkable job you have done!

I wonder if I can use the default error() function of Nuxtjs to handle axios errors globally as same as what we do in middlewares like below:

// ~/plugins/axios.js

export default ({ $axios, error }) => {
    $axios.onResponseError(e => {
        error({
            statusCode: e.response.status,
            message: e.response.statusText
        });
    });
}

But it doen't work as expected.

What I want: image What I get: image

DengSihan commented 3 years ago

sorry for my careless, it's already in helpers

export default function ({ $axios, error: nuxtError }) {
  $axios.onError(error => {
    nuxtError({
      statusCode: error.response.status,
      message: error.message,
    });
    return Promise.resolve(false);
  })
}