msw icon indicating copy to clipboard operation
msw copied to clipboard

MSW makes Sentry requests fail

Open kamilkazmierczakMtab opened this issue 2 years ago • 3 comments

Prerequisites

Environment check

  • [X] I'm using the latest msw version
  • [X] I'm using Node.js version 14 or higher

Browsers

Chromium (Chrome, Brave, etc.)

Reproduction repository

not possible to add (requires sentry configuration)

Reproduction steps

  1. Create project in https://sentry.io/welcome/
  2. Use sentry with msw in same project
  3. Every sentry request will fail

I do not know what is so special about these requests but MSW is doing sth to them and makes them fail.

Current behavior

It makes sentry requests fail

The only way for me to fix it is to make early return in mockServiceWorker.js when sentry request comes - but I do not want to change anything in that file - also It would be nice to have some option to force msw to ignore some requests (I tried adding 'x-msw-bypass': 'true' header but it didn't work)

Expected behavior

It does not make sentry requests fail

Error

image

kamilkazmierczakMtab avatar Jul 19 '22 10:07 kamilkazmierczakMtab

Hey, @kamilkazmierczakMtab. Thanks for reporting this.

One quick question: do you intend to mock Sentry requests or should they pass through?

It looks like the request passes through, but when MSW attempts to clone its response and read its body (to forward it to your console), it appears to have been already read. There are a few points we can learn from this:

  1. Don't tie response signaling to the request resolution logic. We should refactor it so that even if reading the response fails, it doesn't affect the request in the Network. Should be a matter of handling getResponse() errors gracefully in the worker.
  2. Somehow responses may arrive read in the worker (?). This makes little sense to me because passthrough requests are still initiated by the worker, so it should be the first layer to read them.

It's a shame that this scenario needs the whole Sentry setup to look into. I may not have time to configure Sentry right now, this is welcome to be picked up by anybody interested.

kettanaito avatar Jul 19 '22 11:07 kettanaito

I want Sentry requests to pass through

kamilkazmierczakMtab avatar Jul 19 '22 11:07 kamilkazmierczakMtab

You can use simple example from sentry with react to make those requests (whole config of sentry is defined on this page) https://docs.sentry.io/platforms/javascript/guides/react/#verify

kamilkazmierczakMtab avatar Jul 19 '22 11:07 kamilkazmierczakMtab

Same problem. In my case, this occurs only in Brave. When I access it with Chrome, there is no problem.

devinoue avatar Apr 20 '23 15:04 devinoue

My next question is this: have you tried capturing the Sentry requests in a handler and returning req.passthrough()?

rest.get('/some/sentry/path', (req) => req.passthrough())

This will instruct MSW to bypass this request. Put this on top of your request handlers and all Sentry requests should be bypassed then.

Please try this and let me know.

kettanaito avatar Apr 28 '23 14:04 kettanaito

Please migrate to MSW 2.0 and let me know if the issue is still there. I don't think it will be. 2.0 is a full rewrite that brings MSW closer to the Fetch API standard, which means cloning request/response bodies is more reliable and even optimized with the latest changes in caching.

kettanaito avatar Jan 31 '24 09:01 kettanaito