Error Classes
All HTTP error classes extend BaseHttpError (which extends Error).
BaseHttpError
Abstract base class for all HTTP errors.
Properties
| Property | Type | Description |
|---|---|---|
status | literal (e.g. 404) | HTTP status code |
statusText | literal (e.g. "Not Found") | HTTP status text |
headers | Headers | Response headers |
name | string | Error class name (e.g. "NotFoundError") |
Methods
| Method | Return Type | Description |
|---|---|---|
json<T = unknown>() | Promise<T> | Parse response body as JSON with optional type parameter |
text() | Promise<string> | Parse response body as text |
blob() | Promise<Blob> | Parse response body as Blob |
arrayBuffer() | Promise<ArrayBuffer> | Parse response body as ArrayBuffer |
clone() | Same class | Clone the error for multiple body reads |
Static Properties
All error classes also have static status and statusText:
typescript
import { NotFoundError } from '@pbpeterson/typed-fetch';
NotFoundError.status; // 404 (literal type)
NotFoundError.statusText; // "Not Found" (literal type)NetworkError
Represents a network-level failure (DNS, connection refused, timeout). Unlike HTTP errors, NetworkError has no status, headers, or response body — the request never reached the server.
typescript
import { NetworkError } from '@pbpeterson/typed-fetch';
const error = new NetworkError('Connection refused');
error.name; // "NetworkError"
error.message; // "Connection refused"Client Errors (4xx)
| Class | Status | Status Text |
|---|---|---|
BadRequestError | 400 | Bad Request |
UnauthorizedError | 401 | Unauthorized |
PaymentRequiredError | 402 | Payment Required |
ForbiddenError | 403 | Forbidden |
NotFoundError | 404 | Not Found |
MethodNotAllowedError | 405 | Method Not Allowed |
NotAcceptableError | 406 | Not Acceptable |
ProxyAuthenticationRequiredError | 407 | Proxy Authentication Required |
RequestTimeoutError | 408 | Request Timeout |
ConflictError | 409 | Conflict |
GoneError | 410 | Gone |
LengthRequiredError | 411 | Length Required |
PreconditionFailedError | 412 | Precondition Failed |
RequestTooLongError | 413 | Payload Too Large |
RequestUriTooLongError | 414 | URI Too Long |
UnsupportedMediaTypeError | 415 | Unsupported Media Type |
RequestedRangeNotSatisfiableError | 416 | Range Not Satisfiable |
ExpectationFailedError | 417 | Expectation Failed |
ImATeapotError | 418 | I'm a teapot |
MisdirectedRequestError | 421 | Misdirected Request |
UnprocessableEntityError | 422 | Unprocessable Entity |
LockedError | 423 | Locked |
FailedDependencyError | 424 | Failed Dependency |
TooEarlyError | 425 | Too Early |
UpgradeRequiredError | 426 | Upgrade Required |
PreconditionRequiredError | 428 | Precondition Required |
TooManyRequestsError | 429 | Too Many Requests |
RequestHeaderFieldsTooLargeError | 431 | Request Header Fields Too Large |
UnavailableForLegalReasonsError | 451 | Unavailable For Legal Reasons |
Server Errors (5xx)
| Class | Status | Status Text |
|---|---|---|
InternalServerError | 500 | Internal Server Error |
NotImplementedError | 501 | Not Implemented |
BadGatewayError | 502 | Bad Gateway |
ServiceUnavailableError | 503 | Service Unavailable |
GatewayTimeoutError | 504 | Gateway Timeout |
HttpVersionNotSupportedError | 505 | HTTP Version Not Supported |
VariantAlsoNegotiatesError | 506 | Variant Also Negotiates |
InsufficientStorageError | 507 | Insufficient Storage |
LoopDetectedError | 508 | Loop Detected |
NotExtendedError | 510 | Not Extended |
NetworkAuthenticationRequiredError | 511 | Network Authentication Required |
Working with Error Bodies
typescript
import { typedFetch, BadRequestError, isHttpError } from '@pbpeterson/typed-fetch';
const { error } = await typedFetch('/api/users');
if (error && isHttpError(error)) {
// Typed JSON parsing
const details = await error.json<{ message: string; code: string }>();
// Other formats (clone first for multiple reads)
const text = await error.clone().text();
const blob = await error.clone().blob();
const buffer = await error.clone().arrayBuffer();
// Response metadata
console.log(error.status); // 400 (literal)
console.log(error.statusText); // "Bad Request" (literal)
console.log(error.name); // "BadRequestError"
console.log(error.headers.get('content-type')); // response header
}Cloning Errors
Clone an error to read the body in multiple formats:
typescript
if (isHttpError(error)) {
const cloned = error.clone();
const json = await error.json();
const text = await cloned.text();
}