Last active
June 12, 2021 10:30
-
-
Save JaysonChiang/323f372c278ddc23071947f2da61eba0 to your computer and use it in GitHub Desktop.
improve with catch api.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
import axios from 'axios'; | |
axios.defaults.baseURL = 'https://jsonplaceholder.typicode.com'; | |
axios.interceptors.response.use(res => res.data | |
, error => { | |
const { data, status, config } = error.response; | |
switch(status) { | |
case 400: | |
console.error(data); | |
break; | |
case 401: | |
console.error('unauthorised'); | |
break; | |
case 404: | |
console.error('/not-found'); | |
break; | |
case 500: | |
console.error('/server-error'); | |
break; | |
} | |
return Promise.reject(error); | |
}); | |
const api = { | |
list: () => axios.get('/todos'), | |
details: (id) => axios.get(`/todos/${id}`) | |
} | |
export default api; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment