fix(useFetch): Add option to use stock FetchAPI error-like status codes behavior (non-breaking alternative) #3126
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Before submitting the PR, please make sure you do the following
fixes #123
).Description
Solves same problem as #3092 , but provides a non-breaking solution, since users do expect the non-standard behavior, as it's more intuitive. But there are cases, especially when integrating with APIs, where stock FetchAPI behavior is necessary.
This adds an option
throwOnErrCodes
that is true by default, but can be disabled; in which case, http error codes will be accepted as successful responses, and populate thedata
field instead of theerror
field.This also has the benefit of allowing someone to disable
throwOnErrCodes
and implement their own custom error detection inafterFetch
hooks, since throwing inside one results in falling back to the useFetcherror
being populated.Additional context
solves #2452
solves #526
solves #553
supersedes #572
related #2711
🤖 Generated by Copilot at 2ae0871
This pull request enhances the
useFetch
function with a new optionthrowOnErrCodes
that allows users to opt out of throwing errors on non-200 status codes. It also adds tests and documentation for this new feature.🤖 Generated by Copilot at 2ae0871
throwOnErrCodes
option touseFetch
to disable error throwing for non-200 status codes (link, link, link, link)useFetch
documentation to explain how to use the new option and handle error codes like the native FetchAPI (packages/core/useFetch/index.md
, link)useFetch
with and without the new option, and with a custom error thrown in theafterFetch
hook (packages/core/useFetch/index.test.ts
, link, link)