Errors
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.
error
Defines the expected failure
ctx.error
Creates the error to throw
caller.try
Switch on the code
toResponse
Maps kind to status
export const const minNights: ErrorDef<{
minNights: number;
nights: number;
}, {
minNights: number;
nights: number;
}, undefined>
minNights = const error: <z.ZodObject<{
minNights: z.ZodNumber;
nights: z.ZodNumber;
}, z.core.$strip>, undefined>(def?: {
id?: undefined;
kind?: Exclude<ErrorKind, "invalid">;
details?: z.ZodObject<{
minNights: z.ZodNumber;
nights: z.ZodNumber;
}, z.core.$strip> | undefined;
message?: string | ((details: {
minNights: number;
nights: number;
}) => string) | undefined;
meta?: Readonly<Record<string, unknown>>;
} | undefined) => ErrorDef<{
minNights: number;
nights: number;
}, {
minNights: number;
nights: number;
}, undefined>
An expected failure of this module: `export const minNights = error({ details: z.object({ min: z.number() }) })`,
raised with `throw ctx.error(minNights, { min: 3 })`. Its code is `"<module>.<export>"`. Export it from the
module's index, like an event, so the registry knows it.error({
details?: z.ZodObject<{
minNights: z.ZodNumber;
nights: z.ZodNumber;
}, z.core.$strip> | undefined
Its details' schema: checked when raised, and only what it keeps leaves your server.details: import zz.function object<{
minNights: z.ZodNumber;
nights: z.ZodNumber;
}>(shape?: {
minNights: z.ZodNumber;
nights: z.ZodNumber;
} | undefined, params?: string | {
error?: string | z.core.$ZodErrorMap<NonNullable<z.core.$ZodIssueInvalidType<unknown> | z.core.$ZodIssueUnrecognizedKeys>> | undefined;
message?: string | undefined | undefined;
} | undefined): z.ZodObject<{
minNights: z.ZodNumber;
nights: z.ZodNumber;
}, z.core.$strip>
object({ minNights: z.ZodNumberminNights: import zz.function number(params?: string | z.core.$ZodNumberParams): z.ZodNumbernumber(), nights: z.ZodNumbernights: import zz.function number(params?: string | z.core.$ZodNumberParams): z.ZodNumbernumber() }),
message?: string | ((details: {
minNights: number;
nights: number;
}) => string) | undefined
Its message: `"Stay at least $min nights"` takes `details.min`, or a function of the details. Default: a sentence for its kind ("Not found"), never the code.message: "Stay at least $minNights nights",
});
export const const priceStay: Fn<"query", {
nights: number;
}, {
total: number;
}, false, {
scope: {
actor: string | null;
};
}, z.ZodObject<{
nights: z.ZodNumber;
}, z.core.$strip>, undefined>
priceStay = const query: <z.ZodObject<{
nights: z.ZodNumber;
}, z.core.$strip>, undefined, {
total: number;
}, Policy<{
actor: string | null;
}>, never, undefined>(def: Def<{
scope: {
actor: string | null;
};
}, "query", z.ZodObject<{
nights: z.ZodNumber;
}, z.core.$strip>, undefined, {
total: number;
}, Policy<{
actor: string | null;
}>, never, undefined>) => Fn<"query", {
nights: number;
}, {
total: number;
}, false, {
scope: {
actor: string | null;
};
}, z.ZodObject<{
nights: z.ZodNumber;
}, z.core.$strip>, undefined>
Reads data. Can run other queries. No writes, no events, no services.query({
input?: z.ZodObject<{
nights: z.ZodNumber;
}, z.core.$strip> | undefined
What callers pass, validated on every call. Leave it out when the function takes nothing.input: import zz.function object<{
nights: z.ZodNumber;
}>(shape?: {
nights: z.ZodNumber;
} | undefined, params?: string | {
error?: string | z.core.$ZodErrorMap<NonNullable<z.core.$ZodIssueInvalidType<unknown> | z.core.$ZodIssueUnrecognizedKeys>> | undefined;
message?: string | undefined | undefined;
} | undefined): z.ZodObject<{
nights: z.ZodNumber;
}, z.core.$strip>
object({ nights: z.ZodNumbernights: import zz.function number(params?: string | z.core.$ZodNumberParams): z.ZodNumbernumber() }),
async handler: (ctx: QueryCtx<{
scope: {
actor: string | null;
};
}, {
actor: string | null;
}>, input: {
nights: number;
}) => Promise<{
total: number;
}>
handler(ctx: QueryCtx<{
scope: {
actor: string | null;
};
}, {
actor: string | null;
}>
ctx, { nights: numbernights }) {
if (nights: numbernights < 3) throw ctx: QueryCtx<{
scope: {
actor: string | null;
};
}, {
actor: string | null;
}>
ctx.BaseCtx<C extends Config, S>.error: RaiseError
<{
minNights: number;
nights: number;
}, {
minNights: number;
nights: number;
}, undefined>(error: ErrorDef<{
minNights: number;
nights: number;
}, {
minNights: number;
nights: number;
}, undefined>, details: NoInfer<{
minNights: number;
nights: number;
}>) => AppError<string, {
minNights: number;
nights: number;
}>
One of this module's errors, with its details (checked against its schema). Code: `"<module>.<export>"`.error(const minNights: ErrorDef<{
minNights: number;
nights: number;
}, {
minNights: number;
nights: number;
}, undefined>
minNights, { minNights: numberminNights: 3, nights: numbernights });
return { total: numbertotal: nights: numbernights * 120 };
},
});
const const registry: Registry<{
actor: string | null;
}, {
readonly pricing: {
readonly minNights: ErrorDef<{
minNights: number;
nights: number;
}, {
minNights: number;
nights: number;
}, undefined>;
readonly priceStay: Fn<"query", {
nights: number;
}, {
total: number;
}, false, {
scope: {
actor: string | null;
};
}, z.ZodObject<{
nights: z.ZodNumber;
}, z.core.$strip>, undefined>;
};
}, Record<string, never>, {}, AppError<"pricing.forbidden" | "pricing.priceStay.forbidden", undefined> | AppError<"pricing.priceStay.invalidInput", {
issues: {
message: string;
path?: PropertyKey[];
}[];
}> | AppError<...> | AppError<...>>
registry = const createRegistry: <{
readonly pricing: {
readonly minNights: ErrorDef<{
minNights: number;
nights: number;
}, {
minNights: number;
nights: number;
}, undefined>;
readonly priceStay: Fn<"query", {
nights: number;
}, {
total: number;
}, false, {
scope: {
actor: string | null;
};
}, z.ZodObject<{
nights: z.ZodNumber;
}, z.core.$strip>, undefined>;
};
}, {
actor: string | null;
}>(options: TypedOptions<{
scope: {
actor: string | null;
};
}, {
readonly pricing: {
readonly minNights: ErrorDef<{
minNights: number;
nights: number;
}, {
minNights: number;
nights: number;
}, undefined>;
readonly priceStay: Fn<"query", {
nights: number;
}, {
total: number;
}, false, {
scope: {
actor: string | null;
};
}, z.ZodObject<{
nights: z.ZodNumber;
}, z.core.$strip>, undefined>;
};
}, {
actor: string | null;
}>) => Registry<...>
Create the registry once at startup: `createRegistry({ modules, database, services })`.createRegistry({ modules: {
readonly pricing: {
readonly minNights: ErrorDef<{
minNights: number;
nights: number;
}, {
minNights: number;
nights: number;
}, undefined>;
readonly priceStay: Fn<"query", {
nights: number;
}, {
total: number;
}, false, {
scope: {
actor: string | null;
};
}, z.ZodObject<{
nights: z.ZodNumber;
}, z.core.$strip>, undefined>;
};
} & OwnModules<{
scope: {
actor: string | null;
};
}, {
readonly pricing: {
readonly minNights: ErrorDef<{
minNights: number;
nights: number;
}, {
minNights: number;
nights: number;
}, undefined>;
readonly priceStay: Fn<"query", {
nights: number;
}, {
total: number;
}, false, {
scope: {
actor: string | null;
};
}, z.ZodObject<{
nights: z.ZodNumber;
}, z.core.$strip>, undefined>;
};
}>
One entry per module: `{ bookings, availability }` with `import * as bookings from "./bookings"`.modules: { pricing: {
readonly minNights: ErrorDef<{
minNights: number;
nights: number;
}, {
minNights: number;
nights: number;
}, undefined>;
readonly priceStay: Fn<"query", {
nights: number;
}, {
total: number;
}, false, {
scope: {
actor: string | null;
};
}, z.ZodObject<{
nights: z.ZodNumber;
}, z.core.$strip>, undefined>;
} & {
readonly minNights: ErrorDef<{
minNights: number;
nights: number;
}, {
minNights: number;
nights: number;
}, undefined>;
readonly priceStay: Fn<"query", {
nights: number;
}, {
total: number;
}, false, {
scope: {
actor: string | null;
};
}, z.ZodObject<...>, undefined>;
}
pricing: { minNights: ErrorDef<{
minNights: number;
nights: number;
}, {
minNights: number;
nights: number;
}, undefined>
minNights, priceStay: Fn<"query", {
nights: number;
}, {
total: number;
}, false, {
scope: {
actor: string | null;
};
}, z.ZodObject<{
nights: z.ZodNumber;
}, z.core.$strip>, undefined>
priceStay } } });
const const caller: Caller<{
actor: string | null;
}, AppError<"pricing.forbidden" | "pricing.priceStay.forbidden", undefined> | AppError<"pricing.priceStay.invalidInput", {
issues: {
message: string;
path?: PropertyKey[];
}[];
}> | AppError<"pricing.notFound", undefined> | AppError<"pricing.minNights", {
minNights: number;
nights: number;
}>>
caller = const registry: Registry<{
actor: string | null;
}, {
readonly pricing: {
readonly minNights: ErrorDef<{
minNights: number;
nights: number;
}, {
minNights: number;
nights: number;
}, undefined>;
readonly priceStay: Fn<"query", {
nights: number;
}, {
total: number;
}, false, {
scope: {
actor: string | null;
};
}, z.ZodObject<{
nights: z.ZodNumber;
}, z.core.$strip>, undefined>;
};
}, Record<string, never>, {}, AppError<"pricing.forbidden" | "pricing.priceStay.forbidden", undefined> | AppError<"pricing.priceStay.invalidInput", {
issues: {
message: string;
path?: PropertyKey[];
}[];
}> | AppError<...> | AppError<...>>
registry.Registry<{ actor: string | null; }, { readonly pricing: { readonly minNights: ErrorDef<{ minNights: number; nights: number; }, { minNights: number; nights: number; }, undefined>; readonly priceStay: Fn<...>; }; }, Record<...>, {}, AppError<...> | ... 2 more ... | AppError<...>>.as(scope: {
actor: string | null;
}): Caller<{
actor: string | null;
}, AppError<"pricing.forbidden" | "pricing.priceStay.forbidden", undefined> | AppError<"pricing.priceStay.invalidInput", {
issues: {
message: string;
path?: PropertyKey[];
}[];
}> | AppError<"pricing.notFound", undefined> | AppError<"pricing.minNights", {
minNights: number;
nights: number;
}>>
Bind who is calling, once per request, job or test: `const caller = registry.as(scope)`.as({ actor: string | nullactor: null });
const const result: {
ok: false;
error: AppError<"pricing.forbidden" | "pricing.priceStay.forbidden", undefined> | AppError<"pricing.priceStay.invalidInput", {
issues: {
message: string;
path?: PropertyKey[];
}[];
}> | AppError<"pricing.notFound", undefined> | AppError<"pricing.minNights", {
minNights: number;
nights: number;
}>;
} | {
ok: true;
value: {
total: number;
};
}
result = await const caller: Caller<{
actor: string | null;
}, AppError<"pricing.forbidden" | "pricing.priceStay.forbidden", undefined> | AppError<"pricing.priceStay.invalidInput", {
issues: {
message: string;
path?: PropertyKey[];
}[];
}> | AppError<"pricing.notFound", undefined> | AppError<"pricing.minNights", {
minNights: number;
nights: number;
}>>
caller.Caller<{ actor: string | null; }, AppError<"pricing.forbidden" | "pricing.priceStay.forbidden", undefined> | AppError<"pricing.priceStay.invalidInput", { issues: { message: string; path?: PropertyKey[]; }[]; }> | AppError<...> | AppError<...>>.try<Fn<"query", {
nights: number;
}, {
total: number;
}, false, {
scope: {
actor: string | null;
};
}, z.ZodObject<{
nights: z.ZodNumber;
}, z.core.$strip>, undefined>>(fn: Fn<"query", {
nights: number;
}, {
total: number;
}, false, {
scope: {
actor: string | null;
};
}, z.ZodObject<{
nights: z.ZodNumber;
}, z.core.$strip>, undefined>, input: {
nights: number;
}, options?: RunOptions<{
actor: string | null;
}> | undefined): Promise<{
ok: false;
error: AppError<"pricing.forbidden" | "pricing.priceStay.forbidden", undefined> | AppError<...> | AppError<...> | AppError<...>;
} | {
...;
}>
Run it, and get a refusal back instead of a throw: `{ ok: true, value }` or `{ ok: false, error }`, the error typed
by its code (`switch (r.error.code)`). Only expected failures (`AppError`) come back; anything else still throws.
Handle what you need, and end with `default: throw r.error`, or a refusal you didn't handle is lost.try(const priceStay: Fn<"query", {
nights: number;
}, {
total: number;
}, false, {
scope: {
actor: string | null;
};
}, z.ZodObject<{
nights: z.ZodNumber;
}, z.core.$strip>, undefined>
priceStay, { nights: numbernights: 2 });
if (!const result: {
ok: false;
error: AppError<"pricing.forbidden" | "pricing.priceStay.forbidden", undefined> | AppError<"pricing.priceStay.invalidInput", {
issues: {
message: string;
path?: PropertyKey[];
}[];
}> | AppError<"pricing.notFound", undefined> | AppError<"pricing.minNights", {
minNights: number;
nights: number;
}>;
} | {
ok: true;
value: {
total: number;
};
}
result.ok: booleanok) {
switch (const result: {
ok: false;
error: AppError<"pricing.forbidden" | "pricing.priceStay.forbidden", undefined> | AppError<"pricing.priceStay.invalidInput", {
issues: {
message: string;
path?: PropertyKey[];
}[];
}> | AppError<"pricing.notFound", undefined> | AppError<"pricing.minNights", {
minNights: number;
nights: number;
}>;
}
result.error: AppError<"pricing.forbidden" | "pricing.priceStay.forbidden", undefined> | AppError<"pricing.priceStay.invalidInput", {
issues: {
message: string;
path?: PropertyKey[];
}[];
}> | AppError<"pricing.notFound", undefined> | AppError<"pricing.minNights", {
minNights: number;
nights: number;
}>
error.AppError<C extends string = string, D = unknown>.code: "pricing.forbidden" | "pricing.priceStay.forbidden" | "pricing.priceStay.invalidInput" | "pricing.notFound" | "pricing.minNights"code) {
case "pricing.minNights":
var console: Consoleconsole.Console.log(...data: any[]): voidThe **`console.log()`** static method outputs a message to the console.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static)log(const result: {
ok: false;
error: AppError<"pricing.forbidden" | "pricing.priceStay.forbidden", undefined> | AppError<"pricing.priceStay.invalidInput", {
issues: {
message: string;
path?: PropertyKey[];
}[];
}> | AppError<"pricing.notFound", undefined> | AppError<"pricing.minNights", {
minNights: number;
nights: number;
}>;
}
result.error: AppError<"pricing.minNights", {
minNights: number;
nights: number;
}>
error.AppError<"pricing.minNights", { minNights: number; nights: number; }>.details: {
minNights: number;
nights: number;
}
details.minNights: numberminNights);
break;
default:
throw const result: {
ok: false;
error: AppError<"pricing.forbidden" | "pricing.priceStay.forbidden", undefined> | AppError<"pricing.priceStay.invalidInput", {
issues: {
message: string;
path?: PropertyKey[];
}[];
}> | AppError<"pricing.notFound", undefined> | AppError<"pricing.minNights", {
minNights: number;
nights: number;
}>;
}
result.error: AppError<"pricing.forbidden" | "pricing.priceStay.forbidden", undefined> | AppError<"pricing.priceStay.invalidInput", {
issues: {
message: string;
path?: PropertyKey[];
}[];
}> | AppError<"pricing.notFound", undefined>
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.tryto handle an expected failure by code. Unexpected exceptions still throw. - Forward (3). Keep
default: throw result.error. Usecaller.runif you handle no cases. - Identity. Codes use
<module>.<export>or<module>.<id>. See reserved names. - 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. UseminNights.is(e)to narrow details. - Inspection. Use
isAppError(e)to check the class. - Kinds. See kinds, default messages, and statuses.
Catching inside logic
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. 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.