Skip to main content

Class: ApiError

Defined in: use-api.ts:31

The error useApi reports when the server responds with a non-2xx status.

It keeps the status and the raw response, so callers can branch on the status code or read an error body (for example await error.response.json()). Network failures and JSON parse errors are reported as the original Error.

Example​

const { error } = useApi<User>("/api/user");

if (error instanceof ApiError && error.status === 401) {
redirectToLogin();
}

Extends​

  • Error

Constructors​

Constructor​

new ApiError(response): ApiError

Defined in: use-api.ts:37

Parameters​

response​

Response

Returns​

ApiError

Overrides​

Error.constructor

Properties​

response​

readonly response: Response

Defined in: use-api.ts:35

The response that failed. Its body has not been read.


status​

readonly status: number

Defined in: use-api.ts:33

The HTTP status code of the response.