Skip to content

Error Classes

All HTTP error classes extend BaseHttpError (which extends Error).

BaseHttpError

Abstract base class for all HTTP errors.

Properties

PropertyTypeDescription
statusliteral (e.g. 404)HTTP status code
statusTextliteral (e.g. "Not Found")HTTP status text
headersHeadersResponse headers
namestringError class name (e.g. "NotFoundError")

Methods

MethodReturn TypeDescription
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 classClone 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)

ClassStatusStatus Text
BadRequestError400Bad Request
UnauthorizedError401Unauthorized
PaymentRequiredError402Payment Required
ForbiddenError403Forbidden
NotFoundError404Not Found
MethodNotAllowedError405Method Not Allowed
NotAcceptableError406Not Acceptable
ProxyAuthenticationRequiredError407Proxy Authentication Required
RequestTimeoutError408Request Timeout
ConflictError409Conflict
GoneError410Gone
LengthRequiredError411Length Required
PreconditionFailedError412Precondition Failed
RequestTooLongError413Payload Too Large
RequestUriTooLongError414URI Too Long
UnsupportedMediaTypeError415Unsupported Media Type
RequestedRangeNotSatisfiableError416Range Not Satisfiable
ExpectationFailedError417Expectation Failed
ImATeapotError418I'm a teapot
MisdirectedRequestError421Misdirected Request
UnprocessableEntityError422Unprocessable Entity
LockedError423Locked
FailedDependencyError424Failed Dependency
TooEarlyError425Too Early
UpgradeRequiredError426Upgrade Required
PreconditionRequiredError428Precondition Required
TooManyRequestsError429Too Many Requests
RequestHeaderFieldsTooLargeError431Request Header Fields Too Large
UnavailableForLegalReasonsError451Unavailable For Legal Reasons

Server Errors (5xx)

ClassStatusStatus Text
InternalServerError500Internal Server Error
NotImplementedError501Not Implemented
BadGatewayError502Bad Gateway
ServiceUnavailableError503Service Unavailable
GatewayTimeoutError504Gateway Timeout
HttpVersionNotSupportedError505HTTP Version Not Supported
VariantAlsoNegotiatesError506Variant Also Negotiates
InsufficientStorageError507Insufficient Storage
LoopDetectedError508Loop Detected
NotExtendedError510Not Extended
NetworkAuthenticationRequiredError511Network 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();
}

Released under the MIT License.