Skip to content

Commit 9d705e6

Browse files
committed
fix: error.type -> error.kind
Signed-off-by: tunnckoCore <5038030+tunnckoCore@users.noreply.github.com>
1 parent afe026c commit 9d705e6

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

src-v4/ss-zod.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ const pingProc = zagora()
101101
.input(z.number())
102102
.output(z.object({ pong: z.number(), userId: z.string() }).strict())
103103
.errors({
104-
NOT_FOUND: z.object({ type: z.literal("NOT_FOUND"), userId: z.string() }),
105-
AUTH_ERR: z.object({ type: z.literal("AUTH_ERR"), retryAfter: z.number() }),
104+
NOT_FOUND: z.object({ kind: z.literal("NOT_FOUND"), userId: z.string() }),
105+
AUTH_ERR: z.object({ kind: z.literal("AUTH_ERR"), retryAfter: z.number() }),
106106
})
107107
.handler(({ errors, context }, id) => {
108108
if (id === 42) {
@@ -137,7 +137,7 @@ if (
137137
"foo::::",
138138
// Type checking now correctly infers resPing.error as PingCallableError
139139
// and then further narrows it based on the 'type' property.
140-
resPing.error.type === "AUTH_ERR" ? resPing.error.retryAfter : "sasa",
140+
resPing.error.kind === "AUTH_ERR" ? resPing.error.retryAfter : "sasa",
141141
"<<<",
142142
);
143143
} else if (resPing.error && resPing.error instanceof Error) {

src-v4/sscore.ts

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { StandardSchemaV1 } from "@standard-schema/spec";
2-
import { ZagoraError } from "../src/error";
32

43
export type Schema<I, O = I> = StandardSchemaV1<I, O>;
54

@@ -117,6 +116,42 @@ export type ZagoraResult<
117116
isTypedError: boolean;
118117
};
119118

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+
120155
export function handleTupleDefaults(
121156
schema: AnySchema,
122157
rawArgs: unknown[],
@@ -201,7 +236,7 @@ function createErrorHelpers(errorMap: any): Record<string, (data: any) => any> {
201236
if (!schema) continue;
202237

203238
helpers[key] = (data: any) => {
204-
return { type: key, ...data };
239+
return { kind: key, ...data };
205240
};
206241
}
207242
return helpers;
@@ -236,7 +271,7 @@ export type ErrorHelpers<
236271
> = TErrorsMap extends Record<string, AnySchema>
237272
? {
238273
[K in keyof TErrorsMap]: (
239-
data: Prettify<Omit<InferSchemaInput<TErrorsMap[K]>, "type">>,
274+
data: Prettify<Omit<InferSchemaInput<TErrorsMap[K]>, "kind">>,
240275
) => never;
241276
}
242277
: never;

0 commit comments

Comments
 (0)