1
1
"""The module which defines the base functionality for error responses that will be returned by the API.
2
+
2
3
This module only includes the generic utilities using which each module should define its own error responses
3
4
specifically. See :obj:`trolldb.database.errors` as an example on how this module is used.
4
5
"""
@@ -18,19 +19,18 @@ class ResponseError(Exception):
18
19
"""The base class for all error responses. This is derivative of the ``Exception`` class."""
19
20
20
21
descriptor_delimiter : str = " |OR| "
21
- """
22
- A delimiter to combine the message part of several error responses into a single one. This will be shown in textual
23
- format for the response descriptors of the Fast API routes. For example:
22
+ """A delimiter to combine the message part of several error responses into a single one.
23
+
24
+ This will be shown in textual format for the response descriptors of the Fast API routes. For example:
24
25
25
26
``ErrorA |OR| ErrorB``
26
27
"""
27
28
28
- defaultResponseClass : Response = PlainTextResponse
29
- """
30
- The default type of the response which will be returned when an error occurs.
31
- """
29
+ DefaultResponseClass : Response = PlainTextResponse
30
+ """The default type of the response which will be returned when an error occurs."""
32
31
33
32
def __init__ (self , args_dict : OrderedDict [StatusCode , str | list [str ]] | dict ) -> None :
33
+ """TODO."""
34
34
self .__dict : OrderedDict = OrderedDict (args_dict )
35
35
self .extra_information : dict | None = None
36
36
@@ -64,6 +64,7 @@ def __or__(self, other: Self):
64
64
def __assert_existence_multiple_response_codes (
65
65
self ,
66
66
status_code : StatusCode | None = None ) -> (StatusCode , str ):
67
+ """TODO."""
67
68
match status_code , len (self .__dict ):
68
69
case None , n if n > 1 :
69
70
raise ValueError ("In case of multiple response status codes, the status code must be specified." )
@@ -80,6 +81,7 @@ def get_error_details(
80
81
self ,
81
82
extra_information : dict | None = None ,
82
83
status_code : int | None = None ) -> (StatusCode , str ):
84
+ """TODO."""
83
85
status_code , msg = self .__assert_existence_multiple_response_codes (status_code )
84
86
return (
85
87
status_code ,
@@ -91,6 +93,7 @@ def sys_exit_log(
91
93
exit_code : int = - 1 ,
92
94
extra_information : dict | None = None ,
93
95
status_code : int | None = None ) -> None :
96
+ """TODO."""
94
97
msg , _ = self .get_error_details (extra_information , status_code )
95
98
logger .error (msg )
96
99
exit (exit_code )
@@ -99,32 +102,39 @@ def log_as_warning(
99
102
self ,
100
103
extra_information : dict | None = None ,
101
104
status_code : int | None = None ):
105
+ """TODO."""
102
106
msg , _ = self .get_error_details (extra_information , status_code )
103
107
logger .warning (msg )
104
108
105
109
@property
106
110
def fastapi_descriptor (self ) -> dict [StatusCode , dict [Literal ["description" ], str ]]:
111
+ """TODO."""
107
112
return {status : {Literal ["description" ]: ResponseError .__stringify (msg )} for status , msg in self .__dict .items ()}
108
113
109
114
@staticmethod
110
115
def __listify (item : str | list [str ]) -> list [str ]:
116
+ """TODO."""
111
117
return item if isinstance (item , list ) else [item ]
112
118
113
119
@staticmethod
114
120
def __stringify (item : str | list [str ]) -> str :
121
+ """TODO."""
115
122
return ResponseError .descriptor_delimiter .join (ResponseError .__listify (item ))
116
123
117
124
118
125
class ResponsesErrorGroup :
126
+ """TODO."""
119
127
120
128
@classmethod
121
129
def fields (cls ):
130
+ """TODO."""
122
131
return {k : v for k , v in cls .__dict__ .items () if isinstance (v , ResponseError )}
123
132
124
133
@classmethod
125
134
def union (cls ):
135
+ """TODO."""
126
136
buff = None
127
- for k , v in cls .fields ().items ():
137
+ for v in cls .fields ().values ():
128
138
if buff is None :
129
139
buff = v
130
140
else :
0 commit comments