# Error Handling

## Introduction

An error thrown anywhere in a request, in a middleware, a binding or a controller, ends in the app's exception handler. The kernel logs
the error when it is a bug, and the handler turns it into the right answer: an error page for a browser, JSON for every other client. To stop a
request with a status of your choice, call `abort()`:

```ts title="modules/notes/controllers/ShowNoteController.ts"
import type { Authenticated } from '@marmeon/auth';
import type { Row } from '@marmeon/database';
import { abort, Controller, type HttpContext } from '@marmeon/http';

export class ShowNoteController extends Controller {
  handle(ctx: HttpContext<Authenticated, { note: Row<'notes'> }>) {
    if (ctx.params.note.archived_at !== null) abort(410, 'This note was archived.');
    return this.view('notes/Show', { note: ctx.params.note });
  }
}
```

In development, a server error shows the development error page with the stack, the request and suggested solutions. In
production, the visitor sees the app's error page, and the log gets the details.

## Throwing HTTP errors

`abort(status, message?, headers?)` throws an `HttpError` and never returns. It works in a controller, a middleware, a binder, a
policy and the route's check alike:

```ts
abort(404);
abort(403, 'Only the author may edit this note.');
abort(503, 'Down for maintenance.', { 'retry-after': '120' });
```

The message of an `HttpError` is meant for the client: the error page and the JSON answer show it. Without a message, the client
sees the status text, `Not Found` for a 404, in the request's language. A message may be a translation key, such as
`abort(403, 'notes.errors.not_yours')`, and the exception handler shows its text when the key exists.

`new HttpError(status, message, headers)` is the same error as a value, for code that builds it before it throws.

Some errors come with the framework:

| Error | Status | Thrown by |
| --- | --- | --- |
| `ValidationError` | 422 | A request's schema, or `throw new ValidationError({ title: ['…'] }, input)` in a controller. |
| `HttpError(403)` | 403 | A request's `authorize` that returns `false`. |
| `AuthorizationError` | 403, or 404 | A policy that says no, through `can()` or the gate. `denyAsNotFound()` makes it a 404. |
| `HttpError(404)` | 404 | A route that matches nothing, a binding that finds no record. |
| `HttpError(419)` | 419 | A form that fails the [CSRF](csrf.md) check by its token. |
| `HttpError(413)` | 413 | A body over `SERVER_MAX_BODY_MB`. |

A `ValidationError` thrown by your controller answers like a failed schema: the form goes back with the errors, and a JSON client
gets a 422.

## What the client sees

The exception handler decides by what the request asks for:

- **A page**, for a browser's request or a visit inside the app, renders the view `system/errors/Error` with the status. Its title is the
  status text, and its message is the `HttpError`'s message.
- **Anything else** gets JSON with the status: `{ "message": "Not Found" }`.
- **A validation error** goes back to the form with the errors and the input, or answers a JSON client with a 422 and
  `{ message, errors }`.

Because the answer depends on the request's headers, every error answer carries `Vary` for the ones it was chosen by (`Accept`
and `X-Marmeon`, and `X-Requested-With` for a validation error), so a shared cache keeps the page and the JSON apart. A `Vary` an
`HttpError` brings itself is kept; these are added to it.

`abort(204)`, `abort(205)` and `abort(304)` answer with that status and no body, for a page and for JSON alike. An action reads a 204
or 205 as a success without data, and a 304 as an error. The headers of the `HttpError` (an `ETag`, say) go out with them.

An `abort()` with a status that is not a whole number from 200 to 599 (`abort(0)`, `abort(404.5)`) is answered and reported as a 500.

Any error that is not an `HttpError` is a 500. Its own message never reaches the client in production: the page and the JSON say
`Server Error`, so a database error cannot show its query. The headers of an `HttpError` go out with the answer, so a 503 carries
its `Retry-After`.

An error becomes a response at the layer that threw it, so the middleware around it still runs. An error page carries the same
security headers and cookies as every other page.

### Your own error page

Until your app brings its own, a built-in error page shows the status, the title and the message. Yours replaces it as the view
`system/errors/Error`, which is the file `modules/system/views/errors/Error.tsx`:

```tsx title="modules/system/views/errors/Error.tsx"
import type { ErrorPageProps } from '@marmeon/http';
import { Head, Link } from '@marmeon/react';

export default function Error({ status, title, message }: ErrorPageProps) {
  return (
    <main>
      <Head title={title} />
      <p>{status}</p>
      <h1>{title}</h1>
      {message !== title && <p>{message}</p>}
      <Link href="/">Back to the start page</Link>
    </main>
  );
}
```

The page renders inside your default layout. A request that matches no route passes no group's middleware, so its 404 page has
no shared props: declare them optional, and let the layout render without them. `ErrorPageProps` also has `exception`, which holds
the error's name, message and stack only in development.

#### A page per status

A view named for a status comes before `system/errors/Error`: with `modules/system/views/errors/404.tsx`, a 404 shows that page, and every other
status still shows `system/errors/Error` (or the built-in page). The view takes the same `ErrorPageProps`. Only the exact status counts,
there are no patterns such as `4xx`. The server and the browser choose alike, so the page hydrates, and the page object still
says `system/errors/Error`: tests and `PageInfo.component` see no change. What follows the view's name does change: the default layout
of your app, `layout(name)`, is asked with `system/errors/404`, not with `system/errors/Error`. The same goes for an error while a page renders
(status 500, `system/errors/500`) and for the dialog that shows a 5xx over the page.

## Results

An exception is for what should not happen: a bug, a lost connection, a request that stops with a status. An outcome that
belongs to the work is a `Result` instead, such as an invitation that expired or an address another account has taken. The
caller sees it in the return type and has to handle it, and nothing is thrown or logged.

`ok(value)` and `err(error)` of `@marmeon/core` make the two sides. A `Result<T, E>` is plain data, `{ ok: true, value }` or
`{ ok: false, error }`. The error names its case in `kind`, with what the caller needs next to it. `err()` keeps the kind as a
literal and gives the rest its usual types, so `err({ kind: 'invalid', fields: ['email'] })` fits a declared `fields: string[]`:

```ts
accept(invitationId: number, userId: number): Promise<Result<Member, { kind: 'expired' } | { kind: 'already-member'; teamId: number }>>
```

Inside, `return err({ kind: 'expired' })` ends with an error and `return ok(member)` with the value. `ok()` without a value is
for work that only succeeds or fails. The caller narrows the result over `ok`, then switches over `error.kind`. The invitation's token
is made with [`linkToken()`](urls.md#a-link-that-is-used-once). The link's route trades it for the invitation's id in the session, and the
controller takes the row from `PendingInvitation`, so the post has no token in its path
([a form behind a token](security.md#a-form-behind-a-token-in-code)):

```ts title="modules/teams/controllers/AcceptInvitationController.ts"
import type { Authenticated } from '@marmeon/auth';
import { abort, Controller, type HttpContext } from '@marmeon/http';
import { Invitations } from '../Invitations.ts';
import { PendingInvitation } from '../PendingInvitation.ts';

export class AcceptInvitationController extends Controller {
  readonly #invitations: Invitations;
  readonly #pending: PendingInvitation;

  constructor(invitations: Invitations, pending: PendingInvitation) {
    super();
    this.#invitations = invitations;
    this.#pending = pending;
  }

  async handle(ctx: HttpContext<Authenticated>) {
    const invitation = await this.#pending.require();
    const accepted = await this.#invitations.accept(invitation.id, ctx.user.id);
    if (!accepted.ok) {
      switch (accepted.error.kind) {
        case 'expired':
          // The same 404 as a link that never existed: an answer of its own would tell which links were real.
          return abort(404);
        case 'already-member':
          return this.redirect(`/teams/${accepted.error.teamId}`);
        default:
          return accepted.error satisfies never;
      }
    }
    return this.redirect(`/teams/${accepted.value.team_id}`);
  }
}
```

The `default` makes the switch exhaustive. It suits your own results and the framework's results listed
[below](#results-the-framework-returns), whose kinds are closed. When `accept()` gains a kind, the controller stops compiling
until it handles the new one:

```text
Type '{ kind: "banned"; }' does not satisfy the expected type 'never'.
```

A function with a declared return type needs no `default`: a missing case leaves its end reachable, and the compiler says so.
HTTP stays with exceptions: `findOrFail()` answers 404 by throwing, and `abort()` stops a request. Returned from the callback of
a transaction, an `err` rolls the transaction back, as the [transactions](transactions.md#returning-an-err) page explains.

### Results the framework returns

| API | `err` kinds | Page |
| --- | --- | --- |
| `repository.tryInsert()` | `duplicate` | [Queries](queries.md#writing) |
| `repository.tryUpdate()` | `duplicate`, `missing` | [Queries](queries.md#writing) |
| `auth.attempt()` | `invalid-credentials`, `two-factor-required`, `throttled` | [Authentication](authentication.md#signing-in) |
| `twoFactor.challenge()` | `no-challenge`, `expired`, `invalid-code`, `throttled` | [Two-factor authentication](two-factor-authentication.md#the-sign-in) |
| `twoFactor.confirm()` | `not-started`, `invalid-password`, `invalid-code`, `throttled` | [Two-factor authentication](two-factor-authentication.md#setting-up) |
| `SignInStore.consume()` | `invalid`, `stolen` | [Authentication](authentication.md#remember-me) |
| `AccountSessions.end()` | `missing` | [Authentication](authentication.md#ending-every-session-of-a-user) |
| `PasswordBroker.createToken()` | `unavailable` | [Password reset](passwords.md#asking-for-a-link) |
| `PasswordBroker.reset()` | `invalid-token` | [Password reset](passwords.md#resetting-the-password) |
| `PasswordResetTokenStore.create()` | `throttled` | [Password reset](passwords.md#a-store-of-your-own) |
| `PasswordResetTokenStore.consume()` | `invalid-token` | [Password reset](passwords.md#a-store-of-your-own) |
| `lock.get(callback)` | `locked` | [Cache](cache.md#locks) |
| `lock.block(wait, callback)` | `timeout` | [Cache](cache.md#locks) |

These `err` kinds are closed: a new kind comes only with a new method, never as a new member of an existing one. That is why an
exhaustive `switch` over them with `satisfies never` keeps compiling across minor versions. Other unions are open, as
[when a union grows](releases.md#when-a-union-grows) explains.

`SignInStore` and `PasswordResetTokenStore` are the contracts a store of your own implements, so the table lists the kinds
each has to return. A `get(callback)` whose callback returns a `Result` gives a nested one: the lock's `ok` holds the callback's
result. An exception from the callback is rethrown, not turned into an `err`.

Some of these `err`s must survive the transaction they are returned from, because the work behind them is a protective one. They
are made with [`commitAnyway()`](transactions.md#returning-an-err), and a store of your own does the same:

- `SignInStore.consume()` for `stolen` (all of the user's sign-ins are deleted, their tokens with them) and for `invalid` on an expired
  token (the token is taken off its sign-in).
- `PasswordResetTokenStore.consume()` and, through it, `PasswordBroker.reset()` for `invalid-token` on an expired token (the token
  is deleted). `reset()` also commits the `invalid-token` of an account that is gone, so that the token stays used up.

Every other `err` above commits nothing and rolls an enclosing transaction back when its callback returns it.

Where a kind would tell whether an account exists, it is left out on purpose. `attempt()` answers `invalid-credentials` for an
unknown identifier, a wrong password and an account without a password alike; `two-factor-required` is answered only after the
password was right. `reset()` answers `invalid-token` for an unknown
address and for a wrong, used or expired token. `createToken()` answers `unavailable` for an address without an account and for one
whose last link went out moments ago. Do not add kinds of your own that tell these apart.

Two calls are not `Result`s because nothing failed: a unique job that is already queued or running makes `dispatch()` return `undefined`, and
`job.once()` returns `false` when the work already ran. Yes-or-no questions, such as `lock.get()` without a callback
and `auth.check()`, stay `boolean`.

## Reporting

Every server error is logged at the `error` level: its message and its stack, masked as text by the app's `Redactor`, so a token, a
password in a connection string or the value of the session cookie does not reach the log. An `HttpError` with a status below 500 is
not reported: a 404 or a 403 is an answer, not a bug.

To send errors elsewhere as well, such as to an error tracker, write an error reporter. A provider brings it with
`static errorReporters`, next to its other statics, and the container builds it once:

```ts title="modules/system/TrackerReporter.ts"
import type { ErrorReport, ErrorReporter } from '@marmeon/core';
import { ErrorTracker } from './ErrorTracker.ts';

export class TrackerReporter implements ErrorReporter {
  readonly #tracker: ErrorTracker;

  constructor(tracker: ErrorTracker) {
    this.#tracker = tracker;
  }

  async report({ error, source, context }: ErrorReport): Promise<void> {
    // The report is frozen: a SDK that wants to change what it gets, or keep it, takes a copy.
    const copy = Object.assign(new Error(error.message), { ...error });
    await this.#tracker.capture(copy, { source, ...context });
  }

  // Optional: called when the app shuts down, to send what is still buffered.
  flush(): Promise<void> {
    return this.#tracker.flush();
  }
}
```

```ts title="modules/system/SystemServiceProvider.ts"
import type { ServiceProvider } from '@marmeon/core';
import { TrackerReporter } from './TrackerReporter.ts';

export class SystemServiceProvider implements ServiceProvider {
  static errorReporters = [TrackerReporter];
}
```

The Redactor masks secrets it recognises, not every value: an error whose message quotes data, such as a schema's `received "…"`,
reaches the reporter with it. Only a job's payload mismatch is reduced to its field names.

A reporter never sees the error itself. It gets an `ErrorReport` with the **masked view** only:

- `error.name` (the class name when it is an identifier, else `Error`), `error.message` and `error.stack`, each passed through the
  app's `Redactor`, so a connection URL, a token or the session cookie an error quotes is gone;
- `error.code` and `error.status`, when the error has them, and `error.cause`, masked the same way, at most five levels deep. A cause
  that leads back to an error already shown ends the chain;
- `source` and a `context` of masked texts, by what failed:

  | `source` | When | `context` |
  | --- | --- | --- |
  | `request` | A request ends in a server error, or work of a request that runs after its response fails (see below) | `method`, `route` (the route's name, empty when it has none), `path` (without its query, the segments of sensitive route parameters masked); `phase` for work after the response: `deferred`, `after-response` or `terminate` |
  | `job` | A job has failed for good: its last attempt, or an attempt that cannot be repeated (`job.fail()`, a payload that cannot run). Attempts that run again are only logged. | `job` (its name), `jobId`, `queue`, `connection`, `attempt`. Never the payload; a payload that no longer matches its schema is reported by the names of its fields, not the values the schema quotes |
  | `schedule` | A scheduled task throws | `task` (its name) |
  | `command` | A `marmeon` command ends with an error | `command` (its name). Never the arguments: they can carry secrets |

Every process that boots the app builds its reporters: each `marmeon` command and each worker too. A timer or a socket that the constructor opens needs `unref()` or a stop in `flush()`, or the process never ends; a heavy connection is better opened lazily, on the first `report()`.

The original error object, its own fields and the request are not part of it. The report is frozen, down to each cause, so one reporter
cannot change what the next one sees.

A reporter is built once, when the app boots, and runs outside any database transaction. If it cannot be built, the app does not start (`K12`): a wrong DSN must not turn reporting off unnoticed. A reporter cannot hold a request up or take the app down, as long as it does its work in the promise it returns; work it keeps running in the background itself (its memory, its concurrency, rejections nobody handles) is its own business:

- A request never waits for a reporter. The report is handed over after the log line, and what the reporter does with it happens on its own. A call that takes longer than 10 seconds counts as failed (a warning) but keeps its place until it really ends. A reporter whose calls never end gets no further reports until the app restarts.
- One that throws or rejects costs a warning in the log, once per report, and the other reporters still run. What a reporter throws is
  not reported again, and neither is an error that a reporter reports itself: that would be a loop.
- Each reporter has at most 100 reports open at a time. One that hangs fills its own limit and does not slow the others; its
  further reports are dropped (they are still logged), with one warning, and a line with the number of dropped reports follows in the log, at most once a minute,
  also while a flood goes on.
- When the app shuts down, `flush()` of each reporter is called before the providers shut down, while the open reports finish, and the
  shutdown waits at most 2 seconds for all of it. A reporter that is still busy then loses its reports and the shutdown goes on, with a warning.

Server errors of requests, jobs that failed for good, scheduled tasks and commands reach the reporters. So do the errors of work
that a request leaves for after its response: a deferred group that fails while the page streams (`phase: 'deferred'`; the browser
then loads the group itself, and a second failure there is a request error of its own), an `afterResponse.defer()` task
(`'after-response'`) and a `terminate` hook (`'terminate'`). They are reports of the request they belong to, so a reporter groups
them by `route` as before; they carry the same facts and nothing else, never the request's body, cookies or headers nor the props themselves; what an error's message quotes is masked like any request error's (see above).
The reporters are flushed when the app shuts down, after the server has finished this work (it waits at most 8 seconds for it; a report of work still running then may be lost). A deferred group that only takes too
long is logged, not reported. These are logged but not
reported: errors of server rendering, errors at shutdown, a job that ignores its timeout and ends its worker (it runs again after `retryAfterSeconds`), a job that was queued by an error reporter (see below), a job on the `sync`
connection (its error reaches whoever dispatched it, who reports it as their own), a command that is called wrong (exit code 2) or
ends with an exit code but no error, and a prompt that was cancelled. To report an error you caught yourself, for example when a
`catch` still answers 200, ask the app for the service:

```ts
app.container.make(ErrorReports).report(error, { source: 'request' });
```

A reporter that queues a job, say one that sends an alert, does not start a loop: if the alert fails for good it is logged, not
reported again, and the jobs it queues in turn are marked the same way. The mark covers what `report()` and `flush()` of a reporter
start, and the jobs those queue. A timer opened in the constructor or an `afterResponse` callback is not covered: a job queued from
there is reported like any other. Look for the failures that are not reported in the log and in the failed jobs.

`ExceptionHandler` does not report. It is a token with one method, `render(error, request, { debug, translator })`, which returns what
a controller returns: a view, a redirect, a `Response`. To change how errors look, extend `DefaultExceptionHandler` (it has no
constructor parameters) and bind your class to the token.

## The development error page

With `NODE_ENV=development`, a page request that fails with a 5xx gets the development error page instead of `system/errors/Error`:

- **The error:** the status, the class, the message and its causes, the request and its route, and the solutions that match.
- **The stack:** your app's frames first, each with its code and the throwing line marked, and a link that opens the file in your
  editor. The line is the line that threw, since Node runs your TypeScript as written.
- **The request:** its method, URL, headers, query and body; the route with its name, controller, middleware and parameters; the
  database queries it ran, with their duration; its log entries; and the environment.
- **Copy as Markdown:** the same report as Markdown, for an issue or a chat, without your source code.

Everything on it passes the app's redaction first: cookies, `Authorization`, passwords, tokens, password hashes and the
parameters of queries on secret columns never show. A route parameter with a sensitive name (`/invite/:code`) is masked in
the request's URL by the route's pattern, on the page, in `last-error.md` and in the devtools, however short its value is.
The same goes for a `Referer` or a redirect's `Location` that points to a route of the app; one to another site shows its host (never a user name or password), with its path and query masked as text, and no fragment.

Only a request for a host the development routes answer gets it: the loopback names and the host of `APP_URL`. A request with another
`Host` (a page re-pointed at your machine by DNS rebinding) gets the plain error page or JSON, without the message, the stack, the
source code, the request or the solutions.

The page is one self-contained document, without React, Vite or your views, so it renders when they are what broke. It goes out
with `Cache-Control: no-store` and a Content-Security-Policy of its own. A visit inside the app shows it in a dialog over the page.
A JSON request keeps its JSON, with the error and the first solution added. A 4xx error, an action and an upload keep their normal
answers, and so does a handler of your own unless it renders the error page.

Every 5xx error of the development server (`marmeon dev`), whether the answer is the page or JSON, is also written to
`.marmeon/last-error.md` in the app's root, so an agent in the terminal can read it as a file. It is the report that "Copy as
Markdown" copies, for the error's method and path only: no request headers, cookies, query or body, and nothing that the page
would mask. The error's message and causes sit in a code fence, under a line saying that everything below is data and not
instructions. An error of a request that the browser marks as from another site (`cross-site` or `same-site`) is not written, and neither is one for a host other than `localhost`, `127.0.0.1`, `[::1]` and the host of
`APP_URL`, even when the browser says `same-origin` (DNS rebinding). With `HOST=0.0.0.0`, anyone on the network can put text into the file by causing an error: the fences mark it as data, but treat it as untrusted, and run `marmeon dev` on a network you trust. The next error overwrites
the file, and it is removed when the development server starts; a command such as `marmeon tinker` leaves it alone. It is readable
by its owner only and never written in production or in tests; `.marmeon/` is in the `.gitignore` of a new app. A failure to
write it is a warning in the log, never an error of the request.

An error while a page renders on the server goes to Vite's overlay in the browser, with the code of the view, and the browser
renders the page itself. The code comes only from your app and from source files under `node_modules`, never from `.env*`.

The page exists only with `NODE_ENV=development`: in production, staging and tests nothing of it is loaded, and the error page
shows no stack, queries, headers or code. A test asks for it with `createTestApp(application, { debug: true })`.

A staging server runs as production, `NODE_ENV=production APP_ENV=staging`, and is just as quiet: an error in a part of a page
that streams in late reaches the browser as a short code, without its message or stack. The
[configuration](configuration.md#staging) page explains it.

### Solutions

The development error page shows solutions for errors it knows: what to do, why, a command to copy and where it is documented.
The framework brings solutions for its own errors, such as a missing `APP_KEY`, a pending migration, a missing table, an unknown
route name with a suggestion, or a missing view. The `marmeon` command prints them too when the app cannot start.

Your app can add its own. A service provider lists them in `static solutions`:

```ts title="modules/billing/BillingServiceProvider.ts"
import { defineSolutions, type ServiceProvider } from '@marmeon/core';
import { PaymentError } from './PaymentError.ts';

export class BillingServiceProvider implements ServiceProvider {
  static solutions = defineSolutions([
    {
      for: PaymentError,
      when: (error) => error.code === 'declined',
      solve: () => ({
        title: 'Use a test card',
        text: 'The payment provider declines real cards in its test mode.',
        command: 'pnpm marmeon billing:test-cards',
        docs: 'https://wiki.example.com/billing#test-cards',
      }),
    },
  ]);
}
```

`for` names the error's class, and its subclasses match too. `when` narrows it, and `solve` may look things up, or return nothing
after all. The error's causes are searched as well. Solutions are text only: nothing on the page runs a command or calls an
endpoint, since a page that runs code would be a way into your machine.

A provider that brings only solutions is fine; one with no method and no static list at all is refused at start-up. The
[service providers](providers.md#register-and-boot) page explains the rule.

### Server error codes

An error the framework raises at start-up, in the development server or in a command starts its message with a code in square
brackets, such as `[marmeon:K6] Invalid configuration — the application cannot start`. The code stays in logs, in `cause` chains
and across processes (a worker, a command), so one search finds the row below. The development error page, its Markdown and the
`marmeon` command show it next to the solution. It appears only where developers and agents read: never in a response to a
browser in production.

A solution can be tied to a code with `code` in its definition. It then matches only an error with exactly that code, on the same
level of the `cause` chain. `errorCodeOf(error)` from `@marmeon/core` returns an error's code, so your own reporter can tag it.

A letter names the area, a number is given once and never reused. A code keeps its meaning: it never moves to another error. A code that is gone stays in the table as retired. The browser's
codes, `C` and `R`, are listed under [Error codes](#error-codes) below.

| Letter | Area |
| --- | --- |
| K | Start-up: application, container, configuration, discovery, commands |
| H | HTTP: routes, requests, pages, the page protocol, Vite |
| D | Data: connections, migrations, guards, generated types |
| A | Sign-in and sessions |
| Q | Background work: queues, schedules, mail, notifications |
| S | Services: cache, storage, redis, translations, logs, telemetry, validation |

| Code | Package | Meaning |
| --- | --- | --- |
| K1 | `core` | Nothing is bound for a token or an interface, or an abstract store class (`SessionStore`, `AccessTokenStore`, …) was built itself because no store is bound. [The container's errors](container.md#the-containers-errors) |
| K2 | `core` | A scoped service was resolved outside a scope, or by a singleton. [The container's errors](container.md#the-containers-errors) |
| K3 | `core` | A class takes constructor arguments but has no injection list, also when discovery could not resolve a parameter because its type is imported from outside the package. [Constructor injection](container.md#constructor-injection) |
| K4 | `core` | Classes that need each other to be built. [The container's errors](container.md#the-containers-errors) |
| K5 | `core` | The injection transform cannot derive a list from a constructor, or cannot embed it into compiled output. [Constructor injection](container.md#constructor-injection) |
| K6 | `core` | The configuration or `NODE_ENV` is invalid, so the app cannot start. [When a value is wrong](configuration.md#when-a-value-is-wrong) |
| K7 | `core` | Discovery of a package's providers or injections failed, or the `"marmeon"` field of a package in `package.json` has a key that is not known, is no object, or holds a wrong `providers`, `browser`, `experimental`, `namespace`, `entry`, `views` or `lang` (a namespace two packages share, a path that leaves its package or a file in `views` whose name is no view name are the same: [pages of a package](package-pages.md#pages-of-a-package)); a `package.json` that is no JSON object is the same. So is a `main`, `types` or `exports` entry that lies outside the package (also through a symlink), or an `exports` nested deeper than 32 levels. So is a `.d.ts` that re-exports from a file outside its package, or re-exports nested deeper than 64 levels. So is a listed provider whose export is no class at run time, at every boot. With `virtual:marmeon/test-packages`, also a module of your workspace whose needed export is `undefined`, or whose class to inject is `null` or a primitive (where a test mocks it, fix the mock; one that leaves an export out ends in Vitest's own error): [packages of your workspace](vite.md#packages-of-your-workspace). Fix the package: an app that depends on a package it does not control can patch it (`pnpm patch <package>`) or replace it (`overrides` in `package.json`) until the package is fixed. [Discovery](modules.md#discovery), [the field](package-development.md#the-marmeon-field) |
| K8 | `cli` | A command name is taken twice or is one marmeon answers itself (`list`, `help`) or the launcher handles (`dev`, `start`), or a command declares the option `--no-interaction`. [Writing commands](console.md#writing-commands), [Asking a question](console.md#asking-a-question) |
| K9 | `core` | A class is listed as a service provider but has no hook and no static. [Register and boot](providers.md#register-and-boot) |
| K10 | `core` | A definition carries a boot option (`env`, `phase`, `override`); they belong to `createApplication(definition, options)` and `createTestApp()`. [Creating the test app](testing.md#creating-the-test-app) |
| K11 | `marmeon` | A check cannot be registered: its `static check` is not kebab-case, it has no `static phase`, or its name is one of `marmeon`'s own or taken by another class. [Checks](package-commands.md#checks) |
| K12 | `core` | An error reporter of a provider's `static errorReporters` could not be built, so the app does not start. [What a provider brings](providers.md#what-a-provider-brings-besides-code) |
| K13 | `core`, `marmeon` | A health check's or an app check's `static timeout` is zero or less, or over the limit (a minute, ten minutes), which only a value meant as milliseconds can be. `static timeout` is in seconds: for 5000 ms write `{ milliseconds: 5000 }`. [Writing a health check](health.md#writing-a-health-check) |
| K14 | `marmeon` | A file holds an error page or the layer backdrop where the framework no longer looks for it: `modules/errors/views/Error.tsx`, `modules/errors/views/<status>.tsx` or `modules/layers/views/Backdrop.tsx`. They are pages of the `system` module now (`modules/system/views/errors/`, `modules/system/views/layers/Backdrop.tsx`): move the file, then run `marmeon route:types`. `marmeon dev`, `build`, `route:types` and `check` stop until you do. [Your own error page](errors.md#your-own-error-page) |
| K15 | `core` | A pattern of `redact` in `defineApplication()`, or of `Redactor.addPatterns()`, is refused: it is no `RegExp`, it is sticky (`y`), it matches the empty string, it holds a back-reference, or it repeats a group that repeats without limit (`(a+)+`), which can stall every log call. [Patterns of your own](logging.md#patterns-of-your-own) |
| K16 | `core` | A class's injection list holds an `undefined` entry (or `make()` is given `undefined`), usually because two modules import each other and one class was not defined yet. [The container's errors](container.md#the-containers-errors) |
| H1 | `server` | A request of the page protocol named another version, or none, and was answered with `409` before any route ran. [A tab of another protocol version](navigation.md#a-tab-of-another-protocol-version) |
| H2 | `marmeon` | `bootstrap/routes.ts` was generated by another version of Marmeon: `marmeon route:types --check` and `marmeon check` name it, and `marmeon route:types` writes it again; a file of a newer version needs `@marmeon/*` updated. [Typed route names](routing.md#typed-route-names) |
| H3 | `router` | Routes that do not say who may call them, or that let everyone call them and bind a record that any key finds, so the app cannot start. [Every route decides](authorization.md#every-route-decides) |
| H4 | `router`, `http` | A route name that no route has, in `route()`, `redirect().route()` or a signed URL. [URLs for named routes](urls.md#urls-for-named-routes) |
| H5 | `server` | Vite, `@marmeon/vite` or the `marmeon()` plugin is missing, so `marmeon dev` and `marmeon build` cannot start. [When the plugin is missing](vite.md#when-the-plugin-is-missing) |
| H6 | `http` | A write in a read-only request, such as an island: thrown in development and tests, dropped in production. [How islands load](deferred-props.md#how-islands-load) |
| H7 | `live` | Live updates are not set up: `LIVE_DRIVER=redis` without `@marmeon/redis`, `LIVE_DRIVER=database` without `@marmeon/database` or with a `live_hints` table whose `id` is not an auto-incrementing column (`AUTOINCREMENT` on SQLite, an identity or serial column on Postgres), or the memory driver under `--workers`. [More than one process](realtime.md#more-than-one-process) |
| H8 | `router` | A middleware passed a key to `next()` that the context has already: one of the framework (`request`, `params`, `route`, `query`, `cookies`, `ip`, `body`, …), one an earlier middleware set (`user` after `authenticate()`) or a name every object has (`then`, `toJSON`, `constructor`). Extending the context with a new key works, replacing a key does not. Also at start: two middleware of one route declare the same key in `adds`. [Adding to the context](middleware.md#adding-to-the-context) |
| H9 | `server` | Routes under a locale cannot work: no locales are bound (`Route.localized()` needs `@marmeon/i18n`), a route that is not localized starts with a locale of the app, a localized fallback's prefix starts with a locale of the app, another route answers the URL without the locale, or the app has no locales or one that is not letters and digits joined by hyphens. With the default locale without a prefix also: it is none of the supported locales, a localized route starts with a locale of the app after `/:locale` (`/en/x`), or another route answers its address with a method they share, or a route registered after an address that starts with a parameter (`/:username`) has a fixed path (`/login`) and a method that address answers too, and no route before it answers that path with that method. [Routes under a locale](routing.md#routes-under-a-locale), [the default locale without a prefix](routing.md#the-default-locale-without-a-prefix) |
| H10 | `router`, `server` | A route marked with `.sitemap()` cannot be listed: it is not a `GET` route, is `.internal()` or an API route, sits behind a middleware that adds `user` or does not say `authorize(everyone)`, has parameters but no `sitemap()` method on its controller, or `APP_URL` is not an `http` or `https` address. Also the warning that a sitemap was cut at 50 000 URLs. [Sitemap and robots.txt](seo.md#sitemap-and-robotstxt) |
| H11 | `router` | A route binds a parameter with `scopedBy` and a binder that does not declare `static readonly routeBindingScoped = true`: it may ignore `binding.scope` and find a record of another parent. Declare the mark on a binder that reads `binding.scope`. [Your own binder](routing.md#your-own-binder) |
| H12 | `http` | A webhook sender cannot work: its name is defined twice, it has no secret, a secret is too short or not in the scheme's format, the scheme is unknown, or the tolerance is not between 1 second and 30 minutes. Also at start: a route uses `verifyWebhook()` with a name nobody defined. [Defining a sender](webhooks.md#defining-a-sender) |
| H13 | `router` | A route pattern the router cannot read the same way everywhere: a parameter name that is not made of letters, digits and `_` (`/users/:user-id`), a name used twice, a constraint that is never closed or names a group, text right after a parameter in its segment (`/a/:x.json`), or an optional parameter that is not last (`/a/:x?/b`). Each message names the pattern and the case; rename or restructure the parameter. [Parameter names](routing.md#parameter-names) |
| H14 | `core` | The app's package.json cannot be read, or its `"marmeon"."serverModules"` holds something that is no glob of the app's files: invalid JSON, a misspelt key, a `"serverModules"` that is not a list, or an entry the build and the lint preset would read differently: a leading `/` or `#`, a space at either end, braces, parentheses, a pipe, a question mark, square brackets, a `.` or `..` segment, or a folder name with no `*` or file extension. The server-import check and the lint preset cannot know the app's server folders until it is fixed. [Server folders of your own](vite.md#server-folders-of-your-own) |
| H15 | `router`, `server` | Two routes have the same method and path, or a rest route (`/docs/:page*`) registered first takes the path of a later one: the later route never answers for the methods they share (it still answers the others). A warning of `marmeon check` and, in development, once at start; the app starts either way. The message names both routes and where each was registered, and lists the methods that never reach the later route: change those methods or the path of one of the two. At start, a [fallback route](routing.md#the-fallback-route) that cannot work stops the app with the same code: a second fallback for a prefix, a fallback outside the `web` group (also one loaded without a group), one under a prefix with a parameter (the locale of `Route.localized().fallback()` is the one parameter allowed), a localized fallback and one without a locale on the same prefix (the localized one also takes the address without a locale), and a `GET` route that has the form of a fallback or takes the rest of the path before it (also `/:page{.+}` and the address without a locale of a localized rest route, which answers, or redirects while no locale is without a prefix), so the fallback would never answer. The message names both places. [Route order](routing.md#route-order), [The fallback route](routing.md#the-fallback-route) |
| H16 | `server` | A package's document cannot be served. At start: the build's `.vite/marmeon-entries.json` has a format this server does not read, names a namespace that is none, or lists a client entry or page that `.vite/manifest.json` lacks, or `dist/server/ssr-<namespace>.js` is missing or exports no `render`. Run `marmeon build` again with the same version of Marmeon. While rendering, in an app with a package entry: a page's name has a namespace (`nope::Index`) that has no entry; the response is a 500, never the app's document. Fix the namespace in the view's name. [Their document](pages.md#their-document) |
| D1 | `database` | The database's record of migrations (`migrations`) or the migration lock (`migration_lock`) has other columns than this release reads, or there is no record next to tables of the app (a `MigrationStateError`, `newer` or `unmigrated`; `marmeon migrate` writes it for your own database), or a connection is set to `managed: false` and Marmeon migrates it (`unmanaged`). Every query on the connection fails. [How the migrator keeps track](migrations.md#how-the-migrator-keeps-track) |
| D2 | `database` | The database lacks migrations the code has (a `MigrationStateError`, `pending`): the app started before `marmeon migrate`, or after a rollback (a process that was ready before it keeps its schema until it restarts). Every query on the connection fails and `/up/ready` is red until the migrations ran; the app answers within a second of `marmeon migrate`, with no restart. The record in the form Marmeon 0.3 wrote, without a checksum per migration, is refused the same way until `marmeon migrate` carried it over. [Migrating a running app](migrations.md#migrating-a-running-app), [How the migrator keeps track](migrations.md#how-the-migrator-keeps-track) |
| D3 | `database` | The database does not fit the schema the migrations describe after a migration or a `down()`: a table or column the migrations say is there is gone (raw SQL renamed or dropped it), so the migration was rolled back (not on MySQL, see [MySQL](migrations.md#mysql)). Use `schema.alter()` and `schema.rename()`. On Postgres a table or column name over 63 bytes of UTF-8 can be the cause, since Postgres can cut it: the message names the first such missing name as a possible cause. Also: the migrations run in an order their names do not give, and it changes a table; rename the pending migration. [What `migrate` checks](migrations.md#what-migrate-checks-after-each-migration) |
| D4 | `database` | A migration changed `migrations` or `migration_lock`, which belong to the migrator, or a `schema.data()` step changed the schema (on MySQL also a statement that ends the transaction of the step, such as `commit`). [How the migrator keeps track](migrations.md#how-the-migrator-keeps-track), [Changing the data](migrations.md#changing-the-data) |
| D5 | `database` | A name that Postgres would cut at 63 bytes of the database's encoding without a word, or that the encoding cannot hold. Postgres itself is asked, so the same name can fit in LATIN1 and not in UTF8 or EUC_JP. Names it covers: an index or a check constraint (`oneOf()`, `check()`) a migration creates, or the constraint `{table}_{column}_fkey` of `foreign()`, or the new name of an index, constraint or sequence when a table is renamed with `indexes: true`. A table, column or index that `drop()`, `dropIfExists()`, `dropColumn()`, `dropIndex()` or `dropUnique()` names, which Postgres would take for the one its first 63 bytes name, is refused before the first statement, in an `up()` and in a `down()`. In a `down()` the same goes for `rename()`, `renameColumn()`, `dropCheck()`, `alter()` of such a table, `notNull()` and `nullable()`; in an `up()` those end in D19 (and `oneOf()` of such a column in both). An earlier step of the same plan that created the name, renamed a table or column to it, or added it exempts it. A migration that created such a name under a longer spelling must be edited to the name Postgres stored, then drops by that name run; editing a migration that already ran only shows the ["changed since it ran" warning](migrations.md#a-migration-that-changed-after-it-ran) and changes nothing in the database. The table and column a foreign key points at, and the columns of an index or its `where`, are asked too, in an `up()` and a `down()`. A step on a name that Postgres cuts to the same name as one the plan made withdraws that exemption; for a qualified table name the schema must match too, and a name without a schema matches every schema. For a qualified table name each part is asked. On MySQL: a name over 64 characters (counted as characters) of an index, check, foreign key, table or column. Pass the index or check a name (for `oneOf()`, use a shorter table or column name, or `check(name, …)`; for `foreign()`, which has no name option, use a shorter table or column name): in a new migration one of your own, in an existing one the name Postgres stored (`select indexname from pg_indexes where tablename = '…'`), so that a later `dropIndex` finds it. [Migrations](migrations.md#indexes), [Check constraints](migrations.md#check-constraints) |
| D6 | `database` | The framework's tables lack what the installed release needs (a column, or a table): a migration is missing. Run `marmeon publish`, which copies the migrations of your packages into the app, then `marmeon migrate`. [Publishing the migrations of a package](migrations.md#publishing-the-migrations-of-a-package) |
| D7 | `database` | `bootstrap/database.d.ts` was generated by another version of Marmeon: `marmeon db:types` writes it again; a file of a newer version needs `@marmeon/*` updated. [Table types](queries.md#table-types) |
| D8 | `database` | The migration lock is held by another process and `--wait` ran out. Wait for it, or raise `--wait`. [One migration at a time](migrations.md#one-migration-at-a-time) |
| D9 | `database` | A secret column would leave the database: a query selects it without `secret()`, reads a whole row of its table, a finder compares it, or a relation would load it. [Secret columns](migrations.md#secret-columns) |
| D10 | `database` | The secret guard cannot check a construct (an unknown statement, a function name that is no identifier, raw SQL in `FROM`, names that differ only in case) and refuses it. [Secret columns](migrations.md#secret-columns) |
| D11 | `database` | There is no connection to the database: it is not configured, its SQLite directory is missing, or it could not be opened: the host did not answer within the connect timeout (ten seconds, or `connect_timeout` in `DB_URL`), or a full pool had no free connection within it (raise `DB_POOL_MAX` or shorten transactions). [Configuration](database.md#configuration) |
| D12 | `database` | A query that the schema resolver cannot rewrite: a soft delete with parts an update cannot keep (`orderBy`, `limit`, a join, an alias, a second table), a relation or count name the query selects already, or a plugin that changed the tables a statement writes. [Module boundaries](queries.md#module-boundaries) |
| D13 | `database` | A seeder or a module that is not there. [Running seeders](seeding.md#running-seeders) |
| D14 | `database` | A migration file is placed wrongly: its directory is missing, its name is not `<timestamp>_<name>.ts`, it has no `export default defineMigration(…)`, its `up()` or `down()` returns a promise (they build a plan and are not `async`), there are more than 2000 entries below the migrations directory of a module, or it runs on a connection that is not configured. It is also the error when a migration file changed while `migrate` read it: nothing ran, so run the command again. [Migration structure](migrations.md#migration-structure) |
| D15 | `database` | Two migrations have the same name. [Migration structure](migrations.md#migration-structure) |
| D16 | `database` | A migration cannot be rolled back: its file is gone, or it has no `down()`. [Running migrations](migrations.md#running-migrations) |
| D17 | `database` | A package listed an invalid `static migrationUpgrades` entry. The code is retired: a package appends migrations now instead. [Publishing the migrations of a package](migrations.md#publishing-the-migrations-of-a-package) |
| D18 | `database` | A `schema.alter()` that SQLite cannot do in place: making a column NOT NULL or nullable (`table.notNull()`, `table.nullable()`), adding a column with a generated UUIDv7 default (MySQL refuses that too), `table.check()`, `table.dropCheck()` and `table.oneOf()`, which SQLite cannot do on an existing table, or `table.foreign()`, which cannot add a foreign key to an existing column. Also on MySQL: a `schema.raw()` step with a statement per driver and none for `mysql` (`mysql: null` says nothing runs there), and what MySQL cannot do, refused before the migration's first statement: a partial index, `notNull()` or `nullable()`, a `json()`, `binary()` or earlier `text()` column in an index, an index over 3072 bytes or 16 columns, a rename with `indexes: true` of a table whose foreign keys or checks start with the old name, `dropColumn()` of a column a foreign key holds, `renameColumn()` of a column a check starts with, `schema.drop()` of a table a foreign key references, `dropIndex()` of the last index a foreign key stands on, and a rename with `indexes: true` that gives an index the name another index has. [What MySQL refuses](migrations.md#what-mysql-refuses-before-a-migration-runs), [Changing the data](migrations.md#changing-the-data), [Raw SQL](migrations.md#raw-sql), [UUID keys](migrations.md#uuid-keys), [Check constraints](migrations.md#check-constraints), [A reference that comes later](migrations.md#a-reference-that-comes-later) |
| D19 | `database` | A migration's plan cannot be described: it creates a table name a second time without dropping it, adds a column the table has already, or changes a table or column (`schema.alter()`, `schema.rename()`, `renameColumn()`, `notNull()`, `nullable()`, an index) that no migration created with the blueprint. Also: a column in the `where` of a partial index that the table does not have, or a value there of a kind the drivers read differently for that column, and `retire()` of a column such a `where` names. Also: a change of a column that was retired (only `dropColumn()` works), and `retire()` of a primary key, of a column of a timestamps or soft-delete mark, of one a foreign key references, of a `NOT NULL` column without a default, or of a column with a fixed default that is in a unique index, partial or not; and a foreign key (`constrained()`) that points at a retired column. Also: `dropCheck()` of a constraint no migration made with `oneOf()` or `check()`, and a check constraint name used twice on one table. Also: `table.foreign()` of a column that does not exist or already holds a foreign key, to a table or column that does not exist, or to a column that holds another kind of value. Also: `table.oneOf(column, values)` of a column that does not exist, was retired, or is no `string()` or `text()` column, or whose constraint name another one holds. Create it with `schema.create()` (a table named with a dot cannot be created that way: see D31); a change in raw SQL keeps no secret, soft-delete or timestamps mark. [What the migrations describe](migrations.md#what-the-migrations-describe), [retiring a column](migrations.md#retiring-a-column), [a reference that comes later](migrations.md#a-reference-that-comes-later) |
| D20 | `database` | A module reads its migrations from a place your repository does not hold: a directory in `node_modules` (by its real path, so a symlink into it counts too), or one outside the app and its workspace. This applies to the directory, each file and every entry below it. An update of the package could change a migration your database has run, and nothing would show in your app's history. Move the files, with the same names, into a module of your app. Packages of your own monorepo, with the lockfile and `pnpm-workspace.yaml` or `workspaces` in its root, are fine, as long as neither the root nor a folder between it and the app is writable by everyone or owned by a user other than you, root or the owner of your app. [Migrations from a package](migrations.md#migrations-from-a-package) |
| D21 | `database` | A package declares the migrations it ships wrongly. Either the `static migrations` of a service provider is no object with only `from` and `module`, or `from` is no `file:` URL (or has a host, query or fragment). Or two providers declare migrations for one package, or `module` is no kebab-case name. Or `from` does not exist, is no directory, is the package itself or leads out of the package (by a symlink, or into a `node_modules` inside it). Or a file in it leads out of the package or is no regular file, a `.ts` file is not named `<timestamp>_<name>.ts`, the same name stands under two timestamps, there are more than 100 files, a file is unreadable, larger than 1 MiB, not UTF-8 or has no default export, or the directory holds no migration. Fix the declaration. [Declaring the migrations a package ships](package-tables.md#declaring-the-migrations-a-package-ships) |
| D22 | `database` | A migration lost its lock while it ran: its database connection ended (Postgres, MySQL) or another migration took the lock over (SQLite). The run stopped before the next migration. Look at the database before you migrate again: see which migrations ran with `marmeon migrate:status`. [One migration at a time](migrations.md#one-migration-at-a-time) |
| D23 | `database` | Two `defineConnection()` definitions in the application's or its modules' `config` have the same name. Rename one of them. [More connections](database.md#more-connections) |
| D24 | `database` | On Postgres, a statement failed inside a transaction or savepoint and the code caught the error inside it. Postgres aborted that level, so nothing in it was committed: it rolled back and threw this error instead of reporting a commit, and its after-commit work did not run. Catch the error outside the nested `transaction()`, or use `tryInsert()`. The same error follows when a statement you started inside the callback and did not await fails after the callback returned: Postgres rolled the transaction back instead of committing it (a nested one: only its savepoint, and the transaction around it goes on), and `cause` is the database's answer to the commit or to the release of the savepoint. Await every statement before the callback returns ([Nested transactions](transactions.md#nested-transactions)). The error also follows when a `db.transaction()` or `db.startTransaction()` opened inside the callback was never closed, or when a connection is released with its transaction still open: Marmeon rolled the transaction back (a nested one: only it), and the operation that released the connection rejects with this error instead of its own. The usual cause is that Kysely does not roll back a `db.transaction()` whose callback throws an `AbortedTransactionError`, even when you catch it outside the callback. Wrap that error before it leaves the callback (`new Error(message, { cause })`), or open nested transactions with `connections.transaction()`. On MySQL, the transaction was ended by the server or by a statement of the code: a deadlock, a lock wait timeout or a full lock table (the server rolled it back, or may have), or a statement that commits or rolls back by itself (a schema change written as raw SQL, `lock tables`, a `commit`, `begin` or `rollback` of your own, also with `and chain`). Every later statement and the commit are refused without being sent, because they would run outside any transaction; only the rollback of the transaction itself goes out, and a `rollback to` of a savepoint passes. After a deadlock, a lock wait timeout or a full lock table, let the error leave the callback so that the transaction rolls back, and run it again. After a rollback of your own nothing was committed; after a statement of yours that ended it (a commit, begin, schema change or lock statement), or when the cause is not known, first check what is committed already: what ran before that statement is. [Nested transactions](transactions.md#nested-transactions), [On MySQL](transactions.md#on-mysql) |
| D25 | `database` | A package declares the files it publishes wrongly. Either the `static publishes` of a service provider is no object with only `lang`, or `lang` is no `file:` URL (or has a host, query or fragment). Or its directory does not exist, is no directory, is the package itself or leads out of the package. Or a file in it is not named `<language tag>.ts`, leads out of the package or is no regular file, is unreadable, larger than 1 MiB, not UTF-8 or has no default export, there are more than 100 files, or the directory holds none. Fix the declaration. [Declaring the files a package publishes](package-tables.md#declaring-the-files-a-package-publishes) |
| D26 | `database` | The foreign keys of the migrations give relations that clash, so the table types cannot be generated: a foreign key column without `_id` (`uploaded_by`) would give a relation named like the column, two foreign keys to one table need `.inverse()` names, or a relation would take a name twice (also when a table of two unique foreign keys is read as a pivot). `migrate` and `migrate:fresh` in development refuse before they apply or drop anything; `db:types` names it too. Rename the column (`uploaded_by_id`) or name the relation; the message says which. A table with two unique foreign keys that is no pivot says so with `table.notPivot()`. [Relations from foreign keys](migrations.md#relations-from-foreign-keys) |
| D27 | `database` | A package changed a migration after your app published it: the file in the package no longer matches the hash `marmeon publish` recorded in `marmeon-stubs.json`, while your database ran the version you published. `marmeon check` reports it as a warning and `marmeon publish` refuses with it, writing nothing. Compare the package's file with your copy, write a new migration for the difference, then run `marmeon publish <package> --accept` to record the current file. Your copy is not touched, and it may lie in any module. [Publishing the migrations of a package](migrations.md#publishing-the-migrations-of-a-package) |
| D28 | `database` | A MySQL connection was refused because the server or its session is not one Marmeon supports, and the connection was closed: MariaDB, TiDB or Vitess; a MySQL older than 8.4; a version string of a form Marmeon does not know (it is quoted in the message); or a session that does not have what Marmeon sets on every connection (the `sql_mode`, the UTC time zone, the `utf8mb4_0900_bin` collation, the `utf8mb4` client character set, autocommit, `completion_type = NO_CHAIN`, and a `session_track_system_variables` that reports every variable listed in [MySQL](database.md#mysql)), as when a proxy rewrites the session or an `init_connect` of the server switches autocommit off. The session is read when a connection is opened. A connection that goes back to the pool is reset (what a lease left on it, an open transaction included, goes away), set up again and read in full; a failure of that, or a database other than the one it opened in, closes the connection. Inside a lease, an answer that says that the session has `NO_BACKSLASH_ESCAPES`, or that names another client character set than `utf8mb4`, refuses every later statement of the lease with this error before anything is sent. So does an answer that says that `autocommit` is now OFF, or that `completion_type` is no longer `NO_CHAIN` (the next statement, a commit included, is refused before it goes out, so nothing is committed). Should the commit or rollback of a transaction still answer with a transaction left open, the error says that it ran. Do not change `autocommit` or `completion_type` inside a lease; see [Transactions on MySQL](transactions.md#on-mysql). For the cases at connection time, run MySQL 8.4 or newer and remove what rewrites the session. [MySQL](database.md#mysql) |
| D29 | `database` | A connection uses a database driver that a package's database store does not run on, or a connection definition names a driver that is neither `sqlite` nor `pgsql`. The app's connections are refused too when one of them (the default, or a definition) has the driver `mysql`: MySQL cannot be selected yet. For a package's store, the message names the feature (`CACHE_DRIVER=database`, `SESSION_DRIVER=database`, `QUEUE_DRIVER=database`, `QUEUE_FAILED_DRIVER=database`, `LIVE_DRIVER=database`, the stores of authentication, the notifications table, uploads, or a test app with a database), the drivers it runs on and the driver it found. The check runs before the first statement. At the start, for a setting that surely uses the database. On first use, for uploads and for the failed-job store of a `sync` queue. When a test app prepares its database. Where the message names an alternative, use it; otherwise point the feature at a sqlite or pgsql connection. [Configuring a connection](database.md#configuration) |
| D30 | `database` | On MySQL, a schema change of the builder (`createTable()`, `alterTable()`, `dropTable()`, `createIndex()`, `dropIndex()`, a view or a schema) ran inside a transaction, a savepoint or `isolate()`. MySQL commits the transaction at a schema change, so the statement is refused before it is sent and nothing of it runs; a temporary table is allowed. Make the schema change outside the transaction. [On MySQL](transactions.md#on-mysql) |
| D31 | `database` | In an `up()`, `schema.create()` or the new name of `schema.rename()` has a dot in it: a name with a dot names a table together with its schema or database, and the migrations do not describe such a table. The call is refused before any statement runs. Use a name without a dot. `drop()` and `dropIfExists()` still accept one (a table of another tool); `alter()` and the old name of `rename()` of such a table end in D19 in an `up()`. A `down()` is not checked against the migrations and accepts a dotted name in `create()`, `alter()` and `rename()`, except a `oneOf()` change of such a table (D19). [What the migrations describe](migrations.md#what-the-migrations-describe) |
| A1 | `auth` | Passkeys cannot be used: `APP_URL` or the passkey variables name no safe relying party. [Requirements](passkeys.md#requirements) |
| A2 | `auth` | The hasher is set up with values it refuses, is given a password it will not hash (malformed, over the length limit), or asks for argon2id on a Node without `crypto.argon2`. [Limits](hashing.md#limits) |
| A3 | `session` | Sessions are not set up: a driver whose package or provider is missing. [Drivers](session.md#drivers) |
| A4 | `encryption` | A payload did not decrypt: changed, from another key or for another context. [Encrypting and decrypting](encryption.md#encrypting-and-decrypting) |
| A5 | `auth` | A `UserProvider` breaks its contract: `updatePasswordHash()` or `updateSessionKey()` does not answer with a boolean (or `updatePasswordHash()` answers `false` without `expected`), `updateSessionKey()` / `updateEverySessionKey()` is missing (JavaScript, a cast), `updateSessionKey(id, k, k)` is refused for the stored key, a new key is not handed back by `findById()` (also the one a new password passes to `updatePasswordHash()` as `sessionKey`), `findForSignIn()` and `findById()` disagree on `sessionKey`, a provider still has the old method name `findForLogin()` instead of `findForSignIn()`, or `updateEverySessionKey()` returns no count or no `sampleId`. [The user type](authentication.md#the-user-type-and-where-users-come-from), [for your own user provider](authentication.md#for-your-own-user-provider) |
| A6 | `auth` | A store or hasher breaks its contract (JavaScript, a cast; TypeScript refuses to compile most of it): a `PasswordResetTokenStore` without `forgetEveryone()`, an `AccessTokenStore` without `revokeEveryone()`, an `EmailChangeStore` without `forget()` or `forgetEveryone()` (found before anything is written or ended), a `SignInStore` without `endAll()`, `dropToken()` or `endEveryone()` (found before anything is ended), a source of `TwoFactorState` without `factors()`, `enabledSince()` or `removeAll()` (found when `combineFactors()` is called and before a reset token is spent), or a `Hasher` without `failureFloorMs()` or with one that answers anything but a finite number of milliseconds, 0 or more. [What your app implements](authentication.md#what-your-app-implements), [hashing](hashing.md#making-and-checking-hashes) |
| A7 | `auth` | Signing in needs a rate limiter and none is bound: the provider of `@marmeon/cache` is not registered, while a `UserProvider` is bound or a package registered a guard. [How password checks are counted](authentication.md#how-password-checks-are-counted) |
| A8 | `auth` | A guard of a package is declared wrongly: its name is not lower case letters, digits and single hyphens (at most 32, starting with a letter) or is `web`, `api`, `app`, `auth` or `marmeon`, its table prefix is not lower case with a trailing underscore (at most 32), `users` is not a class, a setting is outside its limits or does not exist, or a second guard has the name or the table prefix of one registered, or a guard is registered after the auth provider has booted (for instance in a later provider's `boot()`): register it in the provider's `register()`. Also a policy whose `static guard` is no guard registered in the app (a name, or the definition of a guard nobody registered); the gate refuses it the first time the policy is asked. And a guard slot: one with an invalid name, bound twice, bound to something other than `'web'` or a guard, bound to a guard without `twoFactor` when the slot offers two-factor middleware, bound to a guard no provider registered (at boot), never bound while a policy, its `Auth`, its middleware, a route or a test that signs in through it uses it, one with listeners that is unbound or bound to a guard no provider registered (refused at the start, and reported by `marmeon check`), or one whose middleware the bound guard refuses. [Guards of a package](authentication.md#guards-of-a-package), [Using the app's users instead](authentication.md#using-the-apps-users-instead), [Policies of a guard](authorization.md#policies-of-a-guard) |
| A9 | `auth` | A route mixes guards. At start: the middleware of one route name two guards that cannot share it (a package guard's `guest()` or `authenticate()` and the app's `authenticate()`, or two package guards); `marmeon check` reports it as well. At run time: a middleware that acts for one guard runs on a route that belongs to another (the app's `confirmedPassword()`, `twoFactor()`, `confirmedSecondFactor()` or `verified()` on a route of a package guard; a package guard's `confirmedPassword()` on a route of the app, or on one where its own `authenticate()` has not run before it), or a guard's middleware that the router does not see (called by another middleware) runs after a policy decided on the request for another guard. The response is a 500; the log names both guards. In development, a warning with this code says that a policy of one guard was asked on a route of another and answered no, once per policy and route guard; its message names the policy, the gate method and both guards. [Protecting the routes](authentication.md#protecting-the-routes), [Policies of a guard](authorization.md#policies-of-a-guard), [Middleware that asks a policy](authorization.md#middleware-that-asks-a-policy) |
| A10 | `oauth` | `OAuth.redirect()` or `OAuth.callback()` cannot run a flow, and throws an `OAuthSetupError`: `SESSION_SAME_SITE=strict` (`redirect()` only: the browser would not send the session cookie when it comes back from the provider), no session package, a session that is not started or that this request may not write (`OAuth.callback()` answers `invalid` there instead), an active provider whose callback address (`OAUTH_<NAME>_REDIRECT_URL` or `APP_URL`) is missing or not usable, or code of a provider that asks `http` for an address that is not usable, in `endpoints()` or `profile()` (`https:` only; plain `http:` on a loopback host in development and test; no user name or fragment). A discovery document of an `oidc()` provider that names another issuer than the configured one is this error as well (the message names both). A provider's discovery that gives no usable answer, or endpoints that are not usable, is `unavailable`, not this error. `SocialUserProvider` is not bound when a sign-in needs to find or make a user, or when `link()` checks the user's address, is this error as well. [The flow and its security](oauth.md#the-flow-and-its-security), [SameSite](oauth.md#samesite) |
| A11 | `oauth` | A social login provider is registered wrongly, and the start throws an `OAuthSetupError`: a name that is not lower case letters, digits and dashes (at most 32), brought twice, or an `oauthProviders` that is not an object of functions; a class that brings a name without `oauthCredentials(name)` in its own `static config`, lists it twice, or two credentials configurations that read one variable; an active provider with a wrong definition (label, endpoints, scopes, a field of the wrong type or unknown, `authorizeParams` or an `authorizeUrl` query that set a parameter the flow owns, `profile` or `mapUser`), an endpoint, `OAUTH_<NAME>_REDIRECT_URL` or `APP_URL` that is not `https:`, or an active provider with neither a redirect address nor `APP_URL` (plain `http:` is accepted on a loopback host in development and test only), an `allowAddresses` entry that is no address range, or `OAUTH_<NAME>_SCOPES` with a space in a scope; an active `oidc()` provider with no issuer (neither in `oidc()` nor in `OAUTH_<NAME>_ISSUER`; with `issuer: 'required'` such a provider is inactive instead) or with one in both places, or an issuer or `discoveryUrl` that is not a usable address; an active `microsoft` provider whose `OAUTH_MICROSOFT_TENANT` is neither a keyword nor a tenant ID ([built-in providers](oauth.md#the-built-in-providers)); or an active provider without a bound `HttpClient`. [Starting the app](oauth.md#starting-the-app), [your own provider](oauth.md#your-own-provider) |
| A12 | `auth` | A re-authentication or a password reset hook is claimed wrongly: its name is not lower case letters, digits and hyphens (at most 32, starting with a letter), a name is claimed a second time in the same container, a claim is made on a request's scope instead of the app's container, or a claim comes after the auth provider has booted: claim it in a provider's `register()`. [Opening the confirmation window](oauth.md#opening-the-confirmation-window) |
| Q1 | `queue` | A job record has no version (it was queued by an earlier release) and is moved to the failed jobs. [Deploying while jobs wait](queues.md#deploying-while-jobs-wait) |
| Q2 | `queue` | A job no worker could read within six hours is moved to the failed jobs. [Deploying while jobs wait](queues.md#deploying-while-jobs-wait) |
| Q3 | `queue` | A queued listener has no `static listener`, the stable name its jobs carry. [Queued listeners](events.md#queued-listeners) |
| Q4 | `queue` | Queues are not set up: a job class or a queued listener without what it needs (an event without `static event` and `static schema`, a setting it does not take), a connection or driver that is missing or breaks its contract, or a retry window a job outgrows. [Connections and drivers](queues.md#connections-and-drivers) |
| Q5 | `scheduler` | A schedule cannot start: a cron expression, time, time zone or task name that is invalid, a task without a frequency, a command or job the app does not have, or a package or provider the scheduler needs missing. [Frequencies](scheduling.md#frequencies) |
| Q6 | `notifications` | Notifications are not set up: a class without a name, schema or `via()`, a name or channel taken twice, or a package or provider missing. [Defining notifications](notifications.md#defining-notifications) |
| Q7 | `mail` | An MJML template is invalid (also when a component would load an image from a host of someone else, such as `mj-social-element` without `src`), or the `mjml` package is not installed. [Writing mails](mail.md#writing-mails) |
| Q8 | `mail` | The mail driver cannot be set up: `MAIL_DRIVER` names a transport nobody brought, or a provider's factory returns something without `send()`. [Mail transports](drivers.md#mail-transports) |
| S1 | `cache` | The cache is not set up: a driver's package or provider is missing, the driver is unknown, or a custom driver breaks its contract. [Drivers](cache.md#drivers) |
| S2 | `storage` | Storage is not set up: a disk, its driver, its visibility or its URL is configured wrongly, or the AWS SDK or another package is missing, or a body schema holds an `upload()` in a list, a union, an optional object or `.when()` fields. [Configuration](storage.md#configuration), [Uploads](storage.md#uploads) |
| S3 | `storage` | A storage key a disk refuses: `..`, an absolute path, a backslash, a control character or a symlink out of the disk's root. [Keys](storage.md#keys) |
| S4 | `otel` | The OpenTelemetry API is not installed, or is not version 1. [Installation](observability.md#installation) |
| S5 | `i18n` | A message with a syntax error, such as an unclosed placeholder. [Messages](localization.md#messages) |
| S6 | `i18n` | A file in `lang/marmeon/` or in `lang/<namespace>/` that names a key the texts it rewords do not have, or a text that drops a placeholder of theirs. [The framework's texts](localization.md#the-frameworks-texts), [Texts of a package](localization.md#texts-of-a-package) |
| S7 | `i18n` | A package with texts and a module of the app (`modules/<namespace>/lang/`) both have texts under one namespace. [Texts of a package](localization.md#texts-of-a-package) |
| S9 | `fetch` | Outgoing requests are not set up: an entry of `FETCH_ALLOWED_ADDRESSES` is not an address range, `FETCH_DRIVER` names a transport nobody brought, or a provider's factory returns something without `send()`. [Fetch transports](drivers.md#fetch-transports) |
| S10 | `chat` | Chat messages are not set up: `CHAT_DRIVER` names a driver nobody brought, an entry of `CHAT_WEBHOOKS` is not `name=address`, repeats a name, or holds an address its driver does not accept, a provider's chat driver breaks its contract, or the `FetchServiceProvider` is missing. [Configuration](chat-notifications.md#configuration), [Chat drivers](drivers.md#chat-drivers) |

## Errors in the browser

What the browser does when something fails:

- **An error while a page renders** shows the app's `system/errors/Error` page (or `system/errors/500`, if there is one) inside its layouts, with details in development only. A
  layout that throws shows the page without layouts. The next visit tries again. A layout with `error` shows the failure of what
  is below it itself, inside the layout, with a `retry()` for a button: [When a page fails](pages.md#when-a-page-fails).
- **A failed visit** keeps the page and whatever was typed into it: offline, a 5xx, or an answer to a form that is no page (a
  proxy's 413 or 429, a JSON error). A server that cannot be reached shows a notice, and a 5xx or such an answer shows the
  server's error in a dialog over the page.
- **An island** fails on its own and shows its error, while the rest of the page stays.

The code that shows a failure loads when a failure happens, never with the first download. When it cannot load, a 5xx shows the
notice of an unreachable server, and a page that threw shows nothing in its place: a failure never takes the app down.

### Error codes

A mistake in your app's code, such as a hook outside the app, a page without a default export or a route the browser does not
know, throws an explanation in development: what is wrong and how to fix it. A production build carries only a code and a name,
such as `[marmeon:R2] users/Show`, so the explanations cost no download. Both start with the same code. Run the app in development
to read the whole message, or look the code up here.

The explanation comes only with `NODE_ENV` set to `development` or `test`. A build or a server in production, a staging server
included, gets the code alone.

| Code | Where | Meaning |
| --- | --- | --- |
| C1 | `resolveRoute` | A route name, but the app passed no route manifest. |
| C2 | `resolveRoute` | A route name the browser does not know. API routes, `.internal()` routes and `clientRoutes.except` never ship. |
| C3 | query state | The query state of a route, used on the page of another route. |
| C4 | query state | The query state of a route without a query schema. |
| C5 | `createPoll` | A poll interval that is not a number from above 0 up to 2147483647 ms. |
| C6 | layers | A layer depth outside 1 to 10. |
| C7 | layers | A hint in development: the server answered with a layer before any code loaded the layers. They load now. |
| C8 | layers | A warning in development: a deep link's document has layers, but they were not attached before the first render. |
| C9 | tables | A sort by a column the table does not offer as sortable. |
| C10 | tables | A filter the table does not offer, a value that is none of a select filter's options, or a search without a searchable column. |
| C11 | tables | A page the table does not have, a page size it does not offer, or a next page where there is none. |
| C12 | tables | A row action, a bulk action or an export the table does not offer to this user. |
| C13 | the navigator | A warning in development: the browser has no Navigation API, and the history fallback was not attached. |
| C14 | the navigator | A warning in development: a plain `GET` visit took more than a second with no placeholder and nothing fetched ahead — once per route and document. |
| R1 | pages | A page's `layout` is not a component or a list of them. |
| R2 | pages | No page of that name: no default export in `modules/<module>/views/…`, or for `namespace::View` no installed package with that namespace ships the view. |
| R3 | pages | A page module without a default export. |
| R4 | pages | A page's `ssr` is not `true`, `false` or `'complete'`. |
| R5 | pages | A page's `placeholder` is not a component. |
| R6 | pages | A page's `revalidateOnBack` is not `true`, `false` or a list of prop names. |
| R7 | `useAction` | An action submitted while rendering on the server. |
| R8 | `reloading({ islands })` | An island route the browser does not know. |
| R9 | hooks | A hook of the framework outside the app `createMarmeonApp()` renders. |
| R10 | `useFeature` | A client feature used while rendering on the server. |
| R11 | `useRouter` | A visit while rendering on the server. |
| R12 | translation hooks | A translation hook without the app's messages. |
| R13 | `<Head>` | `<Head>` inside an island. In production it renders nothing. |
| R14 | layers, islands | An optimistic update or a reload while rendering on the server. |
| R15 | `useIsland` | `useIsland()` outside the view of an island. |
| R16 | `createMarmeonApp` | `progress` is not an indicator: pass `progressBar({ delayMs })` or your own. |
| R17 | `<Pagination>` | A warning: the page's query schema has no field for the page number, so the links would lead to the first page. |
| R18 | `useTable` | A table changed while rendering on the server. |
| R19 | layouts | A layout's `error` that is no component. |
| R20 | `<Image priority>` | A warning in development: `priority` in an island cannot preload, because an island cannot reach the document head. |
| R21 | `<Island href>` | An `href` that is no path of this site: another origin, `//host`, a scheme, a backslash or a control character. In production the island shows its error and sends no request. |

Texts a user sees are never codes: the error pages, the questions before leaving a page. Neither are refusals a production log
needs to read, such as a request that would go to another site.

## Testing

A test asserts the status, and the page or JSON that came with it:

```ts title="modules/notes/errors.test.ts"
import { createTestApp, type TestApp } from '@marmeon/testing';
import { beforeEach, it } from 'vitest';
import application from '../../bootstrap/app.ts';
import { UserFactory } from '#modules/auth';
import { NoteRepository } from './NoteRepository.ts';

let app: TestApp;
beforeEach(async () => {
  app = await createTestApp(application, { database: 'refresh' });
});

it('answers 410 for an archived note', async () => {
  const ada = await app.factory(UserFactory).create();
  const note = await app.make(NoteRepository).insert({ user_id: ada.id, title: 'Old', body: '', archived_at: new Date().toISOString() });
  await app.actingAs(ada).expectingJson().get(`/notes/${note.id}`).assertStatus(410).assertJson({ message: 'This note was archived.' });
});
```

`assertNotFound()`, `assertForbidden()` and `assertUnprocessable()` check the common statuses. The [HTTP
tests](http-tests.md#status) page covers every assertion.
