---
title: Errors
description: An application error carries a kind, a code, and details of an expected failure.
---

An application error is an `AppError` that a function throws for an expected failure.

<Diagram caption="The caller handles an AppError by code, and HTTP integrations map its kind to a status.">
  <Flow>
    <Node title="error" subtitle="Defines the expected failure" />
    <Arrow n={1} />
    <Node title="ctx.error" subtitle="Creates the error to throw" />
    <Arrow n={2} />
    <Node title="caller.try" subtitle="Switch on the code" tone="accent" />
    <Arrow n={3} />
    <Node title="toResponse" subtitle="Maps kind to status" />
  </Flow>
</Diagram>

```ts twoslash
import { init } from "logic-sdk";
import { z } from "zod";

const { error, query, createRegistry } = init<{ scope: { actor: string | null } }>();
// ---cut---
export const minNights = error({
  details: z.object({ minNights: z.number(), nights: z.number() }),
  message: "Stay at least $minNights nights",
});

export const priceStay = query({
  input: z.object({ nights: z.number() }),
  async handler(ctx, { nights }) {
    if (nights < 3) throw ctx.error(minNights, { minNights: 3, nights });
    return { total: nights * 120 };
  },
});

const registry = createRegistry({ modules: { pricing: { minNights, priceStay } } });
const caller = registry.as({ actor: null });
const result = await caller.try(priceStay, { nights: 2 });

if (!result.ok) {
  switch (result.error.code) {
    case "pricing.minNights":
      console.log(result.error.details.minNights);
      break;
    default:
      throw result.error;
  }
}
```

- **Raise (1).** Export errors from the module's index. Raise your module's own errors. Run another module's function to get its errors.
- **Handle (2).** Use `caller.try` to handle an expected failure by code. Unexpected exceptions still throw.
- **Forward (3).** Keep `default: throw result.error`. Use `caller.run` if you handle no cases.
- **Identity.** Codes use `<module>.<export>` or `<module>.<id>`. See [reserved names](/docs/reference/errors#built-in-codes).
- **Details.** Use a synchronous schema. Invalid details raise an unexpected exception.
- **Messages.** Insert a detail with `"Stay at least $minNights nights"`, or compute it: `` message: (d) => `Stay at least ${d.minNights} nights` ``.
- **Types.** Use `AppErrorOf<typeof registry>` for a helper that switches on codes. Use `minNights.is(e)` to narrow details.
- **Inspection.** Use `isAppError(e)` to check the class.
- **Kinds.** See [kinds, default messages, and statuses](/docs/reference/errors#kinds-and-statuses).

## Catching inside logic [#catching]

Catch in an action to compensate or continue. Translate known service failures where they occur. Rethrow the rest.

A failed nested mutation blocks commit. Catch it only to translate its error. Use a query first for a check you need to recover from.

For unexpected errors, see [validation exceptions and integration behavior](/docs/reference/errors). Job failures reach `onError`.

## Database errors

Use `sqlState` from `logic-sdk/drizzle/pg` to read wrapped SQLSTATE codes. Translate a code only where you know its meaning.

## See also

[Error reference](/docs/reference/errors), [Authorization](/docs/concepts/authorization), [HTTP](/docs/guides/http), [Jobs](/docs/concepts/jobs)
