Errors and types
Errors
The local API throws these classes. They come from tenoncms, and browser code can import them too.
| Class | Status | Thrown when |
|---|---|---|
ValidationError | 400 | Data or options are invalid |
ForbiddenError | 401, 403 | An access rule denies. 401 without a user, 403 with one |
ConflictError | 409 | Other documents block a delete, or the document changed since you read it |
LinkedContentError | 409 | A 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:
referenceslists the documents and globals with arestrictrelation to it, like{ collection: "pages", id: "a1" }. - From a write with a stale
updatedAt:codeis"CHANGED", andreferencesis 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" } }
| Error | Body |
|---|---|
ValidationError | message, errors |
ConflictError | message, references, and code for CHANGED |
LinkedContentError | message, code, references |
ForbiddenError | message |
| 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, ornull. Readbody.errors,body.codeandbody.referencesfrom it.message:body.message, orRequest 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.
| Type | What 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, Globals | Every collection or global, as HTTP sends them |
Register | The 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:
| Type | What it describes |
|---|---|
CmsOptions | The options of defineCms() |
Collection, Global, Block | One entry of collections, globals or blocks |
Field, Fields | One 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, File | A populated image or file field |
Api, LocalApi | The local API of defineCms(), and the untyped one hooks get |
CollectionHooks, GlobalHooks, RootHooks, MediaHooks | Hooks |
Access, Rule, Where, CmsUser | Access rules and the user they get |
Plugin, Task, TaskContext | Plugins and job tasks |
Storage, ImageProvider | The contracts of a media storage and an image provider |
EmailAdapter, EmailMessage | The contract of an email adapter |
Related resources
- Local API: the errors every method throws.
- HTTP routes: the status codes.