msw
msw copied to clipboard
MSW makes Sentry requests fail
Prerequisites
- [X] I confirm my issue is not in the opened issues
- [X] I confirm the Frequently Asked Questions didn't contain the answer to my issue
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
- Create project in https://sentry.io/welcome/
- Use sentry with msw in same project
- 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
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:
- 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. - 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.
I want Sentry requests to pass through
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
Same problem. In my case, this occurs only in Brave. When I access it with Chrome, there is no problem.
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.
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.