Error Classes
All HTTP error classes extend BaseHttpError
(which extends Error
) with the following properties and methods:
Base HTTP Error Class
The BaseHttpError
class provides a consistent interface for all HTTP errors:
Properties
status
: HTTP status code numberstatusText
: HTTP status messageheaders
: Response headers object
Methods
json()
: Async method to parse response body as JSONtext()
: Async method to parse response body as textblob()
: Async method to parse response body as BlobarrayBuffer()
: Async method to parse response body as ArrayBufferclone()
: Method to clone the error for multiple response body reads
All error classes are exported individually for optimal tree-shaking and inherit these capabilities.
Supported HTTP Status Codes
The library provides comprehensive coverage of HTTP error status codes. For detailed information about each status code, see the MDN HTTP Status Reference.
Client Errors (4xx)
400
- BadRequest402
- PaymentRequired401
- Unauthorized403
- Forbidden404
- NotFound405
- MethodNotAllowed406
- NotAcceptable407
- ProxyAuthenticationRequired408
- RequestTimeout409
- Conflict410
- Gone411
- LengthRequired412
- PreconditionFailed413
- RequestTooLong414
- RequestUriTooLong415
- UnsupportedMediaType416
- RequestedRangeNotSatisfiable417
- ExpectationFailed418
- ImATeapot421
- MisdirectedRequest422
- UnprocessableEntity423
- Locked424
- FailedDependency426
- UpgradeRequired428
- PreconditionRequired429
- TooManyRequests431
- RequestHeaderFieldsTooLarge451
- UnavailableForLegalReasons
Server Errors (5xx)
500
- InternalServer501
- NotImplemented502
- BadGateway503
- ServiceUnavailable504
- GatewayTimeout505
- HttpVersionNotSupported507
- InsufficientStorage511
- NetworkAuthenticationRequired
Network Errors
NetworkError
- For connection issues, timeouts, and other network-related failures
Working with Error Response Bodies
All HTTP error classes provide multiple ways to access the response body:
typescript
import { typedFetch, BadRequestError } from '@pbpeterson/typed-fetch';
const { error } = await typedFetch('/api/users');
if (error && error instanceof BadRequestError) {
// Parse as JSON (common for API errors)
const errorDetails = await error.json();
console.log('Error details:', errorDetails);
// Parse as text
const errorText = await error.text();
console.log('Error message:', errorText);
// Parse as blob (for binary data)
const errorBlob = await error.blob();
// Parse as array buffer (for raw bytes)
const errorBuffer = await error.arrayBuffer();
// Access headers
console.log('Content-Type:', error.headers.get('content-type'));
console.log('Status:', error.status, error.statusText);
}
Error Response Cloning
All HTTP error classes support response cloning, allowing you to read the response body multiple times:
typescript
const { error } = await typedFetch('/api/users');
if (error && 'clone' in error) {
const clonedError = error.clone();
// Both can parse response bodies independently
const originalData = await error.json();
const clonedData = await clonedError.text(); // Different format!
console.log('JSON data:', originalData);
console.log('Text data:', clonedData);
}