|
1 | 1 | """Utils to check, convert and compose server responses for the RESTApi""" |
2 | 2 |
|
3 | 3 | import inspect |
4 | | -from typing import Any, cast |
| 4 | +from typing import Any |
5 | 5 |
|
6 | 6 | from aiohttp import web, web_exceptions |
7 | 7 | from aiohttp.web_exceptions import HTTPError, HTTPException |
8 | 8 | from common_library.error_codes import ErrorCodeStr |
9 | 9 | from common_library.json_serialization import json_dumps |
10 | | -from models_library.basic_types import IDStr |
11 | 10 | from models_library.rest_error import ErrorGet, ErrorItemType |
12 | 11 | from servicelib.rest_constants import RESPONSE_MODEL_POLICY |
13 | 12 |
|
@@ -65,24 +64,26 @@ def create_http_error( |
65 | 64 | if not isinstance(errors, list): |
66 | 65 | errors = [errors] |
67 | 66 |
|
68 | | - support_id: IDStr | None = cast(IDStr, error_code) if error_code else None |
69 | | - |
70 | 67 | is_internal_error: bool = http_error_cls == web.HTTPInternalServerError |
71 | 68 | default_message = reason or get_code_description(http_error_cls.status_code) |
72 | 69 |
|
73 | 70 | if is_internal_error and skip_internal_error_details: |
74 | | - error = ErrorGet( |
75 | | - status=http_error_cls.status_code, |
76 | | - message=default_message, |
77 | | - support_id=support_id, |
| 71 | + error = ErrorGet.model_validate( |
| 72 | + { |
| 73 | + "status": http_error_cls.status_code, |
| 74 | + "message": default_message, |
| 75 | + "support_id": error_code, |
| 76 | + } |
78 | 77 | ) |
79 | 78 | else: |
80 | 79 | items = [ErrorItemType.from_error(err) for err in errors] |
81 | | - error = ErrorGet( |
82 | | - errors=items, # NOTE: deprecated! |
83 | | - status=http_error_cls.status_code, |
84 | | - message=default_message, |
85 | | - support_id=support_id, |
| 80 | + error = ErrorGet.model_validate( |
| 81 | + { |
| 82 | + "errors": items, # NOTE: deprecated! |
| 83 | + "status": http_error_cls.status_code, |
| 84 | + "message": default_message, |
| 85 | + "support_id": error_code, |
| 86 | + } |
86 | 87 | ) |
87 | 88 |
|
88 | 89 | assert not http_error_cls.empty_body # nosec |
|
0 commit comments