mswMock Service Worker
  1. Api
  2. response
  3. networkError()

networkError()

Emulates a network error during the response. Cancels the respective request.

Intended vs unintended exceptions

The res.networkError() call produces an intended exception, as opposed to a throw statement within a request handler, which is considered an unintended exception. Such differentiation is necessary to catch and surface exceptions early during your request handling logic.

Read more about exceptions handling on the Mocking error responses page.

Examples

The request handler below produces a network error in response to the GET /books request:

1rest.get('/books', (req, res, ctx) => {
2 return res.networkError('Failed to connect')
3})