Skip to content

The open-source agent harness - the runtime layer that turns an LLM into a working agent.

Add middleware to make zod errors readable (#108)

Chirag Jain889fcf774dc0
diff --git a/.prettierignore b/.prettierignore
index 6d9f5332a..5ec04f11b 100644
--- a/.prettierignore
+++ b/.prettierignore
@@ -3,8 +3,8 @@ dist/
# Fern-generated: formatted by the generator, not by this repo's config.
packages/sdk/
fern/openapi/
-# Downloaded third-party Helm subcharts (Bitnami); not ours to reformat.
-charts/trueforge/charts/
+# Helm chart sources and downloaded Bitnami subcharts; not ours to reformat.
+charts/
coverage/
# Prettier's markdown parser escapes `*` inside MDX `{/* */}` comments, breaking MDX parsing.
*.mdx
diff --git a/packages/server/src/app.ts b/packages/server/src/app.ts
index 9357016f2..27b869b7b 100644
--- a/packages/server/src/app.ts
+++ b/packages/server/src/app.ts
@@ -1,6 +1,6 @@
/** The API: resource routers, the OpenAPI document and Swagger UI, all under /api/v1. */
import { swaggerUI } from '@hono/swagger-ui';
-import { OpenAPIHono } from '@hono/zod-openapi';
+import { OpenAPIHono, z } from '@hono/zod-openapi';
import type { ISessionStore, Sessions, TurnStreamingEvent } from '@truefoundry/utils-core/agent-session';
import type { RequestReplyRouter } from '@truefoundry/utils-core/request-reply';
import type { Context } from 'hono';
@@ -33,6 +33,7 @@ import type { WithTransaction } from './db/transaction';
import type { IOAuthTokenStore } from './mcp/auth/types';
import type { ActiveTurnRegistry } from './runtime/activeTurns';
import type { EventSubscriptionRegistry } from './runtime/event-subscription';
+import { zodErrorResponse, zodValidationHook } from './zodErrorResponse';
const openApiDocConfig = {
openapi: '3.1.0',
@@ -88,7 +89,7 @@ export interface ServerDeps<TTransaction> {
}
export function createServerApp<TTransaction>(deps: ServerDeps<TTransaction>) {
- const app = new OpenAPIHono();
+ const app = new OpenAPIHono({ defaultHook: zodValidationHook });
app.get('/healthz', c => c.text('OK!'));
@@ -217,6 +218,9 @@ export function createServerApp<TTransaction>(deps: ServerDeps<TTransaction>) {
app.notFound(routeNotFound);
app.onError((error, c) => {
+ if (error instanceof z.ZodError) {
+ return zodErrorResponse(c, error);
+ }
if (error instanceof HTTPException) {
return c.json({ error: { message: error.message } }, error.status);
}
diff --git a/packages/server/src/zodErrorResponse.ts b/packages/server/src/zodErrorResponse.ts
new file mode 100644
index 000000000..bbeb14fdc
--- /dev/null
+++ b/packages/server/src/zodErrorResponse.ts
@@ -0,0 +1,15 @@
+/** Shared Zod → HTTP 400 formatting for OpenAPI validation and thrown ZodError. */
+import type { Hook } from '@hono/zod-openapi';
+import { z } from '@hono/zod-openapi';
+import type { Context } from 'hono';
+
+export function zodErrorResponse(c: Context, error: z.ZodError) {
+ return c.json({ error: { message: z.prettifyError(error) } }, 400);
+}
+
+export const zodValidationHook: Hook<unknown, object, string, Response | undefined> = (result, c) => {
+ if (!result.success) {
+ return zodErrorResponse(c, result.error);
+ }
+ return undefined;
+};