Skip to main content
ofetch automatically throws errors for failed HTTP requests (status codes 400-599), making error handling straightforward and consistent.

Automatic Error Throwing

Unlike native fetch, ofetch automatically throws errors when the response status is 4xx or 5xx:
This eliminates the need to manually check response.ok after every request.

FetchError Type

When a request fails, ofetch throws a FetchError with detailed information:

FetchError Properties

string
Formatted error message in the format: [METHOD] "url": status statusText errorMessageExample: [GET] "/api/user": 404 Not Found
number | undefined
HTTP status code of the response (e.g., 404, 500)
number | undefined
Alias for status
string | undefined
HTTP status text (e.g., “Not Found”, “Internal Server Error”)
string | undefined
Alias for statusText
any
The parsed response body, same as response._data. This is particularly useful for API errors that return error details in the response body.
FetchRequest
The request URL string or Request object that was used
FetchResponse | undefined
The Response object with the parsed data available as _data property
FetchOptions
The resolved request options that were used

Accessing Error Data

The error.data property contains the parsed response body, which is useful for API errors:
The error.data property is the same as error.response._data, providing convenient access to the parsed response body.

Ignoring Response Errors

You can disable automatic error throwing using the ignoreResponseError option:
boolean
default:"false"
When set to true, ofetch will not throw errors for 4xx and 5xx responses. Instead, it returns the response normally.
This is useful when you need to handle different status codes explicitly:

Error Status Codes

ofetch throws errors for all status codes between 400 and 599 (inclusive):
  • 4xx Client Errors: 400-499 (Bad Request, Unauthorized, Forbidden, Not Found, etc.)
  • 5xx Server Errors: 500-599 (Internal Server Error, Bad Gateway, Service Unavailable, etc.)

Error Cause

When the underlying fetch operation fails (network error, timeout, etc.), the original error is preserved in the cause property:
ofetch polyfills the cause property for runtimes that don’t support it natively.

TypeScript Error Handling

You can type the error data for better TypeScript support: