Skip to content

ExamplesError Handling

Error Handling

HTTPError class, isHTTPError type guard, and global error callbacks.

both modes

HTTPError — Catch & Inspect

HTTPError provides status, data, and headers. Select an error code and trigger it.

ts
import { HTTPError } from "@noverax/courier"
import { caller } from "@/lib/caller"

try {
  await caller.misc.error({ code: 404 })
} catch (error) {
  if (error instanceof HTTPError) {
    console.log(error.status)  // 404
    console.log(error.data)    // { success: false, message: "..." }
    console.log(error.headers) // Headers object
  }
}
caller.misc.error({ code })SANDBOX
idle
// run the request to see the response
Error handling docs

isHTTPError — Type Guard

Use isHTTPError() to narrow unknown errors. TypeScript infers status, data, and headers.

ts
import { isHTTPError } from "@noverax/courier"
import { caller } from "@/lib/caller"

try {
  await caller.misc.error({ code: 403 })
} catch (error) {
  if (isHTTPError(error)) {
    // TypeScript knows: error.status, error.data, error.headers
    handleHttpError(error)
  } else {
    // Network error, timeout, etc.
    handleUnknownError(error)
  }
}
isHTTPError(error)SANDBOX
idle
// run the request to see the response

Global onError / onSuccess

Configure global callbacks for all requests in createProxyCaller defaults.

ts
import { createProxyCaller } from "@noverax/courier"

const caller = createProxyCaller<typeof api>("api", {
  // Called for every failed request
  onError: (error) => {
    toast.error(`Request failed: ${error.status}`)

    if (error.status === 401) {
      router.push("/login")
    }
  },

  // Called for every successful request
  onSuccess: (data) => {
    console.log("Success:", data)
  },
})

Global callbacks fire for every request. Useful for toast notifications, auth redirects, and logging.