mirror of
https://gitea.toothfairyai.com/ToothFairyAI/tf_code.git
synced 2026-04-01 14:52:25 +00:00
Co-authored-by: GitHub Action <action@github.com> Co-authored-by: Liang-Shih Lin <liangshihlin@proton.me> Co-authored-by: Dominik Engelhardt <dominikengelhardt@ymail.com> Co-authored-by: Jay V <air@live.ca> Co-authored-by: adamdottv <2363879+adamdottv@users.noreply.github.com>
58 lines
1.2 KiB
TypeScript
58 lines
1.2 KiB
TypeScript
import { z, type ZodSchema } from "zod"
|
|
// import { Log } from "./log"
|
|
|
|
// const log = Log.create()
|
|
|
|
export abstract class NamedError extends Error {
|
|
abstract schema(): ZodSchema
|
|
abstract toObject(): { name: string; data: any }
|
|
|
|
static create<Name extends string, Data extends ZodSchema>(name: Name, data: Data) {
|
|
const schema = z
|
|
.object({
|
|
name: z.literal(name),
|
|
data,
|
|
})
|
|
.openapi({
|
|
ref: name,
|
|
})
|
|
const result = class extends NamedError {
|
|
public static readonly Schema = schema
|
|
|
|
public readonly name = name as Name
|
|
|
|
constructor(
|
|
public readonly data: z.input<Data>,
|
|
options?: ErrorOptions,
|
|
) {
|
|
super(name, options)
|
|
this.name = name
|
|
}
|
|
|
|
static isInstance(input: any): input is InstanceType<typeof result> {
|
|
return "name" in input && input.name === name
|
|
}
|
|
|
|
schema() {
|
|
return schema
|
|
}
|
|
|
|
toObject() {
|
|
return {
|
|
name: name,
|
|
data: this.data,
|
|
}
|
|
}
|
|
}
|
|
Object.defineProperty(result, "name", { value: name })
|
|
return result
|
|
}
|
|
|
|
public static readonly Unknown = NamedError.create(
|
|
"UnknownError",
|
|
z.object({
|
|
message: z.string(),
|
|
}),
|
|
)
|
|
}
|