Skip to content

Instantly share code, notes, and snippets.

@JaysonChiang
Last active June 12, 2021 10:30
Show Gist options
  • Save JaysonChiang/323f372c278ddc23071947f2da61eba0 to your computer and use it in GitHub Desktop.
Save JaysonChiang/323f372c278ddc23071947f2da61eba0 to your computer and use it in GitHub Desktop.
improve with catch api.js
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