Skip to content

Commit a142599

Browse files
committed
make it a string instead of an error
1 parent 4c5863b commit a142599

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

packages/zod/src/config/config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ export function registerConfig<
9090
merge({ [namespace]: {} }, decodedConfig, decodedEnv),
9191
);
9292
if (!parsedConfig.success)
93-
throw typifyError(namespacedSchema, parsedConfig.error, "config", namespace, envKeys);
93+
throw new TypeError(
94+
typifyError(namespacedSchema, parsedConfig.error, "config", namespace, envKeys),
95+
{ cause: parsedConfig.error },
96+
);
9497
const config = parsedConfig.data[namespace];
9598

9699
return config;

packages/zod/src/internal.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,11 @@ export const typifyError = <S extends $ZodType>(
201201
context: "options" | "config",
202202
namespace: string,
203203
envKeys: Map<string, string> = new Map(),
204-
): TypeError => {
204+
jsonSchemaOptions: Parameters<typeof toJSONSchema>[1] = { unrepresentable: "any" },
205+
): string => {
205206
// NOTE: shamelessy copied from `prettifyError`
206207
const lines: string[] = [];
207-
const jsonSchema = toJSONSchema(schema, { unrepresentable: "any" });
208+
const jsonSchema = toJSONSchema(schema, jsonSchemaOptions);
208209

209210
// sort by path length
210211
const issues = [...error.issues].sort((a, b) => (a.path ?? []).length - (b.path ?? []).length);
@@ -227,7 +228,5 @@ export const typifyError = <S extends $ZodType>(
227228
}
228229
}
229230

230-
return new TypeError(`Invalid ${context} for "${namespace}":\n${lines.join("\n")}`, {
231-
cause: error,
232-
});
231+
return `Invalid ${context} for "${namespace}":\n${lines.join("\n")}`;
233232
};

packages/zod/src/module/module.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,9 @@ export class ZodConfigurableModuleBuilder<
199199
return await parseAsync(self.schema, finalOptions);
200200
} catch (err) {
201201
throw err instanceof $ZodError
202-
? typifyError(self.schema, err, "options", this.name)
202+
? new TypeError(typifyError(self.schema, err, "options", this.name), {
203+
cause: err,
204+
})
203205
: err;
204206
}
205207
},
@@ -299,7 +301,9 @@ export class ZodConfigurableModuleBuilder<
299301
return await parseAsync(self.schema, finalOptions);
300302
} catch (err) {
301303
throw err instanceof $ZodError
302-
? typifyError(self.schema, err, "options", this.name)
304+
? new TypeError(typifyError(self.schema, err, "options", this.name), {
305+
cause: err,
306+
})
303307
: err;
304308
}
305309
},
@@ -321,7 +325,7 @@ export class ZodConfigurableModuleBuilder<
321325
return await parseAsync(self.schema, finalOptions);
322326
} catch (err) {
323327
throw err instanceof $ZodError
324-
? typifyError(self.schema, err, "options", this.name)
328+
? new TypeError(typifyError(self.schema, err, "options", this.name), { cause: err })
325329
: err;
326330
}
327331
},

0 commit comments

Comments
 (0)