Skip to content

Commit

Permalink
feat: pass ctx to errorHandlers
Browse files Browse the repository at this point in the history
  • Loading branch information
Pooya Parsa committed Sep 8, 2017
1 parent 148f8b8 commit c70749a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
},
```
Expand Down
8 changes: 4 additions & 4 deletions lib/plugin.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)')
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c70749a

Please sign in to comment.