Skip to content

Commit cf3b01a

Browse files
committed
chore: cleanup exceptions
1 parent e1dc4cf commit cf3b01a

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

aws_lambda_powertools/utilities/batch/exceptions.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,16 @@
22
Batch processing exceptions
33
"""
44
import traceback
5+
from typing import Optional, Tuple
56

67

7-
class SQSBatchProcessingError(Exception):
8-
"""When at least one message within a batch could not be processed"""
9-
8+
class BaseBatchProcessingError(Exception):
109
def __init__(self, msg="", child_exceptions=()):
1110
super().__init__(msg)
1211
self.msg = msg
1312
self.child_exceptions = child_exceptions
1413

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):
1915
exception_list = [f"{parent_exception_str}\n"]
2016
for exception in self.child_exceptions:
2117
extype, ex, tb = exception
@@ -25,22 +21,25 @@ def __str__(self):
2521
return "\n".join(exception_list)
2622

2723

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"""
3026

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)
3529

3630
# Overriding this method so we can output all child exception tracebacks when we raise this exception to prevent
3731
# errors being lost. See https://github.com/awslabs/aws-lambda-powertools-python/issues/275
3832
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)
4535

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

Comments
 (0)