question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Provide a way (or document an existing way) to return a NetworkError once

See original GitHub issue

Is your feature request related to a problem? Please describe.

I would like to have a documented way of defining a handler that returns a NetworkError only once, then falls back to an existing handler. This can be helpful in testing an app’s request-retrying logic.


I know how to return a valid response or a failing status code once:

server.use(
  rest.get('/foo', (_req, res, ctx) => {
    return res.once(
      ctx.status(404, 'Not found')
    );
  })
);

And I know how to return a network error:

server.use(
  rest.get('/foo', (_req, res, ctx) => {
    return res.networkError('Failed to conect');
  })
);

But since these both depend on returning the result of different methods on res, I’m not sure how to use the current API to return a network error once.


I tried a few options, including the three below, but they all continued to respond with errors after the first request. They also resulted in TypeScript errors, which makes sense:

server.use(
  rest.get('/foo', (_req, res, ctx) => {
    return res.once(res.networkError('Failed to conect'));
  })
);
server.use(
  rest.get('/foo', (_req, res, ctx) => {
    return res.once(() => res.networkError('Failed to conect'));
  })
);
server.use(
  rest.get('/foo', (_req, res, ctx) => {
    return res.once((innerRes) => innerRes.networkError('Failed to conect'));
  })
);

Describe the solution you’d like

I’m not familiar with msw’s internals, so there may be a better fit for the API, but an intuitive solution for me would be:

  • Provide networkError on the ctx object. This would be consistent with the API for changing just about every other aspect of a response (networkError strikes me as out of place on the res object).

It could then be used something like this:

server.use(
  rest.get('/foo', (_req, res, ctx) => {
    return res.once(
      ctx.networkError('Failed to conect')
    );
  })
);

Describe alternatives you’ve considered

Another good option would be to provide networkError as a method on once:

server.use(
  rest.get('/foo', (_req, res, ctx) => {
    return res.once.networkError('Failed to conect');
  })
);

Or to provide once on rest and graphql and all request methods on once:

server.use(
  rest.once.get('/foo', (_req, res, ctx) => {
    return res.networkError('Failed to conect');
  })
);

Or to provide once as a method of all request methods and have it inherit the preceding method’s behavior:

server.use(
  rest.get.once('/foo', (_req, res, ctx) => {
    return res.networkError('Failed to conect');
  })
);

Additional context

I’m guessing there may already be a way of accomplishing this, just requiring digging into the internals. If so, it would be wonderful to add documentation for it. This would help users and discourage them from relying on undocumented portions of the API that might change.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
kettanaitocommented, May 2, 2022

I’m considering the introduction of “intentions” returned from the resolver (see #1207). Returning a mocked response object is no longer flexible as the scenarios for mocks grow in complexity. I believe the intention-driven design will allow us to make resolvers more straightforward. It can also allow us to decouple intentions (“return network errors”) from modifiers (always/once), which should fix this particular issue.

1reaction
noahbrennercommented, Dec 24, 2020

I found a workaround for now, though I’d still love to see this supported natively. Here’s what’s working for me:

function enableSuccessfulResponse() {
  server.use(
    rest.get("http://example.com", (_req, res, ctx) => {
      return res(ctx.json({ foo: "bar" }));
    })
  );
}

server.use(
  rest.get("http://example.com", (_req, res) => {
    enableSuccessfulResponse();
    return res.networkError("Failed to connect");
  })
);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Error Link - Apollo GraphQL Docs
Use the onError link to perform custom logic when a GraphQL or network error occurs. You pass this link a function that's executed...
Read more >
Developers - Provide a way (or document an existing way) to return ...
Provide a way (or document an existing way) to return a NetworkError once.
Read more >
Network error code 0x80090330 - existing connection was ...
Network error code 0x80090330 - existing connection was forcibly closed by the remote host. Hi, We have on going issue within our one...
Read more >
Network Error Logging - W3C
Existing methods (such as synthetic monitoring) provide a partial solution ... we must also know how many successful requests are occurring; ...
Read more >
How To Fix the HTTP 302 Error (5 Methods) - Kinsta
Once you know that, you can figure out what's gone wrong and how to make it right. In this post, we'll tell you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found