2
2
Batch processing exceptions
3
3
"""
4
4
import traceback
5
+ from typing import Optional , Tuple
5
6
6
7
7
- class SQSBatchProcessingError (Exception ):
8
- """When at least one message within a batch could not be processed"""
9
-
8
+ class BaseBatchProcessingError (Exception ):
10
9
def __init__ (self , msg = "" , child_exceptions = ()):
11
10
super ().__init__ (msg )
12
11
self .msg = msg
13
12
self .child_exceptions = child_exceptions
14
13
15
- # Overriding this method so we can output all child exception tracebacks when we raise this exception to prevent
16
- # errors being lost. See https://github.com/awslabs/aws-lambda-powertools-python/issues/275
17
- def __str__ (self ):
18
- parent_exception_str = super (SQSBatchProcessingError , self ).__str__ ()
14
+ def format_exceptions (self , parent_exception_str ):
19
15
exception_list = [f"{ parent_exception_str } \n " ]
20
16
for exception in self .child_exceptions :
21
17
extype , ex , tb = exception
@@ -25,22 +21,25 @@ def __str__(self):
25
21
return "\n " .join (exception_list )
26
22
27
23
28
- class BatchProcessingError ( Exception ):
29
- """When batch messages could not be processed"""
24
+ class SQSBatchProcessingError ( BaseBatchProcessingError ):
25
+ """When at least one message within a batch could not be processed"""
30
26
31
- def __init__ (self , msg = "" , child_exceptions = ()):
32
- super ().__init__ (msg )
33
- self .msg = msg
34
- self .child_exceptions = child_exceptions
27
+ def __init__ (self , msg = "" , child_exceptions : Optional [Tuple [Exception ]] = None ):
28
+ super ().__init__ (msg , child_exceptions )
35
29
36
30
# Overriding this method so we can output all child exception tracebacks when we raise this exception to prevent
37
31
# errors being lost. See https://github.com/awslabs/aws-lambda-powertools-python/issues/275
38
32
def __str__ (self ):
39
- parent_exception_str = super (BatchProcessingError , self ).__str__ ()
40
- exception_list = [f"{ parent_exception_str } \n " ]
41
- for exception in self .child_exceptions :
42
- extype , ex , tb = exception
43
- formatted = "" .join (traceback .format_exception (extype , ex , tb ))
44
- exception_list .append (formatted )
33
+ parent_exception_str = super (SQSBatchProcessingError , self ).__str__ ()
34
+ return self .format_exceptions (parent_exception_str )
45
35
46
- return "\n " .join (exception_list )
36
+
37
+ class BatchProcessingError (BaseBatchProcessingError ):
38
+ """When all batch records failed to be processed"""
39
+
40
+ def __init__ (self , msg = "" , child_exceptions : Optional [Tuple [Exception ]] = None ):
41
+ super ().__init__ (msg , child_exceptions )
42
+
43
+ def __str__ (self ):
44
+ parent_exception_str = super (BatchProcessingError , self ).__str__ ()
45
+ return self .format_exceptions (parent_exception_str )
0 commit comments