Skip to content

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 number
  • statusText: HTTP status message
  • headers: Response headers object

Methods

  • json(): Async method to parse response body as JSON
  • text(): Async method to parse response body as text
  • blob(): Async method to parse response body as Blob
  • arrayBuffer(): Async method to parse response body as ArrayBuffer
  • clone(): 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 - BadRequest
  • 402 - PaymentRequired
  • 401 - Unauthorized
  • 403 - Forbidden
  • 404 - NotFound
  • 405 - MethodNotAllowed
  • 406 - NotAcceptable
  • 407 - ProxyAuthenticationRequired
  • 408 - RequestTimeout
  • 409 - Conflict
  • 410 - Gone
  • 411 - LengthRequired
  • 412 - PreconditionFailed
  • 413 - RequestTooLong
  • 414 - RequestUriTooLong
  • 415 - UnsupportedMediaType
  • 416 - RequestedRangeNotSatisfiable
  • 417 - ExpectationFailed
  • 418 - ImATeapot
  • 421 - MisdirectedRequest
  • 422 - UnprocessableEntity
  • 423 - Locked
  • 424 - FailedDependency
  • 426 - UpgradeRequired
  • 428 - PreconditionRequired
  • 429 - TooManyRequests
  • 431 - RequestHeaderFieldsTooLarge
  • 451 - UnavailableForLegalReasons

Server Errors (5xx)

  • 500 - InternalServer
  • 501 - NotImplemented
  • 502 - BadGateway
  • 503 - ServiceUnavailable
  • 504 - GatewayTimeout
  • 505 - HttpVersionNotSupported
  • 507 - InsufficientStorage
  • 511 - 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);
}

Released under the MIT License.