Permalink
Browse files

feat: pass ctx to errorHandlers

  • Loading branch information...
pi0 committed Sep 8, 2017
1 parent 148f8b8 commit c70749a6c51b3506567820fde4edbfe2f7a5e797
Showing with 6 additions and 6 deletions.
  1. +2 −2 README.md
  2. +4 −4 lib/plugin.template.js
@@ -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)
}
},
```
@@ -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

0 comments on commit c70749a

Please sign in to comment.