Skip to content

Commit

Permalink
feat: disable sefault error handler (#44)
Browse files Browse the repository at this point in the history
* Override the default error handler

Otherwise there is the possibility that they are conflicting with each other.
Also added a option, where you can disable the errorHandler at all (for example if you work with the responseInterceptor)

* Changed option name and added the documentation.

Renamed the option to `disableDefaultErrorHandler`.
Added documention for the new option.
  • Loading branch information
pschmidt88 authored and pi0 committed Oct 12, 2017
1 parent 06f20a6 commit f1e95ff
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
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

0 comments on commit f1e95ff

Please sign in to comment.