This site counts visits with analytics cookies only if you accept. Read the consent

Skip to content

API ReferenceHTTPError

HTTPError

Custom error class for HTTP responses with typed data, status code, and headers. Includes the isHTTPError type guard.

2 min readUpdated both modes

Import

ts
import { HTTPError, isHTTPError } from "@noverax/courier/client"

@noverax/courier/server exports them as well, for code only a server loads.

HTTPError Class

ts
import { CourierError, type HttpResponse } from "@noverax/courier/client"

declare class HTTPError<TData = unknown> extends CourierError<"HTTP_ERROR"> {
  readonly status: number
  readonly headers: Headers
  readonly data: TData

  constructor(response: HttpResponse<TData>, options?: { cause?: unknown; message?: string })
}

Properties

ts
error.name       // "HTTPError"
error.code       // "HTTP_ERROR"
error.message    // "HTTP {status}" (e.g., "HTTP 404")
error.status     // HTTP status code (e.g., 404)
error.headers    // Response headers (Headers object)
error.data       // Parsed response body (typed as R)
  • status — the answer's HTTP status code, from 100 to 599. An HTTPError always holds an answer: a request that got none is a NetworkError, one whose time ran out a TimeoutError, and new HTTPError() refuses any other status with a RangeError.
  • headers — the answer's Headers object.
  • data — the parsed response body. Typed as TData (defaults to unknown).
  • name — always "HTTPError"; code — always "HTTP_ERROR".
  • message — "HTTP {status}" (e.g., "HTTP 404").

isHTTPError

Type guard function to check if an error is an HTTPError:

ts
import type { HTTPError } from "@noverax/courier/client"

declare function isHTTPError<TData = unknown>(error: unknown): error is HTTPError<TData>

isHTTPError types the error's data

isHTTPError(error) checks what error instanceof HTTPError checks, and takes a type parameter for the error's data: isHTTPError<ErrorResponse>(error) — see Typed Error Data.

Basic Usage

ts
import { isHTTPError } from "@noverax/courier/client"

try {
  const user = await caller.users.getById({ id: "999" })
} catch (error) {
  if (isHTTPError(error)) {
    console.error(`HTTP ${error.status}:`, error.data)
  }
}

Typed Error Data

Pass a type parameter to isHTTPError to type the error's data:

ts
import { isHTTPError } from "@noverax/courier/client"

type ErrorResponse = {
  success: false
  message: string
  code?: string
}

try {
  await caller.users.getById({ id: "999" })
} catch (error) {
  if (isHTTPError<ErrorResponse>(error)) {
    console.error(error.data.message)  // "User not found"
    console.error(error.data.code)     // "USER_NOT_FOUND"
  }
}

Status Code Handling

ts
import { isHTTPError } from "@noverax/courier/client"

try {
  await caller.users.getAll()
} catch (error) {
  if (isHTTPError(error)) {
    switch (error.status) {
      case 400: console.error("Bad request:", error.data); break
      case 401: redirectToLogin(); break
      case 403: showForbiddenMessage(); break
      case 404: showNotFound(); break
      case 429: console.warn("Rate limited, retry later"); break
      default:
        if (error.status >= 500) console.error("Server error:", error.data)
    }
  }
}

Middleware-Based Error Handling

Use middleware to handle errors globally across all requests:

ts
import { defineMiddleware } from "@noverax/courier/middleware"
import { isHTTPError } from "@noverax/courier/client"

const errorMiddleware = defineMiddleware("errors", "wrap", async (ctx, next) => {
  try {
    return await next()
  } catch (error) {
    if (isHTTPError(error) && error.status === 401) {
      // redirect to login, refresh token, etc.
    }
    throw error
  }
})

Network Errors and Timeouts

A request that got no answer — a host that does not resolve, a connection refused or dropped — rejects with a NetworkError, and one whose time ran out with a TimeoutError, never with an HTTPError: see NetworkError, TimeoutError & AbortError. Through the proxy, the server's side of the call answers the browser, so a browser gets an HTTPError 502 with code: "UPSTREAM_UNREACHABLE" or 504 with code: "UPSTREAM_TIMEOUT":

ts
import { isHTTPError, isNetworkError, isTimeoutError } from "@noverax/courier/client"

try {
  await caller.users.getAll()
} catch (error) {
  if (isNetworkError(error)) console.error("No answer:", error.systemCode)
  else if (isTimeoutError(error)) console.error("Too slow:", error.timeout, "ms")
  else if (isHTTPError(error)) console.error("Refused:", error.status)
}