# NetworkError, TimeoutError & AbortError

The errors a call rejects with when no answer came, its time ran out, or it was cancelled — with the platform's code, the scope of the time, the reason given, and the guards that tell them apart.

A call that got no successful answer rejects with one of three errors, each telling what happened: an `HTTPError` holds an answer, a `NetworkError` says none came, a `TimeoutError` that the time ran out. None of them is ever the other dressed up: there is no `HTTPError` with status 0. A call that was cancelled rejects with an `AbortError`, which is no failure.

## Import

```ts side="both"
import {
  AbortError,
  isAbortError,
  isNetworkError,
  isRequestFailure,
  isTimeoutError,
  NetworkError,
  type RequestFailure,
  TimeoutError,
} from "@noverax/courier/client"
```

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

## NetworkError

```ts side="both"
import { CourierError } from "@noverax/courier/client"

declare class NetworkError extends CourierError<"NETWORK_ERROR"> {
  readonly systemCode: string | undefined // "ECONNREFUSED", "ENOTFOUND", "UND_ERR_SOCKET", "CERT_HAS_EXPIRED", …
}
```

Thrown when a request got no answer: its host could not be resolved or reached, the connection was refused, reset or closed before the answer was whole, or the server's certificate was refused. `cause` is what the platform rejected with — on Node, `TypeError: fetch failed` with the system's error under it, which Courier's log prints whole on the server.

A page's call fails with it too when a part of Courier the call needs did not arrive. Courier loads some parts with the first call that needs them — the encoding of a record that holds a file, an endpoint's retry policy — and where the browser could not fetch one, because a deployment took its file away, say, the call reached no server: `cause` is what the browser said, and the next call asks for the part again. A call a server or an app makes straight to its API fails with what the platform said instead: there the code is on the device, so a part that does not load is a mistake in how it was deployed.

- `systemCode` — the platform's code, where it gave one. A browser gives none: its `fetch` says only that it failed.
- `message` — `"Network request failed (ECONNREFUSED)"`, or `"Network request failed"` without a code. It names no host and no address, so it is safe to show on a page.

## TimeoutError

```ts side="both"
import { CourierError } from "@noverax/courier/client"

declare class TimeoutError extends CourierError<"TIMEOUT"> {
  readonly scope: "attempt" | "deadline"
  readonly timeout: number | undefined // ms
}
```

Thrown when the time ran out before the answer was whole — while waiting for it, or while its body was read.

- `scope` — `"attempt"` for the request's own `timeout`, which each attempt has to itself; `"deadline"` for a retry policy's `deadline`, which its attempts and the waits between them share.
- `timeout` — how long it was, in ms, where it is known.
- `message` — `"Request timed out after 5000 ms"`, or `"Call deadline exceeded after 10000 ms"`.

An attempt that ran out of time may have reached the server, which may still be working on it.

## AbortError

```ts side="both"
import { CourierError } from "@noverax/courier/client"

declare class AbortError extends CourierError<"ABORTED" | "SUPERSEDED"> {}
```

Thrown when a call was cancelled before it was answered. Nothing failed: `isRequestFailure()` is false for it, a caller's `onError` is not told of it, and no retry repeats it.

- `code` — `"ABORTED"` when the call's signal aborted: before its request left, while it was out, while its answer was read, or while a middleware held it between attempts. `"SUPERSEDED"` when a newer call of an endpoint that cancels the previous one took its place — see [Deduplication & Abort](/docs/advanced/abort).
- `cause` — the reason the signal was given: yours, or the platform's `DOMException` named `AbortError` when you gave none. A superseded call has none.
- `message` — `"The call was cancelled"`, or `"A newer call of the endpoint superseded this one"`.
- `name` — `"AbortError"`, as the platform's own is named.

A retry policy's `deadline` that runs out is a `TimeoutError` of scope `"deadline"`, not an `AbortError`: the call did not finish in time.

## Telling Them Apart

```ts side="both"
import { isAbortError, isHTTPError, isNetworkError, isTimeoutError } from "@noverax/courier/client"

try {
  await caller.users.getAll()
} catch (error) {
  if (isAbortError(error)) console.debug("cancelled:", error.code) // nobody waits for the answer
  else if (isTimeoutError(error)) showSlow(error.timeout)
  else if (isNetworkError(error)) showOffline(error.systemCode)
  else if (isHTTPError(error)) showRefused(error.status)
  else throw error
}
```

`isRequestFailure(error)` is true for all three, and `RequestFailure` names them together: it is what a caller's `onError` receives — `createCaller(api, { onError })`, `createProxyCaller(routes, { onError })`.

## Calling Without Throwing

Every HTTP endpoint's method has `.safe()`, which calls it as the method does and resolves to its data, or to why it failed, rather than rejecting:

```ts side="both"
import { isHTTPError } from "@noverax/courier/client"

async function showUser(id: string) {
  const { data, error } = await caller.users.getById.safe({ id })
  if (error) {
    if (isHTTPError(error) && error.status === 404) return showMissing()
    return report(error.code)
  }
  render(data) // the user: `error === null` narrows it
}
```

It resolves to `{ data, error: null }` or to `{ data: null, error }`, whose `error` is a `CallFailure`: a `RequestFailure` — an `HTTPError`, a `NetworkError` or a `TimeoutError` — a `ValidationError` for variables or an answer that failed their schema, or a `MissingTokenError`. `isCallFailure(error)` tells one apart in a `catch` as well. A cancelled call still rejects with its `AbortError`: nobody waits for its answer, so the code after it does not run as if one had come. A mistake in how the API is described or the call made — a `ConfigurationError` — rejects too, as a program's bug.

`.safe()` goes with `.raw` and `.with()`: `caller.users.raw.getById.safe({ id })` resolves to the whole response. An endpoint at the root has it too, `caller.health.safe()`. A stream's and a channel's failures are their events, so they have none. A client given neither a source nor routes reaches an endpoint named `safe` through one that has them, as it does an endpoint named `call` or `bind`.

## Through the Proxy

The browser's side and the server's side of a proxied call each fail by these rules. A browser that could not reach the proxy — offline, the server down — rejects with a `NetworkError`, and one whose `timeout` ran out with a `TimeoutError`. A browser call that was cancelled rejects with an `AbortError`, as a direct call does. When the server's side got no answer from the API, the proxy itself answers the browser, without naming the API's host: an `HTTPError` `502` with `data.code` `"UPSTREAM_UNREACHABLE"`, or `504` with `"UPSTREAM_TIMEOUT"` when the server's side ran out of time. The reason stays in the server's log.

## Related

- [HTTPError](/docs/api/http-error) — an answer with an error status
- [Error Handling Guide](/docs/core-concepts/error-handling) — patterns for handling them, `.safe()` among them
- [Built-in Middleware](/docs/middleware/built-in#retrymiddleware) — which failures a retry policy repeats
