Errors
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.
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.
{"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.
{"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 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 throws a LunaCallError. A declared error sets code and data. An error from Luna sets kind, and retryable is true for Overloaded, Timeout, OutcomeUnknown and Unavailable.
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.
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 |
409 | No | The durable run was cancelled |
Closed |
None | Yes | The app’s connection or client is closed |
Denied |
403 | No | A file URL is missing its signature, or it is wrong |
Internal |
500 | No | The handler failed with an error it did not declare |
Invalid |
400, 405 or 413 | No | The request does not match the luna type |
NoRoom |
503 | Yes | No machine has room for what the luna reserves |
NotFound |
404 | No | No such luna type, handler, route or luna |
OutcomeUnknown |
502 | With an idempotency key | The luna stopped while the call ran |
OutOfMemory |
500 | No | The luna reached its memory limit |
Overloaded |
429 | Yes, later | Too many calls at once |
PaymentRequired |
402 | No | Work is paused until the organisation pays |
Quarantined |
503 | No | The luna crashed too often and is held back |
QuotaExceeded |
429 or 507 | No | A quota or spending cap is reached |
Suspended |
403 | No | The organisation is suspended |
Timeout |
504 | With an idempotency key | No answer came before the deadline |
Unauthorized |
401 or 403 | No | The key or token is missing, wrong or not allowed here |
Unavailable |
503 | Yes | The luna cannot run now, and the call never started |