Created
August 12, 2018 14:40
-
-
Save ortonomy/0e29ccad548d8891ddcebccd399f538a to your computer and use it in GitHub Desktop.
using axios
This file contains hidden or 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
export const axios = Axios.create({ | |
baseURL: `${window.location.protocol}//${Connection().PREPSMITH_HOST}`, | |
withCredentials: true | |
}) | |
// axios errors | |
export const ERRORS = { | |
AUTH_ERROR: new Error('User not logged in'), | |
NETWORK_ERROR: new Error('Network error'), | |
SERVER_ERROR: new Error('Server error'), | |
OTHER_ERROR: new Error('Something went wrong'), | |
MISSING_PARAMS: new Error('Missing parameters') | |
} | |
// axios errors | |
axios.interceptors.response.use(r => r, e => { | |
if ( !e.response ) { | |
return Promise.reject(ERRORS.NETWORK_ERROR); | |
} | |
if ( e.response.status === 403 ) { | |
return Promise.reject(ERRORS.AUTH_ERROR); | |
} | |
if ( e.response.status === 500 || e.response.status === 400 ) { | |
return Promise.reject(ERRORS.SERVER_ERROR); | |
} | |
return Promise.reject(ERRORS.OTHER_ERROR); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment