Skip to content

Override the default error handler when custom error handler is defined #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
- [requestInterceptor](#requestinterceptor)
- [responseInterceptor](#responseinterceptor)
- [init](#init)
- [disableDefaultErrorHandler](#disabledefaulterrorhandler)
- [errorHandler](#errorhandler)
- [Helpers](#helpers)
- [Fetch Style Requests](#fetch-style-requests)
Expand All @@ -69,7 +70,7 @@
Install with npm:
```bash
>_ npm install @nuxtjs/axios
```
```

Install with yarn:
```bash
Expand Down Expand Up @@ -206,7 +207,7 @@ responseInterceptor: (response, ctx) => {
```


Function for manipulating axios responses.
Function for manipulating axios responses.

### `init`
- Default: `null`
Expand All @@ -221,12 +222,20 @@ axios: {
}
```

### `disableDefaultErrorHandler`
- Default: `false`

If you want to disable the default error handler for some reason, you can do it so
by setting the option `disableDefaultErrorHandler` to true.

### `errorHandler`
- Default: (Return promise rejection with error)

Function for custom global error handler.
Function for custom global error handler.
This example uses nuxt default error page.

If you define a custom error handler, the default error handler provided by this package will be overridden.

```js
axios: {
errorHandler (errorReason, { error }) {
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module.exports = function nuxtAxios (moduleOptions) {
proxyHeaders: true,
proxyHeadersIgnore: ['accept', 'host'],
debug: false,
disableDefaultErrorHandler: false,
redirectError: {}
}

Expand Down
6 changes: 3 additions & 3 deletions lib/plugin.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,13 @@ export default <% if (options.init) { %>async<% } %>(ctx, inject) => {
const resInter = <%= serialize(options.responseInterceptor).replace('responseInterceptor(', 'function(').replace('function function', 'function') %>
axios.interceptors.response.use(config => resInter(config, ctx))
<% } %>

// Error handler
axios.interceptors.response.use(undefined, err => errorHandler(err, ctx));

// Error handler
<% if (options.errorHandler) { %>
// Custom error handler
axios.interceptors.response.use(undefined, err => customErrorHandler(err, ctx))
<% } else if (options.disableDefaultErrorHandler === false) { %>
axios.interceptors.response.use(undefined, err => errorHandler(err, ctx));
<% } %>

// Inject axios to the context as $axios
Expand Down