Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from tieme-ndo/bg-handle-network-failure
bug(axios): handle network error globally
- Loading branch information
Showing
10 changed files
with
142 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/components/hoc/axiosErrorHandler/AxiosErrorInterceptor.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import React, { useEffect } from 'react'; | ||
import axios from 'axios'; | ||
import { toast } from 'react-toastify'; | ||
|
||
const AxiosErrorInterceptor = WrappedComponent => { | ||
return function Handler() { | ||
useEffect(() => { | ||
const reqIn = axios.interceptors.request.use(req => req); | ||
const resIn = axios.interceptors.response.use( | ||
function(response) { | ||
return response; | ||
}, | ||
function(error) { | ||
if (error.message === 'Network Error') { | ||
toast.error('Network error, please check your internet connection'); | ||
} | ||
return Promise.reject(error); | ||
} | ||
); | ||
return () => { | ||
axios.interceptors.request.eject(reqIn); | ||
axios.interceptors.response.eject(resIn); | ||
}; | ||
}, []); | ||
return <WrappedComponent />; | ||
}; | ||
}; | ||
|
||
export default AxiosErrorInterceptor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters