11"""Utils to check, convert and compose server responses for the RESTApi"""
22
33import inspect
4- from typing import Any
4+ from typing import Any , cast
55
66from aiohttp import web , web_exceptions
77from aiohttp .web_exceptions import HTTPError , HTTPException
8+ from common_library .error_codes import ErrorCodeStr
89from common_library .json_serialization import json_dumps
10+ from models_library .basic_types import IDStr
911from models_library .rest_error import ErrorGet , ErrorItemType
1012
1113from ..aiohttp .status import HTTP_200_OK
@@ -52,6 +54,7 @@ def create_http_error(
5254 http_error_cls : type [HTTPError ] = web .HTTPInternalServerError ,
5355 * ,
5456 skip_internal_error_details : bool = False ,
57+ error_code : ErrorCodeStr | None = None
5558) -> HTTPError :
5659 """
5760 - Response body conforms OAS schema model
@@ -61,25 +64,24 @@ def create_http_error(
6164 if not isinstance (errors , list ):
6265 errors = [errors ]
6366
64- # TODO: guarantee no throw!
67+ support_id : IDStr | None = cast ( IDStr , error_code ) if error_code else None
6568
6669 is_internal_error : bool = http_error_cls == web .HTTPInternalServerError
6770 default_message = reason or get_code_description (http_error_cls .status_code )
6871
6972 if is_internal_error and skip_internal_error_details :
7073 error = ErrorGet (
71- errors = [],
72- logs = [],
7374 status = http_error_cls .status_code ,
7475 message = default_message ,
76+ support_id = support_id ,
7577 )
7678 else :
7779 items = [ErrorItemType .from_error (err ) for err in errors ]
7880 error = ErrorGet (
79- errors = items ,
80- logs = [],
81+ errors = items , # NOTE: deprecated!
8182 status = http_error_cls .status_code ,
8283 message = default_message ,
84+ support_id = support_id ,
8385 )
8486
8587 assert not http_error_cls .empty_body # nosec
0 commit comments