When a server returns an error status code without content, the error is present but undefined
openapi-fetch version
Latest
Description
The context is this code: https://github.com/openapi-ts/openapi-typescript/blob/12f9c29b7653db3ab5d6beb4ac698ce5b58177aa/packages/openapi-fetch/src/index.js#L235
I wonder if we shouldn't have a tru-ish value (maybe just "true") for error here, so that the calling code can simply use if (error). What do you think?
Reproduction
The problem happens when a server returns an error status (such as 405) without any content.
Expected result
The caller can do if (error) simply.
Extra
- [x] I’m willing to open a PR (see CONTRIBUTING.md)
I've also just been bitten by this default behaviour, which I feel is quite dangerous. I would also recommend returning some sort of truthy value here, but I can see it being problematic with the current way the error is returned. I understand it would be a breaking change (and I'm probably missing context!) but I feel it might be easier if the error was an object with "status" and "content" (or something similar) so that consumers could always have access to these informations while keeping the option of having an empty response.
To avoid patching the lib, for now I'm using a middleware to handle this case (still haven't tested it properly):
function emptyErrorResponseMiddleware(): Middleware {
return {
async onResponse({ response }) {
if (response.ok) {
return response;
}
const error = await response.clone().text();
if (error) {
return response;
}
return new Response("Empty response", response);
},
};
}
I guess we could always check response.ok instead of the returned error property.
or test something such as if ("error" in returnValue) instead of destructuring.
But this is very misleading :/
FYI @julienw I noticed you linked a piece of code mentioning it being responsible for this, but the problem is actually happening here (for me at least), since error is empty in this context.
Interesting! In my case I had Content-Length: 0 so I was in the first case. In your case then error should be defined, but maybe it's the empty string (which is falsy)?
Ah, I see. Yeah then both checks are problematic. Content-Length: 0 first (but Content-Length might not be sent from the server) and then the falsy error (empty string).
We have been bitten by this logic too. We use openapi-react-query (uses openapi-fetch under the hood), which wouldn't trigger onError due to an empty error response body. We ended up patching it directly in the openapi-react-query implementation:
const { data, error, response } = await fn(path, { signal, ...init });
- if (error) {
- throw error;
+ if (!response.ok) {
+ throw error ? error : new Error("Unkown error occurred.");
This is not ideal. Will this be looked at at some point?
https://github.com/openapi-ts/openapi-typescript/discussions/2559
Well looks like openapi-fetch is deprecated so guess we dont get this. This middleware seems to work quite nicely though, its a combo of the response code and a "Content-Length" "fix".
const errorStatusMiddleware: Middleware = {
async onResponse({ response }) {
if (!response.ok) {
let headers = response.headers;
try {
let body = {};
try {
body = await response.json();
} catch {
//The error is silenced if 'Content-Length' header is 0.
//To fix this we set a body and a content length, this allows the onError
//in the query/mutation to show a error
const error = "error " + response?.status;
body = { error };
headers = new Headers(response.headers);
headers.set("Content-Length", error.length + "");
}
return new Response(
JSON.stringify({
...body,
statusCode: response.status,
}),
{
status: response.status,
statusText: response.statusText,
headers: headers,
},
);
} catch {
return response;
}
}
return undefined;
},
};
fetchClient.use(errorStatusMiddleware);