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

Add an option to allow http errors #207

Closed
pi0 opened this issue Feb 4, 2023 · 2 comments · Fixed by #221
Closed

Add an option to allow http errors #207

pi0 opened this issue Feb 4, 2023 · 2 comments · Fixed by #221
Labels
enhancement New feature or request

Comments

@pi0
Copy link
Member

pi0 commented Feb 4, 2023

ofetch by default throws an error if the response status code is any of http standard error codes (such as 50x). However there are situations that users prefer to always get it as a response.

Currently, we provide a solution in docs (https://github.com/unjs/ofetch#%EF%B8%8F-handling-errors), to use .error(err => err.data). However in wrapped contexts when user is not directly using $fetch context, it is impossible. (nuxt/nuxt#18713).

A new option like ignoreResponseError would make it possible to opt-out from the feature.

@DonRedwoood
Copy link

DonRedwoood commented Mar 18, 2023

This looks like a step in the right direction, but it still doesn't feel friendly to communicate with real REST APIs. I may be missing something obvious (new to Nuxt/$fetch/ofetch), but even with the PR, I don't see a way to get the HTTP status code unless it's explicitly returned by the API as a JSON field, or unless I hook into the interceptors (onResponse*). When building a web app that relies exclusively on data returned from a standard REST API on the backend, this makes parsing every request very tedious, since I can't just look at a status code of 200 to parse a correct response and everything else as an incorrect response. I have to check for the existence of fields or bloat the code with interceptors. What am I missing?

@sannajammeh
Copy link

sannajammeh commented May 30, 2023

This looks like a step in the right direction, but it still doesn't feel friendly to communicate with real REST APIs. I may be missing something obvious (new to Nuxt/$fetch/ofetch), but even with the PR, I don't see a way to get the HTTP status code unless it's explicitly returned by the API as a JSON field, or unless I hook into the interceptors (onResponse*). When building a web app that relies exclusively on data returned from a standard REST API on the backend, this makes parsing every request very tedious, since I can't just look at a status code of 200 to parse a correct response and everything else as an incorrect response. I have to check for the existence of fields or bloat the code with interceptors. What am I missing?

If you need the status codes, use native fetch or ofetch.native. If you need them for errors, use FetchError

For non-errors:
https://github.com/unjs/ofetch#native-fetch

For errors:

import { FetchError, ofetch } from "ofetch";
try {
  await ofetch(...)
} catch(e) {
  if(e instanceof FetchError) {
     console.log(e.statusCode);
  }
}

@pi0 pi0 closed this as completed in #221 Jun 6, 2023
pi0 added a commit that referenced this issue Jun 6, 2023
* feat: support ignoreResponseError option #207

* lint

* add ignore check to the wrapper

* update readme

---------

Co-authored-by: Pooya Parsa <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants