Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions packages/typescript-koa-runtime/src/zod-v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ export function parseRequestInput<Schema extends z.ZodTypeAny>(
try {
return schema?.parse(input)
} catch (err) {
throw KoaRuntimeError.RequestError(err, type)
throw KoaRuntimeError.RequestError(
new Error("Invalid request input", {cause: err}),
type,
)
}
}

Expand All @@ -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,
}),
)
}
}
}
17 changes: 14 additions & 3 deletions packages/typescript-koa-runtime/src/zod-v4.ts
Original file line number Diff line number Diff line change
@@ -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<Schema extends z.ZodTypeAny>(
Expand All @@ -21,7 +21,14 @@ export function parseRequestInput<Schema extends z.ZodTypeAny>(
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,
)
}
}

Expand All @@ -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}),
)
}
}
}