mockServiceWorker.js:129 [MSW] Caught an exception from the "POST https:// ...."request (TypeError: Failed to fetch)

1.8k Views Asked by At

I have a react web app where i'm mocking the api requests through MSW. I'm now trying to integrate a third party error reporting service like sentry. The client calls sentry's endpoint whenever an error happens, so I need the msw to bypass this call. The problem now is that MSW is interfeering somehow and makes the call to never reach sentry's endpoint. I know this cause disabling MSW allowed the call to reach it perfectly.

The url sentry's client is trying to reach:

https://token.ingest.sentry.io/api/foo?bar=mee

How I start MSW (shouldn't this part do the trick?)

worker.start({ onUnhandledRequest: 'bypass' })

Error I get

mockServiceWorker.js:129 [MSW] Caught an exception from the "POST https://________________________.ingest.sentry.io/api/_____________/envelope/?sentry_key=__________________________________&sentry_version=7&sentry_client=sentry.javascript.browser%2F7.21.1" request (TypeError: Failed to fetch). This is probably not a problem with Mock Service Worker. There is likely an additional logging output above.
(anonymous) @ mockServiceWorker.js:129
Promise.catch (async)
(anonymous) @ mockServiceWorker.js:118
fetch.ts:35          POST https://________________________.ingest.sentry.io/api/_____________/envelope/?sentry_key=__________________________________&sentry_version=7&sentry_client=sentry.javascript.browser%2F7.21.1 net::ERR_FAILED
1

There are 1 best solutions below

1
Danziger On

In MSW version 4, req.passthrough() might be what you are looking for:

rest.post('/api/foo', (req, res, ctx) => {
    return req.passthrough();
});