9
9
AfterErrorContext ,
10
10
AfterErrorHook ,
11
11
SDKInitHook ,
12
+ AfterSuccessHook ,
12
13
)
13
14
from collections import defaultdict
14
15
15
16
logger = logging .getLogger (UNSTRUCTURED_CLIENT_LOGGER_NAME )
16
17
17
18
18
- class LoggerHook (AfterErrorHook , SDKInitHook ):
19
+ class LoggerHook (AfterErrorHook , AfterSuccessHook , SDKInitHook ):
19
20
"""Hook providing custom logging"""
20
21
21
22
def __init__ (self ) -> None :
22
23
self .retries_counter : DefaultDict [str , int ] = defaultdict (int )
23
24
24
- def log_retries (self , response : Optional [requests .Response ], operation_id : str ):
25
+ def log_retries (self , response : Optional [requests .Response ], error : Optional [ Exception ], operation_id : str , ):
25
26
"""Log retries to give users visibility into requests."""
26
27
27
28
if response is not None and response .status_code // 100 == 5 :
@@ -33,6 +34,15 @@ def log_retries(self, response: Optional[requests.Response], operation_id: str):
33
34
)
34
35
if response .text :
35
36
logger .info ("Server message - %s" , response .text )
37
+
38
+ elif error is not None and isinstance (error , requests .exceptions .ConnectionError ):
39
+ logger .info (
40
+ "Failed to process a request due to connection error - %s. "
41
+ "Attempting retry number %d after sleep." ,
42
+ error ,
43
+ self .retries_counter [operation_id ],
44
+ )
45
+
36
46
37
47
def sdk_init (
38
48
self , base_url : str , client : requests .Session
@@ -43,7 +53,9 @@ def sdk_init(
43
53
def after_success (
44
54
self , hook_ctx : AfterSuccessContext , response : requests .Response
45
55
) -> Union [requests .Response , Exception ]:
46
- del self .retries_counter [hook_ctx .operation_id ]
56
+ self .retries_counter .pop (hook_ctx .operation_id , None )
57
+ # NOTE: In case of split page partition this means - at least one of the splits was partitioned successfully
58
+ logger .info ("Successfully partitioned the document." )
47
59
return response
48
60
49
61
def after_error (
@@ -54,5 +66,18 @@ def after_error(
54
66
) -> Union [Tuple [Optional [requests .Response ], Optional [Exception ]], Exception ]:
55
67
"""Concrete implementation for AfterErrorHook."""
56
68
self .retries_counter [hook_ctx .operation_id ] += 1
57
- self .log_retries (response , hook_ctx .operation_id )
69
+ self .log_retries (response , error , hook_ctx .operation_id )
70
+
71
+ if response and response .status_code == 200 :
72
+ # NOTE: Even though this is an after_error method, due to split_pdf_hook logic we may get
73
+ # a success here when one of the split requests was partitioned successfully
74
+ logger .info ("Successfully partitioned the document." )
75
+
76
+ else :
77
+ logger .error ("Failed to partition the document." )
78
+ if response :
79
+ logger .error ("Server responded with %d - %s" , response .status_code , response .text )
80
+ if error is not None :
81
+ logger .error ("Following error occurred - %s" , error )
82
+
58
83
return response , error
0 commit comments