Reference

Errors and types

The errors Tenon throws, the error body over HTTP, and the types that describe your content.

Errors

The local API throws these classes. They come from tenoncms, and browser code can import them too.

ClassStatusThrown when
ValidationError400Data or options are invalid
ForbiddenError401, 403An access rule denies. 401 without a user, 403 with one
ConflictError409Other documents block a delete, or the document changed since you read it
LinkedContentError409A delete would break links in other documents

Any other error is a 500. The HTTP layer logs it and runs afterError.

import { ConflictError, ValidationError } from "tenoncms";

try {
  await cms.update("posts", id, { slug: "taken" }, { updatedAt });
} catch (error) {
  if (error instanceof ValidationError) console.log(error.errors.slug);
  if (error instanceof ConflictError && error.code === "CHANGED") console.log("Reload first");
}

ValidationError

new ValidationError(errors). errors holds one message per field path, like { "seo.title": "Required" }. The key "" is for the whole document. message is the first message.

ForbiddenError

new ForbiddenError(status). status is 401 or 403, and message is "Unauthorized" or "Forbidden".

ConflictError

new ConflictError(references?, message?, code?).

  • From a delete: references lists the documents and globals with a restrict relation to it, like { collection: "pages", id: "a1" }.
  • From a write with a stale updatedAt: code is "CHANGED", and references is empty.

LinkedContentError

new LinkedContentError(references), with code "LINKED_CONTENT". Each reference names the document or global, the field, and optionally the locale and draft that link to the deleted content. { allowBrokenLinks: true } deletes anyway.

The error body

A route answers an error with JSON. message is always there:

{ "message": "Already used", "errors": { "slug": "Already used" } }
ErrorBody
ValidationErrormessage, errors
ConflictErrormessage, references, and code for CHANGED
LinkedContentErrormessage, code, references
ForbiddenErrormessage
A failed CSRF check{ "message": "" } with 403
Anything else{ "message": "Internal Server Error" } with 500

Routes under /auth answer with the body of better-auth, like { "message": "...", "code": "INVALID_OTP" }.

CmsError

The client does not rebuild the classes above. It throws a CmsError from tenoncms/client for every failed request:

  • status: the HTTP status.
  • body: the parsed error body, or null. Read body.errors, body.code and body.references from it.
  • message: body.message, or Request failed (<status>).
import { CmsError } from "tenoncms/client";

try {
  await client.create("posts", data);
} catch (error) {
  if (error instanceof CmsError && error.status === 400) showErrors(error.body.errors);
}

Content types

These types turn your schema into the shape of a document. They come from tenoncms/client, so browser code can use them without the server.

TypeWhat it gives you
DocumentOf<K, O?>A document of collection K, as a read with the options O returns it, with populate and select
GlobalOf<K, O?>Global K, as getGlobal() with the options O returns it
BlockOf<V, T?>The items of a blocks value V, narrowed to the block types T
Collections, GlobalsEvery collection or global, as HTTP sends them
RegisterThe interface that tells the client about your cms
import type { DocumentOf } from "tenoncms/client";

type Post = DocumentOf<"posts", { populate: { author: true } }>;

The types know your schema once it is registered. Astro and Nuxt do this for you. Elsewhere, add tenoncms/register to types in tsconfig.json. See Types without an adapter.

Config types

tenoncms exports a type for every option and API. The ones you need most:

TypeWhat it describes
CmsOptionsThe options of defineCms()
Collection, Global, BlockOne entry of collections, globals or blocks
Field, FieldsOne field, and a record of fields
Data<F>, Doc<F>Stored data of fields F, and the same with id, timestamps, publishedAt and hasDraft
Image, FileA populated image or file field
Api, LocalApiThe local API of defineCms(), and the untyped one hooks get
CollectionHooks, GlobalHooks, RootHooks, MediaHooksHooks
Access, Rule, Where, CmsUserAccess rules and the user they get
Plugin, Task, TaskContextPlugins and job tasks
Storage, ImageProviderThe contracts of a media storage and an image provider
EmailAdapter, EmailMessageThe contract of an email adapter