---
title: Errors
description: The shape of a failed call, how a handler's own errors differ from Luna's, and every error code with its status and whether to retry.
related:
  - /lunas/return-errors
  - /sdk/errors
  - /clients/call-lunas-over-http
---

A failed call answers with an error code. The code says whether your handler refused the call or Luna stopped it, and whether trying again can help.

## Read the error body

Over HTTP, a failed call answers with a status and a JSON body. An error from Luna carries a `code` and a `message`.

```json
{"error": {"code": "NotFound", "message": "no luna type Lists"}}
```

A few codes add fields. `NoRoom` adds `reserve` and `retryAfterMs`, and `OutOfMemory` adds `limitMib`.

## Tell your errors from Luna's

A handler's declared error, thrown with `ctx.fail`, answers with status 422. Its body has the `code` you declared and its `data`, and no `message`.

```json
{"error": {"code": "Insufficient", "data": {"left": 100}}}
```

Every other status of a handler call comes from Luna. An HTTP route is the exception, since its handler sets its own status and body. A declared code may share a name with one of Luna's, such as `NotFound`. The status tells them apart: 422 is always yours. [Return errors](/lunas/return-errors) shows how to declare them.

When `onBeforeConnect` refuses a call with `ctx.fail`, the status is 403 and the code is the one your hook chose. The body adds `data` and a `message` saying the hook refused the connection.

## Catch errors in a luna

[`ctx.call`](/sdk/ctx) throws a [`LunaCallError`](/sdk/errors). A declared error sets `code` and `data`. An error from Luna sets `kind`, and `retryable` is true for `Overloaded`, `Timeout`, `OutcomeUnknown` and `Unavailable`.

```ts luna/lunas/shop.ts
try {
  return await ctx.call(Wallet, { id: ctx.key.id }).charge({ amount });
} catch (e) {
  if (!(e instanceof LunaCallError)) throw e;
  if (e.code === "Insufficient") ctx.fail("Declined", e.data);
  if (e.retryable) console.warn(`wallet busy: ${e.kind}`);
  throw e;
}
```

## Catch errors in an app

The client in `luna/client` throws a `ConnectionError`. Its `code` is the error code, whether declared or Luna's, and `data` holds a declared error's data.

```ts app/add.ts
import { ConnectionError, createClient } from "luna/client";

const client = createClient({ url: process.env.LUNA_URL!, key: process.env.LUNA_KEY });

try {
  await client.luna("List", "groceries").mutate("add", { text: "milk" });
} catch (e) {
  if (e instanceof ConnectionError) console.error(e.code, e.message, e.data);
}
```

## Every code

| Code | Status | Retry | Means |
| --- | --- | --- | --- |
| [`Cancelled`](/errors/cancelled) | 409 | No | The durable run was cancelled |
| [`Closed`](/errors/closed) | None | Yes | The app's connection or client is closed |
| [`Denied`](/errors/denied) | 403 | No | A file URL is missing its signature, or it is wrong |
| [`Internal`](/errors/internal) | 500 | No | The handler failed with an error it did not declare |
| [`Invalid`](/errors/invalid) | 400, 405 or 413 | No | The request does not match the luna type |
| [`NoRoom`](/errors/no-room) | 503 | Yes | No machine has room for what the luna reserves |
| [`NotFound`](/errors/not-found) | 404 | No | No such luna type, handler, route or luna |
| [`OutcomeUnknown`](/errors/outcome-unknown) | 502 | With an idempotency key | The luna stopped while the call ran |
| [`OutOfMemory`](/errors/out-of-memory) | 500 | No | The luna reached its memory limit |
| [`Overloaded`](/errors/overloaded) | 429 | Yes, later | Too many calls at once |
| [`PaymentRequired`](/errors/payment-required) | 402 | No | Work is paused until the organisation pays |
| [`Quarantined`](/errors/quarantined) | 503 | No | The luna crashed too often and is held back |
| [`QuotaExceeded`](/errors/quota-exceeded) | 429 or 507 | No | A quota or spending cap is reached |
| [`Suspended`](/errors/suspended) | 403 | No | The organisation is suspended |
| [`Timeout`](/errors/timeout) | 504 | With an idempotency key | No answer came before the deadline |
| [`Unauthorized`](/errors/unauthorized) | 401 or 403 | No | The key or token is missing, wrong or not allowed here |
| [`Unavailable`](/errors/unavailable) | 503 | Yes | The luna cannot run now, and the call never started |
