createThrowableErrorMap
Turn an error map into the callable errors object that throws typed KizloErrors.
createThrowableErrorMap turns an error map into the callable errors object, where errors.CODE(options) returns
a KizloError ready to throw. Kizlo builds this for every procedure and passes it to your handler as errors, so
you rarely call it yourself.
Reach for it when a shared error map has to be thrown from somewhere that isn't a procedure handler, most often a
standalone middleware in its own file. A handler gets errors typed from its own procedure, but a reusable
middleware defined elsewhere doesn't know which procedure it's attached to, so it can't rely on that argument. Build
its own callable map from the same defineErrorMap the procedures
use, and both throw identical, typed errors.
Parameters
createThrowableErrorMap(errors)errors
Your error map: an object keyed by code, each value an
ErrorDefinition. Usually the result of
defineErrorMap.
Returns
A ThrowableErrorMap: the map you passed, merged with the built-in common HTTP errors.
Each key is a function:
errors.CODE(options?: { status?: number; message?: string; data?: ... }) => KizloErrorstatusandmessageoverride the map's defaults for this throw.datais required and typed when the code declares adataschema, and validated synchronously on the call (an invalid payload throws). Codes without a schema take optional, untypeddata.
Examples
Share one error map between a procedure and a middleware that live in different files.
Define the map once:
import { defineErrorMap } from "kizlo"
export const MEMBER_ERRORS = defineErrorMap({
NOT_A_MEMBER: { status: 403, message: "Members only" },
})A standalone middleware builds its own callable map from it. It can't use the handler's errors, since it
doesn't know which procedure it guards:
import { createMiddleware, createThrowableErrorMap } from "kizlo"
import { MEMBER_ERRORS } from "./error"
const errors = createThrowableErrorMap(MEMBER_ERRORS)
export const requireMember = createMiddleware(async ({ context, next }) => {
const user = await context.getAuthUser()
if (!user) throw errors.NOT_A_MEMBER()
return next({ context: { userId: user.id } })
})The procedure throws the same typed codes through its handler errors by passing the map to errors:
import { createProcedure } from "kizlo"
import { MEMBER_ERRORS } from "./error"
import { requireMember } from "./middleware"
createProcedure(
{ scope: "api", method: "GET", path: "/perks", /* …schemas… */ errors: MEMBER_ERRORS, middlewares: [requireMember] },
async ({ errors }) => {
throw errors.NOT_A_MEMBER() // same code, same type, raised in the handler
},
)errors.NOT_A_MEMBER() returns a KizloError with status 403. Common codes like errors.UNAUTHORIZED() are
always available on the built map too.