Error Handling and Global Interceptors
Handle request errors and configure Axios interceptors.
Requests Can Fail
Networks drop, servers return errors, and tokens expire. Robust apps anticipate failure and respond gracefully instead of crashing or leaving the user staring at a spinner forever.
try/catch Around Requests
Wrap awaited requests in try/catch to capture errors. Axios throws when the server responds with a 4xx or 5xx status.
async fetchPosts() {
try {
const res = await api.get("/posts")
this.posts = res.data
} catch (err) {
console.error(err)
}
}All lessons in this course
- HTTP Requests with Axios
- Lifecycle Hooks and Data Fetching
- Error Handling and Global Interceptors