Skip to main content

Overview

ofetch throws FetchError 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.
Constructor Parameters:
string
required
Error message describing what went wrong
unknown
The underlying error that caused this error (error chaining)
Properties: Inherits all properties from 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 errorMessage
unknown
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._data
number | undefined
HTTP status code from the response
string | undefined
HTTP status text from the response
number | undefined
Alias for status
string | undefined
Alias for statusText

Error Message Format

FetchError messages follow this format:
Examples:

Error Handling

Basic Error Handling

Handling Specific Status Codes

Suppressing Errors

Use ignoreResponseError to prevent errors from being thrown:

Using Hooks for Error Handling

Handle errors globally using hooks:

Network Errors vs HTTP Errors

Network Errors (triggered in onRequestError):
  • Network connectivity issues
  • Timeouts
  • Aborted requests
  • CORS errors
HTTP Errors (triggered in 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.
This function is used internally by ofetch and typically not needed in application code.

Type Parameters

any
default:"any"
The type of the response data and error data