Skip to content

Instantly share code, notes, and snippets.

Last active June 9, 2022 06:42
What would you like to do?
Global error handling using axios interceptor for http calls

How to use it

I am assuming you have a Laravel app with axios installed.

  1. Run npm install izitoast --save to pull the iziToast
  2. Create a resources/assets/js/services folder and place toast.js and errorHandler.js in it.
  3. Now open the resources/assets/js/bootstrap.js and add following:
window.axios = require('axios');
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

  1. Thats it, now you should be seeing toast notification on request fail.

Turn off gloabl error handling for specific call

You can turn off the global error handling for a specific request by passing {errorHandle: false} as config in axios call.

axios.get('/user/1', {errorHandle: false}).then((response) => {
    console.log('Everything is awesome.');
}).catch((error) => {
    // handle this error here
    console.warn('Not good man :(');
import axios from 'axios'
import toast from './toast'
function errorResponseHandler(error) {
// check for errorHandle config
if( error.config.hasOwnProperty('errorHandle') && error.config.errorHandle === false ) {
return Promise.reject(error);
// if has response show the error
if (error.response) {
// apply interceptor on response
response => response,
export default errorResponseHandler;
import 'izitoast/dist/css/iziToast.min.css'
import iZtoast from 'izitoast'
const toast = {
error: (message, title = 'Error') => {
return iZtoast.error({
title: title,
message: message,
position: 'bottomCenter'
success: (message, title = 'Success') => {
return iZtoast.success({
title: title,
message: message,
position: 'bottomCenter'
export default toast;
Copy link

This doesn't seem to work with axios 0.19.x. Any idea how to pass the errorHandle flag in the latest version of axios?

Copy link

This makes sense... You can also try this approach I use personally guys medium article

Copy link

devinrhode2 commented Sep 10, 2020

I am using something like this

    // From axios github readme:
    if (error.response) {
      // The request was made and the server responded with a status code
      // that falls out of the range of 2xx
      // toast.error(; // TODO setup toast notification here
    } else if (error.request) {
      // The request was made but no response was received
      // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
      // http.ClientRequest in node.js
    } else {
      // Something happened in setting up the request that triggered an Error
      console.error("Error", error.message);
    console.error("full error object", error);

    // modified from
    // you can individually handle an error for any ajax call by doing this:
    // axios
    //   .get("/user/1", { handleErrorLocally: true })
    //   .then((response) => console.log("Everything is awesome."))
    //   .catch((error) => {
    //     // handle this error here
    //     console.warn("Not good man :(");
    //   });
    if (
      error.config.hasOwnProperty("handleErrorLocally") &&
      error.config.handleErrorLocally === true
    ) {
        "error.config has handleErrorLocally property, returning error object"
      return Promise.reject(error);
    } else {
      // Throwing in this interceptor may be incorrect behavior in some circumstances,
      // please feel free to change this.
      throw new Error("request failed");

Although I think @Godofbrowser's medium article is pretty great. Typescript is great, but I'm not yet that committed to typescript enough to use interface and class ComposedAjaxError implements ComposedError` although I feel that day is coming soon, I'm holding out for optional static typing to become standardized in ES

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment