pierrejeambrun commented on code in PR #66888:
URL: https://github.com/apache/airflow/pull/66888#discussion_r3406377951
##########
airflow-core/src/airflow/api_fastapi/common/exceptions.py:
##########
@@ -108,6 +108,52 @@ def _is_dialect_matched(self, exc: IntegrityError) -> bool:
return False
+class DataErrorHandler(BaseErrorHandler[DataError]):
+ """
+ Translate ``sqlalchemy.exc.DataError`` into a 422.
+
+ The database rejected a value that passed Pydantic validation (too long,
out
+ of range, or the wrong type for its column), so it is a client error, not a
+ 500. The statement and underlying DB error are logged under a lookup id and
+ returned to the caller only when ``[api] expose_stacktrace`` is set,
mirroring
+ ``_UniqueConstraintErrorHandler``.
+ """
+
+ def __init__(self):
+ super().__init__(DataError)
+
+ def exception_handler(self, request: Request, exc: DataError):
+ """Handle DataError exception."""
+ exception_id = get_random_string()
+ stacktrace = ""
+ for tb in traceback.format_tb(exc.__traceback__):
+ stacktrace += tb
+
+ log_message = f"Error with id {exception_id}, statement:
{exc.statement}\n{stacktrace}"
+ log.error(log_message)
+ if conf.get("api", "expose_stacktrace") == "True":
+ message = log_message
+ statement = str(exc.statement)
+ orig_error = str(exc.orig)
+ else:
+ message = (
+ "Serious error when handling your request. Check logs for more
details - "
+ f"you will find it in api server when you look for ID
{exception_id}"
+ )
+ statement = "hidden"
+ orig_error = "hidden"
+
+ raise HTTPException(
+ status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
Review Comment:
As a follow up we can probably factorize this code which is really the same
as `_UniqueConstraintErrorHandler`, but non blocking for now.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]