Skip to content

HttpError

HttpError is the typed error class returned in the error slot of every HttpResult. It’s also what gets thrown when you opt in with { throw: true }. Predicates are getters — call them without parentheses.

For the narrative guide on common error-handling patterns, see Error handling.

Import

import { HttpError } from "@mongez/http";

Class signature

class HttpError extends Error {
readonly status: number | null;
readonly body: unknown;
readonly response: Response | null;
readonly isAborted: boolean;
readonly isTimeout: boolean;
readonly isNetwork: boolean;
readonly headers: Record<string, string> | null;
readonly request: OutgoingRequest | null;
// Status predicates (getters — no `()`):
get isClientError(): boolean; // 4xx
get isServerError(): boolean; // 5xx
get isUnauthorized(): boolean; // 401
get isForbidden(): boolean; // 403
get isNotFound(): boolean; // 404
get isValidationError(): boolean; // 422
get isRateLimited(): boolean; // 429
toJSON(): Record<string, unknown>;
}

Properties

NameTypeDescription
statusnumber | nullHTTP status code, or null for network/abort/timeout errors.
bodyunknownParsed response body — JSON when Content-Type: application/json, else raw text.
responseResponse | nullThe raw Response object, if one was received.
headersRecord<string, string> | nullResponse headers as a plain object. null when no response was received.
requestOutgoingRequest | nullThe request that triggered this error. Warning: may contain sensitive headers (Authorization, Cookie). Do not log without redacting.
isAbortedbooleanThe request was cancelled via AbortController or .cancel().
isTimeoutbooleanThe request hit the configured timeout.
isNetworkbooleanDNS, CORS, or other network-level failure (no HTTP response received).

Status predicates

All are getters — write error.isNotFound, never error.isNotFound().

PredicateTrue when status is
isClientError400 ≤ status < 500
isServerError500 ≤ status < 600
isUnauthorized401
isForbidden403
isNotFound404
isValidationError422 (typical for form errors with field details in body)
isRateLimited429

For statuses without a dedicated predicate (409 Conflict, 503 Service Unavailable, etc.), compare error.status directly.

Methods

toJSON()

toJSON(): Record<string, unknown>

JSON-safe serialisation. Intentionally omits request to avoid leaking Authorization / Cookie headers into logs. Includes status, body, headers, and the three boolean flags.

log.error({ err: error.toJSON() }); // safe to ship to logs

Recognising specific failure modes

const { data, error } = await http.get<User>(`/users/${id}`);
if (error) {
// Cancellation — user navigated away mid-request
if (error.isAborted) return;
// Network problem — no HTTP response at all
if (error.isNetwork) return showOffline();
// Timeout
if (error.isTimeout) return showSlowConnection();
// Status-based branches
if (error.isNotFound) return null;
if (error.isUnauthorized) return router.go("/login");
if (error.isForbidden) return showPermissionDenied();
if (error.isValidationError) return showErrors(error.body);
if (error.isRateLimited) return scheduleRetry(error.headers?.["retry-after"]);
if (error.isServerError) return showServerDown();
// Catch-all
return toast.error(error.message);
}

See also

  • Http — the client class that returns HttpError in results
  • HttpResult — the discriminated union containing error
  • Guide: Error handling — patterns, refresh-token flows, validation rendering