You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
REST query is mistaken for GraphQL query and can't be intercepted (throws an error) #732
In order to provide meaningful handler suggestions, it first needs to parse the request as REST/GraphQL. I believe in the case of running under onUnhandledRequest, it shouldn't throw an exception on invalid query, because the request type is ambiguous at this point (whereas when run in a GraphQL handler we know you're expecting GraphQL, so throwing on an invalid query is a viable behavior).
We could benefit from wrapping the parseGraphQLRequest call in a function that'd silence any exceptions raised, because they are not relevant in the current execution context:
is made before the rest of the handler selection, at this stage I don't know if it is possible to tell that a request is definitely not a graphQL request and should not be parsed
the error printing is just confusing when you're not using GraphQL, even if it does not seem to cause a pb
Your request has no matching request handler, so MSW attempts to handle it via onUnhandledRequest. That's only to produce a warning/error if you set it up to do so. It also attempts to parse all unhandled requests to provide you with smart suggestions based on your existing handlers (maybe you made a typo). It's during that step that it fails because it assumes it can safely call parseGraphQLRequest on an arbitrary request, which isn't the case.
That's why I'm suggesting to wrap the parseGraphQLRequest call only in the onUnhandledRequest function to ignore any thrown exceptions/errors. If the parsing uses console.error to print out the parsing error, I'd argue it should throw instead. That way its exceptions can be handled in the parental scope.
Wrap the parseGraphQLRequest call in the onUnhandledRequest function in something like maybe or tryCatch custom function that would silence any exceptions thrown.
Activity
kettanaito commentedon May 12, 2021
Hey, @thomasmery.
Do you have any request handlers defined at all? If so, could you please post them?
Looks like the issue happens when MSW attempts to call
onUnhandledRequestfor this request for which you have no request handler defined:msw/src/utils/request/onUnhandledRequest.ts
Line 129 in 9caef0a
In order to provide meaningful handler suggestions, it first needs to parse the request as REST/GraphQL. I believe in the case of running under
onUnhandledRequest, it shouldn't throw an exception on invalidquery, because the request type is ambiguous at this point (whereas when run in a GraphQL handler we know you're expecting GraphQL, so throwing on an invalid query is a viable behavior).We could benefit from wrapping the
parseGraphQLRequestcall in a function that'd silence any exceptions raised, because they are not relevant in the current execution context:And the utility would be:
Please, would you be interested in opening a pull request with this fix? We could use your help.
thomasmery commentedon May 12, 2021
Hi @kettanaito
thanks for the quick reply
I actually made a mistake when reporting but the error is not thrown but only printed to the console
msw/src/utils/internal/parseGraphQLRequest.ts
Line 143 in 9caef0a
makes this check
that allows the parsing to occur when query param is present
if it can't parse it just says so and I believe the rest of the handler selection can happen
I'd be happy to help but I confess I don't know why
msw/src/utils/request/onUnhandledRequest.ts
Line 129 in 9caef0a
is made before the rest of the handler selection, at this stage I don't know if it is possible to tell that a request is definitely not a graphQL request and should not be parsed
the error printing is just confusing when you're not using GraphQL, even if it does not seem to cause a pb
FYI my handlers resemble this one
kettanaito commentedon May 12, 2021
Your request has no matching request handler, so MSW attempts to handle it via
onUnhandledRequest. That's only to produce a warning/error if you set it up to do so. It also attempts to parse all unhandled requests to provide you with smart suggestions based on your existing handlers (maybe you made a typo). It's during that step that it fails because it assumes it can safely callparseGraphQLRequeston an arbitrary request, which isn't the case.That's why I'm suggesting to wrap the
parseGraphQLRequestcall only in the onUnhandledRequest function to ignore any thrown exceptions/errors. If the parsing usesconsole.errorto print out the parsing error, I'd argue it should throw instead. That way its exceptions can be handled in the parental scope.thomasmery commentedon May 12, 2021
thanks,
makes sense if I turn the logging into a throwing
I'll try and open a PR by the end of the week :)
kettanaito commentedon May 12, 2021
We can try to
parseGraphQLRequestcall in theonUnhandledRequestfunction in something likemaybeortryCatchcustom function that would silence any exceptions thrown.Thank you for your support!
Fix confusing error logging on unhandled request query parsing
Merge branch 'master' into fix/mswjs#732
Fix confusing error logging on unhandled request query parsing
Fix confusing error logging on unhandled request query parsing