diff --git a/README.md b/README.md index 59ba2ed4..17d6f268 100755 --- a/README.md +++ b/README.md @@ -48,6 +48,7 @@ - [requestInterceptor](#requestinterceptor) - [responseInterceptor](#responseinterceptor) - [init](#init) + - [disableDefaultErrorHandler](#disabledefaulterrorhandler) - [errorHandler](#errorhandler) - [Helpers](#helpers) - [Fetch Style Requests](#fetch-style-requests) @@ -69,7 +70,7 @@ Install with npm: ```bash >_ npm install @nuxtjs/axios -``` +``` Install with yarn: ```bash @@ -206,7 +207,7 @@ responseInterceptor: (response, ctx) => { ``` -Function for manipulating axios responses. +Function for manipulating axios responses. ### `init` - Default: `null` @@ -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 }) { diff --git a/lib/index.js b/lib/index.js index 25a3a20d..e2d3c10a 100755 --- a/lib/index.js +++ b/lib/index.js @@ -19,6 +19,7 @@ module.exports = function nuxtAxios (moduleOptions) { proxyHeaders: true, proxyHeadersIgnore: ['accept', 'host'], debug: false, + disableDefaultErrorHandler: false, redirectError: {} } diff --git a/lib/plugin.template.js b/lib/plugin.template.js index 5e8b8388..05cd383b 100755 --- a/lib/plugin.template.js +++ b/lib/plugin.template.js @@ -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