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

Handle error codes and failed request in error #1179

Open
1 task done
Visual-Dawg opened this issue Jun 23, 2023 · 2 comments
Open
1 task done

Handle error codes and failed request in error #1179

Visual-Dawg opened this issue Jun 23, 2023 · 2 comments
Labels
docs Requires an update to docs openapi-fetch Relevant to the openapi-fetch library

Comments

@Visual-Dawg
Copy link

Description
Currently in order to handle errors the code usually looks something like this:

const { data, error } = get('url/which/fails/to/fetch')
    .catch(error => ({error, response: new Response()}))

console.log(error) //=> TypeError: Failed to fetch

This is because a failed fetch is not getting caught by the library.
Which can also lead to Typescript not liking the new possible return type introduced by the catch.

Proposal

Instead this would be nicer:

const { data, error } = get('url/which/fails/to/fetch')

console.log(error) //=> TypeError: Failed to fetch

I am not sure if I am simply doing something wrong, but having to do error handling twice can complicate things quite a lot.

The downside to this approach is that the previously unhandled errors cannot be typechecked/
However, they need to be handled in both cases anyway.

Checklist

@Visual-Dawg Visual-Dawg added enhancement New feature or request PRs welcome PRs are welcome to solve this issue! openapi-fetch Relevant to the openapi-fetch library labels Jun 23, 2023
@drwpow
Copy link
Contributor

drwpow commented Jun 24, 2023

Thanks for the suggestion. I’m a little hesitant to deviate from the default fetch behavior too much, which this library implements. If this library throws, then fetch() itself threw, which “shouldn’t happen”™.

But to your point, this library is meant to provide better quality of life improvements over the default fetch spec. That’s probably in the form of clearer documentation here.

@drwpow drwpow added documentation docs Requires an update to docs and removed enhancement New feature or request PRs welcome PRs are welcome to solve this issue! documentation labels Jul 25, 2023
@ThorFjelldalen
Copy link

I second this, after having used some time debugging why the client middleware onResponse() handler would not fire for a redirected response which produced the same TypeError: Failed to fetch.

In this case my response would never be defined because of the error, which would require a catch handler on every request, or with a custom fetch function in the ClientOptions that catches every request.

The latter is what I currently use, but it was not obvious to me, and a more intuitive solution could be to add a middleware option like onError(), to control what happens when errors occur, and the onResponse() is never reached. Might be easily confused with http error codes though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Requires an update to docs openapi-fetch Relevant to the openapi-fetch library
Projects
None yet
Development

No branches or pull requests

3 participants