diff --git a/packages/typescript-koa-runtime/src/zod-v3.ts b/packages/typescript-koa-runtime/src/zod-v3.ts index 686ff248..10371bf2 100644 --- a/packages/typescript-koa-runtime/src/zod-v3.ts +++ b/packages/typescript-koa-runtime/src/zod-v3.ts @@ -21,7 +21,10 @@ export function parseRequestInput( try { return schema?.parse(input) } catch (err) { - throw KoaRuntimeError.RequestError(err, type) + throw KoaRuntimeError.RequestError( + new Error("Invalid request input", {cause: err}), + type, + ) } } @@ -48,7 +51,11 @@ export function responseValidationFactory( // TODO: throw on unmatched response return value } catch (err) { - throw KoaRuntimeError.ResponseError(err) + throw KoaRuntimeError.ResponseError( + new Error("Invalid response", { + cause: err, + }), + ) } } } diff --git a/packages/typescript-koa-runtime/src/zod-v4.ts b/packages/typescript-koa-runtime/src/zod-v4.ts index 2d1eaa4e..9a4cfa8f 100644 --- a/packages/typescript-koa-runtime/src/zod-v4.ts +++ b/packages/typescript-koa-runtime/src/zod-v4.ts @@ -1,6 +1,6 @@ import {findMatchingSchema} from "@nahkies/typescript-common-runtime/validation" -import type {z} from "zod/v4" +import {z} from "zod/v4" import {KoaRuntimeError, type RequestInputType} from "./errors.ts" export function parseRequestInput( @@ -21,7 +21,14 @@ export function parseRequestInput( try { return schema?.parse(input) } catch (err) { - throw KoaRuntimeError.RequestError(err, type) + const enhancedMessagePostfix = + err instanceof z.ZodError ? ` - ${z.prettifyError(err)}` : "" + throw KoaRuntimeError.RequestError( + new Error(`Invalid request input${enhancedMessagePostfix}`, { + cause: err, + }), + type, + ) } } @@ -48,7 +55,11 @@ export function responseValidationFactory( // TODO: throw on unmatched response return value } catch (err) { - throw KoaRuntimeError.ResponseError(err) + const enhancedMessagePostfix = + err instanceof z.ZodError ? ` - ${z.prettifyError(err)}` : "" + throw KoaRuntimeError.ResponseError( + new Error(`Invalid response${enhancedMessagePostfix}`, {cause: err}), + ) } } }