|
1 | 1 | import type { StandardSchemaV1 } from "@standard-schema/spec"; |
2 | | -import { ZagoraError } from "../src/error"; |
3 | 2 |
|
4 | 3 | export type Schema<I, O = I> = StandardSchemaV1<I, O>; |
5 | 4 |
|
@@ -117,6 +116,42 @@ export type ZagoraResult< |
117 | 116 | isTypedError: boolean; |
118 | 117 | }; |
119 | 118 |
|
| 119 | +export class ZagoraError extends Error { |
| 120 | + readonly issues?: readonly SchemaIssue[]; |
| 121 | + override readonly cause?: unknown; |
| 122 | + readonly data?: unknown; |
| 123 | + readonly reason: string; |
| 124 | + |
| 125 | + constructor( |
| 126 | + message: string, |
| 127 | + options?: { |
| 128 | + issues?: readonly SchemaIssue[]; |
| 129 | + cause?: unknown; |
| 130 | + data?: unknown; |
| 131 | + reason?: string; |
| 132 | + }, |
| 133 | + ) { |
| 134 | + super(message); |
| 135 | + this.name = "ZagoraError"; |
| 136 | + this.issues = options?.issues; |
| 137 | + this.cause = options?.cause; |
| 138 | + this.data = options?.data; |
| 139 | + this.reason = options?.reason || "Unknown or internal error"; |
| 140 | + } |
| 141 | + |
| 142 | + static fromIssues( |
| 143 | + issues: readonly SchemaIssue[], |
| 144 | + reason?: string, |
| 145 | + error?: any, |
| 146 | + ) { |
| 147 | + const message = issues.map((issue) => issue.message).join(", "); |
| 148 | + return new ZagoraError(message, { |
| 149 | + issues, |
| 150 | + reason: reason || "Failure caused by validation", |
| 151 | + }); |
| 152 | + } |
| 153 | +} |
| 154 | + |
120 | 155 | export function handleTupleDefaults( |
121 | 156 | schema: AnySchema, |
122 | 157 | rawArgs: unknown[], |
@@ -201,7 +236,7 @@ function createErrorHelpers(errorMap: any): Record<string, (data: any) => any> { |
201 | 236 | if (!schema) continue; |
202 | 237 |
|
203 | 238 | helpers[key] = (data: any) => { |
204 | | - return { type: key, ...data }; |
| 239 | + return { kind: key, ...data }; |
205 | 240 | }; |
206 | 241 | } |
207 | 242 | return helpers; |
@@ -236,7 +271,7 @@ export type ErrorHelpers< |
236 | 271 | > = TErrorsMap extends Record<string, AnySchema> |
237 | 272 | ? { |
238 | 273 | [K in keyof TErrorsMap]: ( |
239 | | - data: Prettify<Omit<InferSchemaInput<TErrorsMap[K]>, "type">>, |
| 274 | + data: Prettify<Omit<InferSchemaInput<TErrorsMap[K]>, "kind">>, |
240 | 275 | ) => never; |
241 | 276 | } |
242 | 277 | : never; |
|
0 commit comments