Go-like syntax to handle errors
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 5.4k
- Forks
- 195
- PR merge metrics
- No merged PRs in 30d
Description
Describe the feature
I suggest to add throw: boolean option so we can return the HTTP error instead of throwing it. This way users can get full type safety after implementation of Typed API definition.
Example:
const { data, error } = await $fetch('/api/v1', { throw: false })
or not throw by default:
const $api = $fetch.create<unknown, ApiDefinition>({
baseURL: 'http://httpstat.us',
throw: false
})
const { data, error } = await $api('/500')
error // -> error is typed here based on the ApiDefinition
data - 2xx response if OK, otherwise undefined
error - 5xx, 4xx response if not OK, otherwise undefined
Alternatives
Return array:
const [data, error] = $fetch('/api/v1', { throw: false })
Additional information
- Would you be willing to help implement this feature?
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the typed API definition work in issue #364 and the proposed $fetch and $fetch.create examples here. Define how the throw: false option affects successful and failed responses, including the typed data and error values, and resolve whether the option is per-request or configured by default.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- api
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100