Skip to content

Send a phone call from AI agent, in an API call. Or, directly call the bot from the configured phone number!

Python

fix: Details field in error message is always a list

Clémence Lesné8ec3a25be9b8
diff --git a/function_app.py b/function_app.py
index 40a40a9..5aad47d 100644
--- a/function_app.py
+++ b/function_app.py
@@ -836,22 +836,35 @@ def _str_to_contexts(value: Optional[str]) -> Optional[set[CallContextEnum]]:
def _validation_error(
e: Exception,
) -> func.HttpResponse:
- body: dict[str, Any] = {
+ """
+ Generate a standard validation error response.
+
+ Response body is a JSON object with the following structure:
+
+ ```
+ {
"error": {
"message": "Validation error",
- "details": [],
+ "details": ["Error message"]
}
}
+ ```
+
+ Returns a 400 Bad Request with a JSON body.
+ """
+ messages = []
if isinstance(e, ValidationError):
- body["error"][
- "details"
- ] = e.errors() # Pydantic returns well formatted errors, use them
+ messages = e.errors() # Pydantic returns well formatted errors, use them
elif isinstance(e, ValueError):
- body["error"]["details"] = str(
- e
- ) # TODO: Could it expose sensitive information?
+ messages = [str(e)] # TODO: Could it expose sensitive information?
+ res_json = {
+ "error": {
+ "message": "Validation error",
+ "details": messages,
+ }
+ }
return func.HttpResponse(
- body=json.dumps(body),
+ body=json.dumps(res_json),
mimetype="application/json",
status_code=HTTPStatus.BAD_REQUEST,
)