Skip to content
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 Request] Ability to handle network error easier #62

Closed
MarlBurroW opened this issue Nov 22, 2019 · 3 comments
Closed

[Feature Request] Ability to handle network error easier #62

MarlBurroW opened this issue Nov 22, 2019 · 3 comments

Comments

@MarlBurroW
Copy link

MarlBurroW commented Nov 22, 2019

Currently, I didn't found any explicit way to handle network error with wretch. Catch network error is very useful on mobile app or desktop app (electron) to give a better feedback to the end user, or trigger some offline stuff if a network issue is detected.

In 1.5.5, I manage network error with this kind of code:

// This call will trigger a `net::ERR_NAME_NOT_RESOLVED`, 
// and the error passed to the `catch()` will be a string containing `Failed to fetch`
wretch(`http://unexisting-server`)
  .post()
  .res(res => {})
  .catch(error => {
    if (!error.response) {
      // Here is the network error handling
    } else {
      // Here is the http error handling
    }
  })

But it's not very straightforward as wretch has accustomed us.

Something like this is probably more "wretch":

wretch("http://unexisting-server")
  .get()
  .networkError(error => { /* ... */ })
  .res(response => /* ... */)
  .catch(error => { /* uncaught errors */ })

I know fetch doesn't give any information about the network error (there is no way to know that the error see above is a net::ERR_NAME_NOT_RESOLVED, but it's good to know at less that is a network error, and have a nice way to handle it.

Possibly related to this: #56

@elbywan
Copy link
Owner

elbywan commented Nov 27, 2019

Currently, I didn't found any explicit way to handle network error with wretch. Catch network error is very useful on mobile app or desktop app (electron) to give a better feedback to the end user, or trigger some offline stuff if a network issue is detected.

NetworkErrors are very browser/platform dependent.

But what I can add is a .fetchError that will only include errors encountered when calling .fetch, which would be roughly equivalent most of the time.

@MarlBurroW
Copy link
Author

MarlBurroW commented Nov 27, 2019

I know fetch has different low level implementations on each browser, but I guess it works the same on all browsers at the top level of fetch javascript api: Basically, it rejects the promise if it was not able to retrieve a response from the server, maybe with different messages, but it does not matter. The main goal of my request is "have a handler method on the wretch api to know when a true fetch error occurs, and then be able to do different things, without manually check the presence of the response property in the wretch reject handler.

So yes, a .fetchError() handler method sounds good to me !

Thanks for your work

elbywan added a commit that referenced this issue Nov 27, 2019
@elbywan
Copy link
Owner

elbywan commented Nov 27, 2019

📦 Version 1.6.0 has just been released with the feature: https:/elbywan/wretch#fetcherrorcb-error-networkerror-originalrequest-wretcher--any

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants