1+ from typing import Any , Dict , Optional , Union
2+
3+
14class BentoError (Exception ):
25 """
36 Represents a Databento specific error.
@@ -13,18 +16,18 @@ class BentoHttpError(BentoError):
1316
1417 def __init__ (
1518 self ,
16- http_status = None ,
17- http_body = None ,
18- json_body = None ,
19- message = None ,
20- headers = None ,
21- ):
19+ http_status : int ,
20+ http_body : Optional [ Union [ bytes , str ]] = None ,
21+ json_body : Optional [ Dict [ str , Any ]] = None ,
22+ message : Optional [ str ] = None ,
23+ headers : Optional [ Any ] = None ,
24+ ) -> None :
2225 super (BentoHttpError , self ).__init__ (message )
2326
24- if http_body and hasattr (http_body , "decode" ):
27+ if http_body and isinstance (http_body , bytes ):
2528 try :
2629 http_body = http_body .decode ("utf-8" )
27- except BaseException :
30+ except UnicodeDecodeError :
2831 http_body = (
2932 "<Could not decode body as utf-8. "
3033 "Please report to [email protected] >" @@ -37,15 +40,15 @@ def __init__(
3740 self .headers = headers or {}
3841 self .request_id = self .headers .get ("request-id" , None )
3942
40- def __str__ (self ):
43+ def __str__ (self ) -> str :
4144 msg = self .message or "<empty message>"
4245 msg = f"{ self .http_status } { msg } "
4346 if self .request_id is not None :
4447 return f"Request { self .request_id } : { msg } "
4548 else :
4649 return msg
4750
48- def __repr__ (self ):
51+ def __repr__ (self ) -> str :
4952 return (
5053 f"{ type (self ).__name__ } ("
5154 f"request_id={ self .request_id } , "
@@ -61,15 +64,15 @@ class BentoServerError(BentoHttpError):
6164
6265 def __init__ (
6366 self ,
64- http_status = None ,
65- http_body = None ,
66- json_body = None ,
67- message = None ,
68- headers = None ,
69- ):
67+ http_status : int ,
68+ http_body : Optional [ Union [ bytes , str ]] = None ,
69+ json_body : Optional [ Dict [ str , Any ]] = None ,
70+ message : Optional [ str ] = None ,
71+ headers : Optional [ Any ] = None ,
72+ ) -> None :
7073 super ().__init__ (
71- http_body = http_body ,
7274 http_status = http_status ,
75+ http_body = http_body ,
7376 json_body = json_body ,
7477 message = message ,
7578 headers = headers ,
@@ -83,15 +86,15 @@ class BentoClientError(BentoHttpError):
8386
8487 def __init__ (
8588 self ,
86- http_status = None ,
87- http_body = None ,
88- json_body = None ,
89- message = None ,
90- headers = None ,
91- ):
89+ http_status : int ,
90+ http_body : Optional [ Union [ bytes , str ]] = None ,
91+ json_body : Optional [ Dict [ str , Any ]] = None ,
92+ message : Optional [ str ] = None ,
93+ headers : Optional [ Any ] = None ,
94+ ) -> None :
9295 super ().__init__ (
93- http_body = http_body ,
9496 http_status = http_status ,
97+ http_body = http_body ,
9598 json_body = json_body ,
9699 message = message ,
97100 headers = headers ,
0 commit comments