-
-
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: recognize a Wretch
error when catching request failure
#79
Comments
Hi @SamyCookie, thanks for the suggestion!
Well, I think that it would be a good idea on paper! Unfortunately, I'm a bit worried by the fact that it will add more code and bump the package size. And my goal is to go through the codebase soon and try to remove as much bytes as I can. In addition, there is actually an easy way to check for an error thrown by wretch already (even though it is not ideal): if(err.status && err.response) {
// an error thrown by wretch has this two properties
} |
Ok, but I think that this solution is less clean and usable, and maybe should be documented if kept as is. A smaller proposition is possible: --- a/src/resolver.ts
+++ b/src/resolver.ts
@@ -34,6 +34,7 @@ export type ResponseChain = {
class WretchErrorWrapper {
constructor(public error: any) {}
}
+export class WretchError extends Error {}
export const resolver = (wretcher: Wretcher) => {
const {
@@ -71,7 +72,7 @@ export const resolver = (wretcher: Wretcher) => {
if (!response.ok) {
return response[conf.errorType || "text"]().then(msg => {
// Enhances the error object
- const err = new Error(msg)
+ const err = new WretchError(msg)
err[conf.errorType || "text"] = msg
err["status"] = response.status
err["response"] = response And I don't think it adds too much bytes, especially if minified. |
It is already documented here:
I am a bit busy with other projects these days, but I'll think about it. In any case the library is ~3Kb minzipped as of today, which is already too much. |
Oh, how I did to not see this ? Sorry !
Thanks for your time ! |
Should be fixed with the v2. |
Thanks you for this code integration, but I do not see any way how I can reach the |
The type should be imported from the root: import type { WretchError } from "wretch" The class can be imported from the resolver: import { WretchError } from "wretch/resolver" |
Maybe I have missed something, but the In the dist bundle |
No, the second one is the javascript Class and is exported from
Ah, I was not aware that you were using the bundles through script tags. <script type="module">
import wretch from 'https://cdn.skypack.dev/wretch/dist/bundle/wretch.all.min.mjs'
console.log(wretch.WretchError) // class s {}
wretch("http://httpstat.us/400").get().badRequest(error => {
console.log(error instanceof Error) // true
console.log(error instanceof wretch.WretchError) //true
}).res()
</script> |
Would it be possible for the import wretch from 'wretch';
try {
// throws
} catch(e) {
if(e instanceof wretch.WretchError) {
console.log(e.response); // TS: 'response' does not exist
}
} Or perhaps there's some declaration merging going on that I'm missing? I'm on 2.0.3 by the way |
Hey @dxptqhtlutehvlyxcmtg 👋,
I just released 2.0.4 📦 which contains a rework of the |
The main goal of the current ticket I wrote was solve with the 2.0.3 version for me, thanks for the work ! |
Type narrowing works great now. Thank you very much |
Hi !
Today when having a request error, an
error
object can be reached.cf.
wretch/src/resolver.ts
Line 74 in c624b20
But if the request is embedded inside a ton of wrappers, when the error is catched, it is complicated to know if the error come from a wretch error or anything else.
I just propose to create a custom
Wretch
error and make it exportable inside API, eg.and in user code, we can do this:
What do you think ?
The text was updated successfully, but these errors were encountered: