Loading
Current section: Network 7 exercises
Problem

Error responses

Loading exercise

Transcript

00:00 One of the reasons you want to be in charge of the network is so you're able to reproduce all sorts of network-dependent scenarios. Like here, our getofftoken function has a special handling for 401 responses. Those are the cases when the credentials you provided were invalid,

00:16 and when that happens it throws this nice error message. So it would be nice for us to return this 401 response so we're able to trigger this particular logic in tests. But if we head to our handlers and just replace the mock response to be an error response, we will be hard-coding it

00:33 for all the matching requests in tests. And that's not a great behavior. In fact, a much better approach is to keep this handlers array exclusively dedicated to happy path behaviors for situations when your app's supposed to work normally. This way you'd be able to achieve proper

00:50 network layering. But how do you do that layering? For that, you will create a new special kind of request handler called a runtime request handler. You will use server.use API to do that. But for that API to work, you need access to this server reference. And this is the same server object we

01:08 have in our test setup right here when we called the setup server function from MSW. But importing this Vita setup in tests is not a good idea. So before you do anything, your task is to follow these instructions and do a bit of refactoring so you're able to reference the same server instance

01:24 both in the test setup and in individual tests. And once you're able to do that, complete this error response test case by providing a runtime request handler for our post request that will always respond with an error response.