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
No nice way to handle 202 Accepted #228
Labels
Comments
Hey @voronaam, sorry for the late answer.
One way of doing this would be to add a tiny middleware which throws a custom error on 202s and catch it: class NoContentError extends Error {
name = 'NoContentError';
message = '202 No Content';
}
const api = wretch(`https://httpstat.us`)
.middlewares([next => async (url, opts) => {
const res = await next(url, opts);
if (res.status === 202) {
throw new NoContentError()
}
return res;
}])
.catcher("NoContentError", (error) => {
console.log(error.message)
})
api.url('/200')
.post({ hello: "world" })
.text((responseBody) => {
console.log(responseBody)
})
api.url('/202')
.post({ hello: "world" })
.text((responseBody) => {
console.log(responseBody)
}) |
Thank you! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
We were pretty happy with a nice
Until the backend added a feature to process large requests in the background. In which case the API return
HTTP 202 Accepted
and no body.I tried to catch it with
.error(202, (res) => {...})
but since 202 is not a error response, it did not work.In the end I had to resort to the raw response handling as in:
The actual code is a bit more ugly because of extra
.catch(...)
everywhere.Is there a more wretch-like way to handle the 202 response status?
The text was updated successfully, but these errors were encountered: