Overview
ofetch throwsFetchError instances when requests fail. These errors provide detailed information about what went wrong, including the request, response, and status information.
FetchError Class
The main error class thrown by ofetch.string
required
Error message describing what went wrong
unknown
The underlying error that caused this error (error chaining)
IFetchError interface (see below).
Example:
IFetchError Interface
Interface defining the structure of fetch errors.Properties
string
Always
"FetchError"string
Error message in format:
[METHOD] "url": status statusText errorMessageunknown
The underlying error that caused this error, if available
FetchRequest | undefined
The original request URL or Request object
FetchOptions | undefined
The fetch options used for the request. See FetchOptions.
FetchResponse<T> | undefined
The response object if available. See FetchResponse.
T | undefined
Parsed response data from
response._datanumber | undefined
HTTP status code from the response
string | undefined
HTTP status text from the response
number | undefined
Alias for
statusstring | undefined
Alias for
statusTextError Message Format
FetchError messages follow this format:Error Handling
Basic Error Handling
Handling Specific Status Codes
Suppressing Errors
UseignoreResponseError to prevent errors from being thrown:
Using Hooks for Error Handling
Handle errors globally using hooks:Network Errors vs HTTP Errors
Network Errors (triggered inonRequestError):
- Network connectivity issues
- Timeouts
- Aborted requests
- CORS errors
onResponseError):
- 4xx client errors (400, 401, 404, etc.)
- 5xx server errors (500, 502, 503, etc.)
createFetchError
Internal function used to create FetchError instances from a FetchContext.Type Parameters
any
default:"any"
The type of the response data and error data
Related
- FetchOptions - Request options including
ignoreResponseError - FetchHooks -
onRequestErrorandonResponseErrorhooks - FetchContext - Context passed to error hooks
- FetchResponse - Response interface