-
-
Notifications
You must be signed in to change notification settings - Fork 100
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
Feature: option to disable default errorType() #118
Comments
Hey @gbcreation,
Thanks! 🙇
I think this is a very uncommon usecase, and using
…but the good thing is that you can achieve this already using a middleware 😄. // Define a middleware that will intercept the response.
const errorMiddleware = next => (url, opts) => {
return next(url, opts).then(response => {
// Define a "_msg" function that will resolve to an empty string.
// If you want a custom error message you can put anything inside…
response._msg = () => Promise.resolve("")
return response
})
}
// Configure the middleware and the errorType with the "_msg" function here…
const w = wretch("https://httpstat.us").errorType("_msg").middlewares([errorMiddleware])
w.url("/400").get().res().catch(error =>
// Access the response using the `.response` property.
// it will not be consumed since "_msg" did not touch the body.
// Here we consume it manually by calling ".text()"
error.response.text()
).then(txt => console.log(txt)) |
Hi @elbywan Thank you for your suggestion. It works perfectly, but looks a little bit hacky IMO because we have to circumvent the fact that Wretch always processes the error Response. Here, a processing is performed to explicitly tell Wretch to do nothing, while the same result could be achieved if Wretch would indeed do nothing :) I agree calling |
When calling |
The bindings are wrong here, I'll need to update them to accept any string as an argument. In the meantime
Wretch needs to populate the error message actually.
Again I think that the use case is so uncommon that it is not really worth it IMO. And adding a "magic string" argument sounds even more hacky to me 😉 . |
Thank you for this temporary solution.
Well, maybe a new |
@gbcreation Typescript bindings have been updated and released with v1.7.7 📦 ✨. Regarding the original feature request, since it is a very uncommon use case and quite easily done using a simple middleware I'm sorry but I'm not up for implementing that in the lib itself (& by extension increasing the bundle size). |
Thanks for the types update. Closing this issue as your suggested solution based on a middleware works. |
First of all, thank you for making Wretch. That's a very useful Fetch wrapper.
When an error
Response
is returned byfetch()
, Wretch always consumes it through a call totext()
orjson()
depending on the configuration oferrorType()
. However, it can be useful some times to get the unconsumed raw response and let the error catcher handle it.Currently, it is not possible to disable the consuming of the reponse. Maybe a call to
errorType()
without parameter could be used for that. WDYT?The text was updated successfully, but these errors were encountered: