Skip to content

Commit 946a182

Browse files
pescnclaude
andcommitted
fix: return 400 for CompilerError in search endpoints
compileSearch throws CompilerError for user input errors (unknown fields, invalid enum values, etc.). Wrap all three endpoints with try-catch to return 400 instead of letting it bubble up as 500. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 1f2518f commit 946a182

1 file changed

Lines changed: 27 additions & 3 deletions

File tree

backend/src/api/admin/search.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,15 @@ export const adminSearch = new Elysia()
6262
} catch (err) {
6363
return status(400, { error: err instanceof Error ? err.message : "Invalid timeRange" });
6464
}
65-
const compiled = compileSearch(result.query, { timeRange });
65+
let compiled;
66+
try {
67+
compiled = compileSearch(result.query, { timeRange });
68+
} catch (err) {
69+
return status(400, {
70+
error: "Invalid query",
71+
details: err instanceof Error ? err.message : "Compilation failed",
72+
});
73+
}
6674

6775
// If the query has aggregation, return aggregation results
6876
if (compiled.aggregation) {
@@ -130,7 +138,15 @@ export const adminSearch = new Elysia()
130138
} catch (err) {
131139
return status(400, { error: err instanceof Error ? err.message : "Invalid timeRange" });
132140
}
133-
const compiled = compileSearch(result.query, { timeRange });
141+
let compiled;
142+
try {
143+
compiled = compileSearch(result.query, { timeRange });
144+
} catch (err) {
145+
return status(400, {
146+
error: "Invalid query",
147+
details: err instanceof Error ? err.message : "Compilation failed",
148+
});
149+
}
134150

135151
try {
136152
const buckets = await searchCompletionsTimeSeries(
@@ -192,7 +208,15 @@ export const adminSearch = new Elysia()
192208
} catch (err) {
193209
return status(400, { error: err instanceof Error ? err.message : "Invalid timeRange" });
194210
}
195-
const compiled = compileSearch(result.query, { timeRange });
211+
let compiled;
212+
try {
213+
compiled = compileSearch(result.query, { timeRange });
214+
} catch (err) {
215+
return status(400, {
216+
error: "Invalid query",
217+
details: err instanceof Error ? err.message : "Compilation failed",
218+
});
219+
}
196220

197221
try {
198222
// Fetch all results (up to 10000 for export)

0 commit comments

Comments
 (0)