Skip to content

Commit a186759

Browse files
refactor(event_handler): allow to pass dict as argument to exception classes (#7341)
add docstrings to mention message can be either string or dictionary
1 parent 1726feb commit a186759

File tree

1 file changed

+61
-11
lines changed

1 file changed

+61
-11
lines changed
Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1+
from __future__ import annotations
2+
13
from http import HTTPStatus
24

35

46
class ServiceError(Exception):
57
"""Powertools class HTTP Service Error"""
68

7-
def __init__(self, status_code: int, msg: str):
9+
def __init__(self, status_code: int, msg: str | dict):
810
"""
911
Parameters
1012
----------
1113
status_code: int
1214
Http status code
13-
msg: str
14-
Error message
15+
msg: str | dict
16+
Error message. Can be a string or a dictionary
1517
"""
1618
self.status_code = status_code
1719
self.msg = msg
@@ -20,54 +22,102 @@ def __init__(self, status_code: int, msg: str):
2022
class BadRequestError(ServiceError):
2123
"""Powertools class Bad Request Error (400)"""
2224

23-
def __init__(self, msg: str):
25+
def __init__(self, msg: str | dict):
26+
"""
27+
Parameters
28+
----------
29+
msg : str | dict
30+
Error message. Can be a string or a dictionary.
31+
"""
2432
super().__init__(HTTPStatus.BAD_REQUEST, msg)
2533

2634

2735
class UnauthorizedError(ServiceError):
2836
"""Powertools class Unauthorized Error (401)"""
2937

30-
def __init__(self, msg: str):
38+
def __init__(self, msg: str | dict):
39+
"""
40+
Parameters
41+
----------
42+
msg : str | dict
43+
Error message. Can be a string or a dictionary.
44+
"""
3145
super().__init__(HTTPStatus.UNAUTHORIZED, msg)
3246

3347

3448
class ForbiddenError(ServiceError):
3549
"""Powertools class Forbidden Error (403)"""
3650

37-
def __init__(self, msg: str):
51+
def __init__(self, msg: str | dict):
52+
"""
53+
Parameters
54+
----------
55+
msg : str | dict
56+
Error message. Can be a string or a dictionary.
57+
"""
3858
super().__init__(HTTPStatus.FORBIDDEN, msg)
3959

4060

4161
class NotFoundError(ServiceError):
4262
"""Powertools class Not Found Error (404)"""
4363

44-
def __init__(self, msg: str = "Not found"):
64+
def __init__(self, msg: str | dict = "Not found"):
65+
"""
66+
Parameters
67+
----------
68+
msg : str | dict
69+
Error message. Can be a string or a dictionary.
70+
"""
4571
super().__init__(HTTPStatus.NOT_FOUND, msg)
4672

4773

4874
class RequestTimeoutError(ServiceError):
4975
"""Powertools class Request Timeout Error (408)"""
5076

51-
def __init__(self, msg: str):
77+
def __init__(self, msg: str | dict):
78+
"""
79+
Parameters
80+
----------
81+
msg : str | dict
82+
Error message. Can be a string or a dictionary.
83+
"""
5284
super().__init__(HTTPStatus.REQUEST_TIMEOUT, msg)
5385

5486

5587
class RequestEntityTooLargeError(ServiceError):
5688
"""Powertools class Request Entity Too Large Error (413)"""
5789

58-
def __init__(self, msg: str):
90+
def __init__(self, msg: str | dict):
91+
"""
92+
Parameters
93+
----------
94+
msg : str | dict
95+
Error message. Can be a string or a dictionary.
96+
"""
5997
super().__init__(HTTPStatus.REQUEST_ENTITY_TOO_LARGE, msg)
6098

6199

62100
class InternalServerError(ServiceError):
63101
"""Powertools class Internal Server Error (500)"""
64102

65-
def __init__(self, message: str):
103+
def __init__(self, message: str | dict):
104+
"""
105+
Parameters
106+
----------
107+
msg : str | dict
108+
Error message. Can be a string or a dictionary.
109+
"""
66110
super().__init__(HTTPStatus.INTERNAL_SERVER_ERROR, message)
67111

68112

69113
class ServiceUnavailableError(ServiceError):
70114
"""Powertools class Service Unavailable Error (503)"""
71115

72-
def __init__(self, msg: str):
116+
def __init__(self, msg: str | dict):
117+
"""
118+
Parameters
119+
----------
120+
msg : str | dict
121+
Error message. Can be a string or a dictionary.
122+
"""
73123
super().__init__(HTTPStatus.SERVICE_UNAVAILABLE, msg)

0 commit comments

Comments
 (0)