-
-
Notifications
You must be signed in to change notification settings - Fork 473
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Failed to construct 'Response': The status provided (0) is outside the range [200, 599] #529
Comments
Hi @Yagogc, I'm pretty sure what is happening here. In the latest release, we have introduced event handling. When the service worker receives a response from a request it will be sent to the client and the user could listen for Before it a new
The network error can be handled using |
Hey, @Yagogc. Thanks for reporting this. I'll set up a test scenario that reproduces this. Hopefully, it'll help us find the right solution to the issue. Thanks for the investigation, @marcosvega91. I suspect you're right, still will follow up after I have the test failing. |
Watching as I'm getting the same behaviour with rest mocking. It is also affecting image requests that have nothing to do with the mocked request (not sure if related). |
Taking this stack trace into account, the exception seems to originate from the Service Worker file: [MSW] Mocking enabled.
index.js:1034 Uncaught RangeError: Failed to construct 'Response': The status provided (0) is outside the range [200, 599].
at index.js:1034
at ServiceWorkerContainer.<anonymous> (index.js:1200) The only place where we construct a Lines 248 to 253 in 9c89476
@lwhiteley, may I please ask for your help in reproducing this issue? I've written the following test, but it passes: const worker = setupWorker(
graphql.mutation('BookAppointment', (req, res, ctx) => {
return res(ctx.status(422, 'Custom status text'))
}),
)
test('handles 0 response status code', async () => {
const runtime = await createRuntime()
const res = await executeOperation(runtime.page, {
query: `
mutation BookAppointment {
appointment {
id
}
}
`,
})
const status = res.status()
const statusText = res.statusText()
expect(status).toBe(422)
expect(statusText).toBe('Custom status text')
return runtime.cleanup()
}) I also confirm that it works as expected on runtime. If you can, would you mind to contribute a pull request with a failing test? We have the Contribution guidelines on how to do that, so don't worry if you've never done something like this before. We'd be glad to have you involved and will help once there's a reproducible test. Thanks! |
@kettanaito I wont be able to do any investigation until weekend or possibly in the evening for me. but i can let you know my observations for now. Issues:
Notes:
|
This seems odd. If that's the case, then the only point where MSW may cause such an exception is when handling a bypass request (the one that doesn't have a request handler, i.e. static assets). This is also contradictory to your original description: you mock a Please, may I ask you to provide a reproduction repository with this issue? I was unsuccessful in reproducing it in a test using your example above—the test passes and the mutation is intercepted. |
@kettanaito i think you're mixing my specific issue with the issue of the original reporter. Im saying that this same error happens for mocking |
https://github.com/lwhiteley/msw-error-reproduce here is a reproduction. As you can see, i mocked the request but in the app i never actually invoke it .. as this is a fresh create-react-app template without any mods except msw unfortunately it doesnt happen all the time but im trying to replicate it definitely as it always happens in my app |
@kettanaito ive updated the example to increase possibility of it happening .. https://github.com/lwhiteley/msw-error-reproduce How to reproduce:
check the console for the error |
@lwhiteley my bad, I've indeed mixed those two issues. Thank you for preparing that repo, I'll check it out and perhaps both issues will have the same cause. |
I just encountered the exact same issue as @lwhiteley's. But the mocks are not broken. My handlers look like this: // mocks/handlers.js
import { rest } from 'msw';
export const handlers = [
rest.get('*/an_end_point', (req, res, ctx) => res(ctx.json({
data: {
something: {
data: {
property_a: 1.0,
property_b: 1.0
}
}
}
})))
]; Other files: // index.js
...
const { worker } = require('./mocks/browser');
worker.start();
... // mocks/browser.js
import { setupWorker, rest } from 'msw';
import { handlers } from './handlers';
// This configures a Service Worker with the given request handlers.
export const worker = setupWorker(...handlers);
// Make the `worker` and `rest` references available globally,
// so they can be accessed in both runtime and test suites.
window.msw = {
worker,
rest,
}; I can make a repository if it's needed @kettanaito |
For me this happens for CORS requests (not sure if for all). |
I'm having the same problem, downgrading to the |
Having the same issue. I have the exact set up as listed in the documentation. |
Seems like the culprit here are CORS requests, that don't have CORS headers included on the response. I'm not sure, but I think we won't be able to return a |
Seeing this as well after upgrading to 25. Some debugging info here, looks like this one hasn't been reported yet.
function createResponseListener(context) {
return (_, message) => {
const { payload } = message;
if(!payload || !payload.status) {
console.log('Payload status 0 or missing', payload);
debugger
}
const response = new Response(payload.body, payload);
... Active request on network tab is for react-devtools: If I disable react-devtools extension I'm unable to reproduce this issue. Immediately when dev-tools are activated this issue comes up all the time. Should MSW somehow filter out these extension requests? |
same issue here, Only occur the first page load after execute |
@AriPerkkio that's a good insight. I think the issue is connected to #536, as MSW may be attempting to handle a request originating from a different source than the client-side of MSW (i.e. React DevTools). @timdeschryver, thank you for the update on that! If there's a CORS request without CORS headers in the response, doesn't that raise an exception, halting the request cycle in the worker? 🤔 I wonder if we can simulate such CORS request/response and put it into a test suite. |
I was planning on trying to reproduce this behavior in our test suites today, I'll keep you updated. |
I created a reproduction in our test suite at https://github.com/mswjs/msw/tree/reproduce-529. I still think that we need to handle these responses differently, and current I'm thinking of the following options:
|
I confirm your test suite reproducing the issue reliably, @timdeschryver. Upon further inspection, I can see that due to the CORS request we get a Since such responses cannot be handled in JavaScript per specification, we cannot construct a I suggest fixing this by analyzing the |
Based on the amazing job done by @timdeschryver and other participants of this thread, I've added a pull request (#564) that should fix this issue. MSW will not attempt to construct a @lwhiteley, I confirm that the aforementioned pull request fixes the issue in your reproduction repository. Once more huge thanks for preparing that! |
Does this mean that v 0.25 will have this update? I tried npm i msw@latest but am still getting this error. Delete all node modules as well and reinstalled. |
i assume it will be 0.26 @Justinohallo. i have downgraded to 0.24 until the change has been released |
@Justinohallo this fix isn't included in v0.25. |
@lwhiteley @tigerabrodi Thank you! |
The fix will be published in the next minor version—0.26.0. We need to see if we can tackle #536 in the same release, but I'd expect this to be published this week. |
Environment
Request handlers
Actual request
Current behavior
When running the react application I'm getting the following logs:
Obviously the MSW is not intercepting said mutation...
Any ideas? I'm guessing that I'm making a silly mistake that I'm not able to see -_-
Expected behavior
The only log I should see is the MSW getting initialise:
The text was updated successfully, but these errors were encountered: