Network errors
Loading "Network Errors"
Run locally for transcripts
It's time to use API mocking to help us discover blind spots in our
getAuthToken()
function!A common mistake when dealing with the network is forgetting about network error handling. Now, I'm not talking about error responses (we covered those in the previous exercise), but rather the scenarios where the request fails entirely.
Here are a few examples of network errors:
- The requested server is down;
- The client goes offline or the server connection is interrupted;
- The client performs an invalid request.
These errors aren't related to the server response but rather to network connectivity issues. Despite that, we can still model these scenarios using Mock Service Worker!
http.post('/endpoint', () => {
// Produce a network error for this request.
return Response.error()
})
TheResponse.error()
static method is the standard API for creating network errors primarily designed to be used in Service Workers.
In the request handler above, MSW will respond to
POST /endpoint
requests with a network error. That, in turn, will reject the response promise and allow us to test how well we handle that scenario in our code.π¨βπΌ Use what you've learned about the runtime request handlers (
server.use()
) to complete the network error handling test case in .