22from typing import Optional
33
44from mindee .parsing .common .string_dict import StringDict
5+ from mindee .parsing .v2 import ErrorItem , ErrorResponse
56
67
7- class MindeeHTTPErrorV2 (RuntimeError ):
8+ class MindeeHTTPErrorV2 (RuntimeError , ErrorResponse ):
89 """An exception relating to HTTP calls."""
910
10- status : int
11- detail : Optional [str ]
12-
13- def __init__ (self , status : int , detail : Optional [str ]) -> None :
11+ def __init__ (self , response : ErrorResponse ) -> None :
1412 """
1513 Base exception for HTTP calls.
1614
17- :param status: HTTP code for the error
18- :param detail: Error details.
15+ :param response:
1916 """
20- self .status = status
21- self .detail = detail
22- super ().__init__ (f"HTTP error { status } - { detail } " )
17+ self .status = response .status
18+ self .title = response .title
19+ self .code = response .code
20+ self .detail = response .detail
21+ self .errors : list [ErrorItem ] = response .errors
22+ super ().__init__ (
23+ f"HTTP { self .status } - { self .title } :: { self .code } - { self .detail } "
24+ )
2325
2426
2527class MindeeHTTPUnknownErrorV2 (MindeeHTTPErrorV2 ):
2628 """HTTP error with unknown status code."""
2729
2830 def __init__ (self , detail : Optional [str ]) -> None :
29- super ().__init__ (- 1 , f"Couldn't deserialize server error. Found: { detail } " )
31+ super ().__init__ (
32+ ErrorResponse (
33+ {
34+ "status" : - 1 ,
35+ "code" : "000-000" ,
36+ "title" : "Unknown Error" ,
37+ "detail" : f"Couldn't deserialize server error. Found: { detail } " ,
38+ }
39+ )
40+ )
3041
3142
3243def handle_error_v2 (raw_response : StringDict ) -> None :
@@ -38,7 +49,4 @@ def handle_error_v2(raw_response: StringDict) -> None:
3849 """
3950 if "status" not in raw_response or "detail" not in raw_response :
4051 raise MindeeHTTPUnknownErrorV2 (json .dumps (raw_response , indent = 2 ))
41- raise MindeeHTTPErrorV2 (
42- raw_response ["status" ],
43- raw_response ["detail" ],
44- )
52+ raise MindeeHTTPErrorV2 (ErrorResponse (raw_response ))
0 commit comments