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!One of the common mistakes when dealing with the network is forgetting about network error handling. Now, I'm not talking about error responses (we've handled those in the previous exercise) but about the scenarios when the request fails altogether.
Here's 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 have nothing to do with the server response but rather with the network connectivity. Despite that, we can still model those scenarios using Mock Service Worker!
http.post('/endpoint', () => {
// Produce a network error for this request.
return Response.error()
})
TheResponse.error()
static method is a 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 code.π¨βπΌ Use what you've learned about the runtime request handlers (
server.use()
) to complete the network error handling test case in .