Errors
The error classes the SDK throws and lets you throw, every kind of system error with its HTTP status, and the codes of JwtError.
A failed call throws a LunaCallError. It carries a code and data when the handler failed with an error it declares, and a kind when Luna could not run the call or the handler failed in some other way.
import { defineLuna, LunaCallError, t } from "luna";
import { Wallet } from "./wallet.ts";
export const Shop = defineLuna({
name: "Shop",
key: { id: t.string() },
handlers: (h) => ({
buy: h({ args: { user: t.string(), amount: t.int() }, errors: { Short: t.object({ have: t.int() }), TryLater: t.json() } }, async (ctx, { user, amount }) => {
try {
return await ctx.call(Wallet, { id: user }).charge({ amount });
} catch (e) {
if (!(e instanceof LunaCallError)) throw e;
if (e.code === "Short") throw e;
if (e.retryable) ctx.fail("TryLater", { kind: e.kind });
throw e;
}
}),
}),
});
Reference
LunaError
The error ctx.fail throws. Throwing one yourself does the same.
import { LunaError } from "luna";
codestring
The error's code, one the handler declares in errors.
stringdata?unknown
Data of the type the handler declares for the code.
unknownnullkeep?boolean
The failed call keeps what it wrote and sent.
booleanfalseConstruct it with new LunaError(code, data?, { keep? }).
LunaCallError
What a call to another luna throws when it fails.
codestring | undefined
The declared error's code, for an error the handler declares.
string | undefineddataunknown
The declared error's data, or null.
unknownkindSystemKind | undefined
The system error's kind, for any other failure.
SystemKind | undefinedretryableboolean
True for Overloaded, Timeout, OutcomeUnknown, Unavailable and NoRoom. Never true for a declared error.
booleanSystemKind
Each kind has a page under Errors. A call over HTTP answers with the status shown.
| Kind | Status | Retryable | Meaning |
|---|---|---|---|
Invalid |
400 | No | The call was malformed, such as an unknown luna type or handler, or arguments that do not match. |
NotFound |
404 | No | The handler requires an existing luna, and this one has no stored data. |
Overloaded |
429 | Yes | Too many calls are running or waiting on the luna. |
Timeout |
504 | Yes | The call’s deadline passed. |
OutcomeUnknown |
502 | Yes | The luna stopped after it received the call, so the handler may or may not have run. Retry with an idempotency key. |
Unavailable |
503 | Yes | The luna cannot run now, and the call never started. |
Quarantined |
503 | No | The luna crashed too often and is held back. |
NoRoom |
503 | Yes | The luna type reserves its limits, and no machine has room for them now. The call never started. |
OutOfMemory |
500 | No | The luna reached its memory limit, and the same call would again. |
Cancelled |
409 | No | The durable run was cancelled, or the caller went away. |
Unauthorized |
401 | No | The call came without a valid key or token, or with one that may not be used here. |
Denied |
403 | No | onBeforeConnect refused the caller, or a file URL’s signature was wrong. |
QuotaExceeded |
429 | No | The organisation or tenant reached a quota or its spending cap. |
PaymentRequired |
402 | No | Work is paused until a payment goes through. |
Suspended |
403 | No | Luna suspended the organisation. |
Closed |
None | No | The client, or its connection to the luna, is closed. |
Internal |
500 | No | The handler threw an error it does not declare, failed with a code it does not declare, or returned a result that does not match returns. |
A declared error answers 422 over HTTP, unless the route maps its code to another status.
HttpError
Thrown to a caller through an SDK when the handler answers with a web Response whose status is 400 or above.
import { HttpError } from "luna";
statusnumber
The response's status.
numberheadersHeaders
The response's headers.
Headersbodyunknown
The response's body.
unknownIsolateError
Thrown by a run of an isolate that failed.
import { IsolateError } from "luna";
namestring
The name of the error the code threw, such as TypeError, or CpuLimitExceeded, MemoryLimitExceeded or TimeoutExceeded when a limit stopped it.
stringmessagestring
The error's message.
stringlogsIsolateLog[]
What the code logged before it failed.
IsolateLog[]snippetStackstring
The stack trace inside the code that ran.
stringRunCancelled
Thrown in a durable run that was cancelled, at its first new step, sleep or wait. Its message is the run was cancelled.
import { RunCancelled } from "luna";
RunCodeChanged
Fails a durable run when a deploy changed the order of its steps, sleeps and waits. The message names the operation that changed. Finish or cancel runs before you change their code.
import { RunCodeChanged } from "luna";
JwtError
A LunaError that the token checks in luna/auth throw. Thrown from onBeforeConnect, it refuses the caller with its code, as ctx.fail(code) does.
import { JwtError } from "luna/auth";
| Code | Meaning |
|---|---|
malformed |
The token is not a well-formed JWT, or lacks a claim it needs, such as exp. |
bad_signature |
The signature does not verify. |
expired |
The token’s exp has passed, or a DPoP proof is too old. |
not_yet_valid |
The token’s nbf is still to come. |
wrong_issuer |
The token’s iss is not the one expected. |
wrong_audience |
The token’s aud is not the one expected. |
unknown_key |
No key fits the token’s kid. |
bad_algorithm |
The token’s algorithm is not allowed, or the key cannot verify it. |
bad_proof |
A DPoP proof is malformed or signed by another key than the token is bound to. |
replayed |
A DPoP proof was used before. |
Good to know
- A declared error from a luna you call passes through your handler when your handler declares the same code. Otherwise your caller gets
Internal. - A failed call undoes its writes and sends nothing, unless it failed with
keep: true. - An app’s client throws
ConnectionError, covered in connect.
Examples
Keep a failed attempt
The attempt counts toward a lockout even though the call fails.
login: h({ args: { password: t.string() }, errors: { WrongPassword: t.object({ left: t.int() }) } }, (ctx, { password }) => {
ctx.db.attempts.insert({ at: ctx.now() });
const left = 5 - ctx.db.attempts.count();
if (password !== ctx.env.SHOP_PASSWORD) ctx.fail("WrongPassword", { left }, { keep: true });
ctx.db.attempts.delete({});
}),
Report what broke in an isolate
price: h({ args: { code: t.string() } }, async (ctx, { code }) => {
try {
const { result } = await ctx.isolates.rule.run(code);
return result;
} catch (e) {
if (e instanceof IsolateError) return { error: e.name, message: e.message };
throw e;
}
}),