Skip to content

Commit 95614ad

Browse files
committed
fix(api): surface schema validation failures as ApiError instead of ZodError
schema.parse() in apiRequestToRegion could throw a raw ZodError on unexpected API response shapes, which the central error handler would display as an 'Unexpected error' stack trace. Use safeParse() instead and convert failures to ApiError so they surface as clean user-facing error messages regardless of which command triggered the request.
1 parent c373529 commit 95614ad

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/lib/api-client.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,23 @@ export async function apiRequestToRegion<T>(
305305
}
306306

307307
const data = await response.json();
308-
const validated = schema ? schema.parse(data) : (data as T);
309308

310-
return { data: validated, headers: response.headers };
309+
if (schema) {
310+
const result = schema.safeParse(data);
311+
if (!result.success) {
312+
// Treat schema validation failures as API errors so they surface cleanly
313+
// through the central error handler rather than showing a raw ZodError
314+
// stack trace. This guards against unexpected API response format changes.
315+
throw new ApiError(
316+
`Unexpected response format from ${endpoint}`,
317+
response.status,
318+
result.error.message
319+
);
320+
}
321+
return { data: result.data, headers: response.headers };
322+
}
323+
324+
return { data: data as T, headers: response.headers };
311325
}
312326

313327
/**

0 commit comments

Comments
 (0)