@@ -86,26 +86,30 @@ def create_http_error(
8686 - Can skip internal details when 500 status e.g. to avoid transmitting server
8787 exceptions to the client in production
8888 """
89- if not isinstance (errors , list ):
90- errors = [errors ]
91-
92- is_internal_error = bool (http_error_cls == web .HTTPInternalServerError )
93-
9489 status_reason = status_reason or get_code_display_name (http_error_cls .status_code )
9590 error_message = error_message or get_code_description (http_error_cls .status_code )
91+
9692 assert len (status_reason ) < MAX_STATUS_MESSAGE_LENGTH # nosec
9793
94+ # WARNING: do not refactor too much this function withouth considering how
95+ # front-end handle errors. i.e. please sync with front-end developers before
96+ # changing the workflows in this function
97+
98+ is_internal_error = bool (http_error_cls == web .HTTPInternalServerError )
9899 if is_internal_error and skip_internal_error_details :
99- error = ErrorGet .model_validate (
100+ error_model = ErrorGet .model_validate (
100101 {
101102 "status" : http_error_cls .status_code ,
102103 "message" : error_message ,
103104 "support_id" : error_code ,
104105 }
105106 )
106107 else :
108+ if not isinstance (errors , list ):
109+ errors = [errors ]
110+
107111 items = [ErrorItemType .from_error (err ) for err in errors ]
108- error = ErrorGet .model_validate (
112+ error_model = ErrorGet .model_validate (
109113 {
110114 "errors" : items , # NOTE: deprecated!
111115 "status" : http_error_cls .status_code ,
@@ -115,15 +119,14 @@ def create_http_error(
115119 )
116120
117121 assert not http_error_cls .empty_body # nosec
122+
118123 payload = wrap_as_envelope (
119- error = error .model_dump (mode = "json" , ** RESPONSE_MODEL_POLICY )
124+ error = error_model .model_dump (mode = "json" , ** RESPONSE_MODEL_POLICY )
120125 )
121126
122127 return http_error_cls (
123128 reason = safe_status_message (status_reason ),
124- text = json_dumps (
125- payload ,
126- ),
129+ text = json_dumps (payload ),
127130 content_type = MIMETYPE_APPLICATION_JSON ,
128131 )
129132
0 commit comments