Skip to content

Commit 4969ada

Browse files
committed
fix(api): catch DataError in /alerts/query to return 400 instead of 500
When a CEL filter contains a non-UUID value against a UUID-typed column, PostgreSQL raises a DataError which was not caught — only CelToSqlException was handled, so FastAPI returned a 500. Add an explicit except DataError block that returns a 400 with a clear message. This is a provider-agnostic safety net: any provider that stores a non-UUID alert id would hit the same crash, regardless of fixes applied at the provider level. Related: keephq#6219 (fixes root cause in Prometheus provider).
1 parent b2eabaf commit 4969ada

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

keep/api/routes/alerts.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from fastapi.responses import JSONResponse
1717
from pusher import Pusher
1818
from sqlalchemy_utils import UUIDType
19+
from sqlalchemy.exc import DataError
1920
from sqlmodel import Session
2021

2122
from keep.api.arq_pool import get_pool
@@ -228,6 +229,14 @@ def query_alerts(
228229
raise HTTPException(
229230
status_code=400, detail=f"Error parsing CEL expression: {query.cel}"
230231
) from e
232+
except DataError as e:
233+
logger.exception(
234+
f'Database type error while executing query "{query.cel}". {str(e)}'
235+
)
236+
raise HTTPException(
237+
status_code=400,
238+
detail=f"Invalid value in query - a field may contain a non-UUID value: {query.cel}",
239+
) from e
231240

232241
db_alerts = enrich_alerts_with_incidents(tenant_id, db_alerts)
233242
enriched_alerts_dto = convert_db_alerts_to_dto_alerts(

0 commit comments

Comments
 (0)