diff --git a/README.md b/README.md index 35ca1407..84134997 100755 --- a/README.md +++ b/README.md @@ -170,8 +170,8 @@ This example uses nuxt default error page. ```js axios: { - errorHandler (error) { - this.error('Request Error: ' + error) + errorHandler (error, { error }) { + error('Request Error: ' + error) } }, ``` diff --git a/lib/plugin.template.js b/lib/plugin.template.js index f5d8c4ac..c45bdb45 100755 --- a/lib/plugin.template.js +++ b/lib/plugin.template.js @@ -45,12 +45,12 @@ function setupHelpers( axios ) { const redirectError = <%= serialize(options.redirectError) %> // Set appreciate `statusCode` and `message` to error instance -function errorHandler(error) { +function errorHandler(error, ctx) { if (error.response) { // Error from backend (non 2xx status code) // ...Auto redirect on special status codes if (redirectError[error.response.status]) { - this.redirect(redirectError[error.response.status]) + ctx.redirect(redirectError[error.response.status]) } error.statusCode = error.statusCode || parseInt(error.response.status) || 500 error.message = error.message || error.response.statusText || (error.statusCode + ' (Internal Server Error)') @@ -175,11 +175,11 @@ export default <% if (options.init) { %>async<% } %>(ctx, inject) => { <% } %> // Error handler - axios.interceptors.response.use(undefined, errorHandler.bind(ctx)); + axios.interceptors.response.use(undefined, err => errorHandler(err, ctx)); <% if (options.errorHandler) { %> // Custom error handler - axios.interceptors.response.use(undefined, customErrorHandler.bind(ctx)) + axios.interceptors.response.use(undefined, err => customErrorHandler(err, ctx)) <% } %> // Inject axios to the context as $axios