Skip to content

Commit d910aec

Browse files
committed
feat: improve validation error handling in /api/journeys
1 parent 4bbb71c commit d910aec

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

app/api/journeys/route.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { vendoJourneySchema } from "@/utils/schemas";
22
import { createClient } from "db-vendo-client";
33
import { profile as dbProfile } from "db-vendo-client/p/db/index";
4-
import { z } from "zod/v4";
4+
import { prettifyError, z } from "zod/v4";
55
import {
66
getApiCount,
77
incrementApiCount,
@@ -53,11 +53,25 @@ const handler = async (request: Request) => {
5353
// Verbindungen von DB-API abrufen
5454
const journeys = await client.journeys(urlParams.from, urlParams.to, options);
5555

56-
const parsed = z
56+
const parseResult = z
5757
.object({ journeys: z.array(vendoJourneySchema) })
58-
.parse(journeys);
58+
.safeParse(journeys);
5959

60-
let allJourneys = parsed.journeys;
60+
if (!parseResult.success) {
61+
return Response.json(
62+
{
63+
success: false,
64+
error: `Validation of 'journeys' on DB-API failed: ${prettifyError(
65+
parseResult.error
66+
)}`,
67+
},
68+
{
69+
status: 500,
70+
}
71+
);
72+
}
73+
74+
let allJourneys = parseResult.data.journeys;
6175

6276
console.log(`Received ${allJourneys.length} journeys from main query`);
6377

0 commit comments

Comments
 (0)