Skip to content

Instantly share code, notes, and snippets.

@saqueib
Last active June 9, 2022 06:42
Show Gist options
  • Save saqueib/a495af17d7c0e2fd5c2316b0822ebac3 to your computer and use it in GitHub Desktop.
Save saqueib/a495af17d7c0e2fd5c2316b0822ebac3 to your computer and use it in GitHub Desktop.

Revisions

  1. saqueib revised this gist Jun 7, 2020. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -14,8 +14,8 @@ require('./services/errorHandler');
    ```
    4. Thats it, now you should be seeing toast notification on request fail.

    ## Turn on gloabl error handling for specific call
    You can turn on the global error handling for a specific request by passing `{errorHandle: false}` as config in axios call.
    ## 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.

    ```js
    axios.get('/user/1', {errorHandle: false}).then((response) => {
  2. saqueib revised this gist Sep 8, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -15,10 +15,10 @@ require('./services/errorHandler');
    4. Thats it, now you should be seeing toast notification on request fail.

    ## Turn on gloabl error handling for specific call
    You can turn on the global error handling for a specific request by passing `{handleError: false}` as config in axios call.
    You can turn on the global error handling for a specific request by passing `{errorHandle: false}` as config in axios call.

    ```js
    axios.get('/user/1', {handleError: false}).then((response) => {
    axios.get('/user/1', {errorHandle: false}).then((response) => {
    console.log('Everything is awesome.');
    }).catch((error) => {
    // handle this error here
  3. saqueib revised this gist Sep 8, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ require('./services/errorHandler');
    4. Thats it, now you should be seeing toast notification on request fail.

    ## Turn on gloabl error handling for specific call
    You can turn on the global error handling for a specific request by passing `{handleErrors: false}` as config in axios call.
    You can turn on the global error handling for a specific request by passing `{handleError: false}` as config in axios call.

    ```js
    axios.get('/user/1', {handleError: false}).then((response) => {
  4. saqueib revised this gist Sep 8, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,7 @@ require('./services/errorHandler');
    You can turn on the global error handling for a specific request by passing `{handleErrors: false}` as config in axios call.

    ```js
    axios.get('/user/1', {handleErrors: false}).then((response) => {
    axios.get('/user/1', {handleError: false}).then((response) => {
    console.log('Everything is awesome.');
    }).catch((error) => {
    // handle this error here
  5. saqueib revised this gist May 27, 2018. 1 changed file with 27 additions and 0 deletions.
    27 changes: 27 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    ## 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:

    ```js
    ...
    window.axios = require('axios');
    window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

    require('./services/errorHandler');
    ```
    4. Thats it, now you should be seeing toast notification on request fail.

    ## Turn on gloabl error handling for specific call
    You can turn on the global error handling for a specific request by passing `{handleErrors: false}` as config in axios call.

    ```js
    axios.get('/user/1', {handleErrors: false}).then((response) => {
    console.log('Everything is awesome.');
    }).catch((error) => {
    // handle this error here
    console.warn('Not good man :(');
    })
    ```
  6. saqueib created this gist May 26, 2018.
    22 changes: 22 additions & 0 deletions errorHandler.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    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) {
    toast.error(error.response.data.message);
    }
    }

    // apply interceptor on response
    axios.interceptors.response.use(
    response => response,
    errorResponseHandler
    );

    export default errorResponseHandler;
    21 changes: 21 additions & 0 deletions toast.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    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;