-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
More easily checkable errors #7
Comments
I would be ok with that, though I didn't expect fetch to produce any error other than FetchError, do you really think that conditional instanceof is necessary? |
it'd probably go at the end of a big chain of stuff, e.g:- https://github.com/Financial-Times/next-grumman/blob/master/server/controllers/capi.js#L127 if (err instanceof fetchres.BadServerResponseError || err instanceof FetchError) { |
Looks like network error is main pain here, per spec though, network error is a response with status 0. I wonder if we should change behavior according to spec instead: https://fetch.spec.whatwg.org/#concept-network-error ie. resolve instead of reject on network error (and custom timeout) |
oh that would actually be OK for us assuming |
yeah update: looking at the wording, as long as response is not in range 200 ~ 299, it should be false |
This is incorrect, see follow up comment I have a 2.0 branch ready for testing, see if covers your need https://github.com/bitinn/node-fetch/tree/2.0 see test for timeout https://github.com/bitinn/node-fetch/blob/2.0/test/test.js#L317 |
Though I am not exactly sure if TCP timeout would be a problem (node.js net doesn't have a default timeout, so I guess it's up to linux server to decide). |
Sorry, after reading the spec a bit more though, I realize what they mean by Network Error is a Response: Response here is just an internal object, and Network Error is a Response object with But per spec, we don't actually resolve Network Error, we should check for type This means your problem is still valid, one cannot easily distinguish a custom timeout/maximum redirect errors (currently you have to check error message, which sucks), unless we establish some sorts of convention on how Error will be formatted. |
Yes, checking the error message sucks.
The important keywords ( Could we just add a code property to the error object, similar to https://github.com/request/request? |
@pekeler the idea of Fetch is user shouldn't know why it failed (due to security), user should just know that it failed. So our custom error message is really for human troubleshooting, not machine processing. I am not against making it machine readable, but it will most definitely be against Fetch Spec. If you have a better suggestion, please move it to #8 :) |
I just read whatwg/fetch#25 and the security argument makes no sense to me because someone with bad intentions could just use curl or some other networking tool to find out more details. I'd like to encourage you to focus less on 100% compatibility with an unfinished standard, and more on providing a viable alternative to https://github.com/request/request :) I like the proposed approach of adding a code property to the error because it is such a limited departure from the standard. |
@pekeler checkout https://github.com/sindresorhus/got if a replacement for |
Thanks for the link @bitinn. I didn't know about got. Unfortunately, looks like got doesn't allow me to control redirects which is important to me. |
One of the listed Features of node-fetch is
Options also include "node.js extensions". Can we expose the error perhaps as a node.js extension, and document the difference from the client, as mentioned in the 2nd item under Features? |
By extension we mean it provides parameters beyond the default Fetch Spec (mostly for server-side convenience). Could you open an issue to clarify when "expose the errors" mean. |
I believe this very issue shows when @pekeler and I mean: a Use case: we use the error codes above in node-influx to decide whether to resubmit a failed request or not. |
We had a long discussion on this in the PR and the result is ultimately we want custom error code. Regards,
|
The PR lists some error codes, but you changed some of them and I see that system error codes are also passed through, though not under the Where can we find a complete list of error codes, along with how to distinguish between custom and system error codes/types? Perhaps error handling merits more of a documentation section? |
I should preface this by saying: Fetch Spec isn't designed to be transparent about errors, anything we do will be an extension. And I try to keep it to a minimum. There are currently no document on a list of possible errors, but:
In many cases whether or not a request should be re-initiated is entirely based on use-case. So I didn't find providing a detailed README section to be necessary (usually developers run into errors, and decide how to handle them). And to be honest, the more options you add to a module, the more possible errors it yields. So if you do want to handle errors before they even appear. The safest bet is to go as low-level as possible: |
I have updated an error handling doc for this purpose in v1.6.3: https://github.com/bitinn/node-fetch/blob/master/ERROR-HANDLING.md |
Would you be open to a pull request that creates more specific errors than just
Error
. If I remember correctly the whatwg-fetch spec leaves errors up to implementations to decide how to handle so I think we should be free to do what we want…It would be really helpful if there was something like a
FetchError
for all errors (although a different type per error would be OK too) so we can do things likeThe text was updated successfully, but these errors were encountered: