Open mikicho opened 2 months ago
I have the same issue currently when using @mswjs/interceptors
through nock
. I am currently unable to produce a reproducible example, so it is very difficult to investigate this.
Does anyone have an idea for a workaround until this is fixed? @mikicho maybe?
@marikaner In order to keep this issue clean, I opened an issue in nock
to track this and discuss about workaround (https://github.com/nock/nock/issues/2789)
I think it should be safe to allow arbitrary error reasons. .destroy()
isn't strict about this, as you've mentioned, and some other clients don't support custom error reasons at all.
The only thing, I'd be careful with the handleRequest
as it tackles a bunch of scenarios and some of them are not that apparent. But all are covered with tests so any change will be verified.
I'd probably suggest to refactor handleRequest
to have two internal callbacks: onResolve
and onReject
that are called whenever something is returned via .respondWith()
or something throws during the request resolution (no matter if via throw
or .errorWith()
). Basically, to streamline the request handling to two paths:
The current handleResponse
and handleResponseError
are too vague. Besides, there are situations when thrown errors are responses and get handled as response, which is confusing to read:
We can likely keep the existing handleResponse
/handleResponseError
, maybe rename them, and use them within the newly added onResolve
and onReject
callbacks.
@mikicho, would you like to give this one a try?
Sure! I'd love to contribute to this one... I'm looking into it.
@kettanaito I need clarification on what you have in mind. How is it different from what we currently have?
export async function handleRequest(options) {
const handleResponse..
const handleResomposeError..
...
const result = await until...
if (result.error)... // a.k.a onReject
if (result.data)... // a.ka onResolve
}
Right now we have cases where rejections can be translated to successful response handling:
And successful responses translated to errors:
This all is intended but reading it is a bit confusing. So I proposed to have higher-level onResolve
and onReject
callbacks within the handleRequest
function to make it easier to read. It's much easier to understand "onResolve triggered with an error, so treat it as an error" than "successful response callback triggered with an error, so it's an error". Do you see my line of thinking?
interceptors
expects the error to be from typeError
inhandleRequest
, which is not required by Node.If the interceptor is applied, the program prints
response
and throws:For real requests, it is called the
error
callback.WDYT?