Openstatus
www.openstatus.dev
1import type { ZodError } from "zod";
2
3import { BaseError } from "./base-error";
4import type { ErrorCode } from "./error-code";
5import { parseZodErrorIssues } from "./utils";
6
7type Context = { raw: unknown };
8
9export class SchemaError extends BaseError<Context> {
10 public readonly name = SchemaError.name;
11 public readonly code: ErrorCode;
12
13 constructor(opts: {
14 code: ErrorCode;
15 message: string;
16 cause?: BaseError;
17 context?: Context;
18 }) {
19 super(opts);
20 this.code = opts.code;
21 }
22
23 static fromZod<T>(e: ZodError<T>, raw: unknown): SchemaError {
24 return new SchemaError({
25 code: "UNPROCESSABLE_ENTITY",
26 message: parseZodErrorIssues(e.issues),
27 context: { raw: JSON.stringify(raw) },
28 });
29 }
30}