1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
"""Possible Exceptions that are returned by the Szurubooru REST API
We define the general Exception `SzurubooruException` and possible child exceptions
defined in this module as well as the Szurubooru API docs:
https://github.com/rr-/szurubooru/blob/master/doc/API.md#error-handling
"""
import requests as r
import json
class SzurubooruException(Exception):
"""General Error returned from the Szurubooru REST API.
The Szurubooru API returns errors together with an http code in a form like this:
.. code-block:: JSON
{
"name": "Name of the error, e.g. 'PostNotFoundError'",
"title": "Generic title of error message, e.g. 'Not found'",
"description": "Detailed description of what went wrong, e.g.
'User `rr-` not found."
}
We map this json response to the SzurubooruException class or more specific children
such as MissingRequiredFileError with the fields / args that are provided from the
json response.
Args:
name: Name of the exception as defined by the Szurubooru REST API
The name is also used in more specific Exceptions such as
MissingRequiredFileError(SzurubooruException).
title: Generic, human readable title of the error message.
description: More detailed description of what went wrong. Often includes
input from the user that caused the error.
"""
name: str
title: str
description: str
def __init__(self, name: str, title: str, description: str) -> None:
self.name = name
self.title = title
self.description = description
super().__init__(description)
@staticmethod
def response_is_exception(res: r.Response) -> bool:
"""Check if the Szurubooru REST API response is an error or not.
Args:
res (r.Response): the Response of the request from the requests module.
Returns:
bool: True if response is an error and False if it is not.
"""
return res.status_code != 200
@staticmethod
def map_exception(res: r.Response) -> Exception:
"""Generate an exception for an error response from the Szurubooru REST API.
Args:
res (r.Response): the Response from the Szurubooru REST API that should be
mapped to a Python Exception.
Raises:
LookupError: Thrown when the Szurubooru REST API sent an error type (name)
that could not be mapped to one of the SzurubooruException
Python Exceptions. This can happen when there was an unexpected
e.g. an Internal Server Error in the Szurubooru REST API.
Returns:
Exception: Returns a more specific exception.
"""
content: dict[str, str] = json.loads(res.content)
name: str = content["name"]
title: str = content["title"]
description: str = content["description"]
match name:
case "MissingRequiredFileError":
return MissingRequiredFileError(name, title, description)
case "MissingRequiredParameterError":
return MissingRequiredParameterError(name, title, description)
case "InvalidParameterError":
return InvalidParameterError(name, title, description)
case "IntegrityError":
return IntegrityError(name, title, description)
case "SearchError":
return SearchError(name, title, description)
case "AuthError":
return AuthError(name, title, description)
case "PostNotFoundError":
return PostNotFoundError(name, title, description)
case "PostAlreadyFeaturedError":
return PostAlreadyFeaturedError(name, title, description)
case "PostAlreadyUploadedError":
return PostAlreadyUploadedError(name, title, description)
case "InvalidPostIdError":
return InvalidPostIdError(name, title, description)
case "InvalidPostSafetyError":
return InvalidPostSafetyError(name, title, description)
case "InvalidPostSourceError":
return InvalidPostSourceError(name, title, description)
case "InvalidPostContentError":
return InvalidPostContentError(name, title, description)
case "InvalidPostRelationError":
return InvalidPostRelationError(name, title, description)
case "InvalidPostNoteError":
return InvalidPostNoteError(name, title, description)
case "InvalidPostFlagError":
return InvalidPostFlagError(name, title, description)
case "InvalidFavoriteTargetError":
return InvalidFavoriteTargetError(name, title, description)
case "InvalidCommentIdError":
return InvalidCommentIdError(name, title, description)
case "CommentNotFoundError":
return CommentNotFoundError(name, title, description)
case "EmptyCommentTextError":
return EmptyCommentTextError(name, title, description)
case "InvalidScoreTargetError":
return InvalidScoreTargetError(name, title, description)
case "InvalidScoreValueError":
return InvalidScoreValueError(name, title, description)
case "TagCategoryNotFoundError":
return TagCategoryNotFoundError(name, title, description)
case "TagCategoryAlreadyExistsError":
return TagCategoryAlreadyExistsError(name, title, description)
case "TagCategoryIsInUseError":
return TagCategoryIsInUseError(name, title, description)
case "InvalidTagCategoryNameError":
return InvalidTagCategoryNameError(name, title, description)
case "InvalidTagCategoryColorError":
return InvalidTagCategoryColorError(name, title, description)
case "TagNotFoundError":
return TagNotFoundError(name, title, description)
case "TagAlreadyExistsError":
return TagAlreadyExistsError(name, title, description)
case "TagIsInUseError":
return TagIsInUseError(name, title, description)
case "InvalidTagNameError":
return InvalidTagNameError(name, title, description)
case "InvalidTagRelationError":
return InvalidTagRelationError(name, title, description)
case "InvalidTagCategoryError":
return InvalidTagCategoryError(name, title, description)
case "InvalidTagDescriptionError":
return InvalidTagDescriptionError(name, title, description)
case "UserNotFoundError":
return UserNotFoundError(name, title, description)
case "UserAlreadyExistsError":
return UserAlreadyExistsError(name, title, description)
case "InvalidUserNameError":
return InvalidUserNameError(name, title, description)
case "InvalidEmailError":
return InvalidEmailError(name, title, description)
case "InvalidPasswordError":
return InvalidPasswordError(name, title, description)
case "InvalidRankError":
return InvalidRankError(name, title, description)
case "InvalidAvatarError":
return InvalidAvatarError(name, title, description)
case "ProcessingError":
return ProcessingError(name, title, description)
case "ValidationError":
return ValidationError(name, title, description)
case _:
raise LookupError(f'Unknown SzurubooruException: "{name}"')
class MissingRequiredFileError(SzurubooruException): # noqa: D101
pass
class MissingRequiredParameterError(SzurubooruException): # noqa: D101
pass
class InvalidParameterError(SzurubooruException): # noqa: D101
pass
class IntegrityError(SzurubooruException): # noqa: D101
pass
class SearchError(SzurubooruException): # noqa: D101
pass
class AuthError(SzurubooruException): # noqa: D101
pass
class PostNotFoundError(SzurubooruException): # noqa: D101
pass
class PostAlreadyFeaturedError(SzurubooruException): # noqa: D101
pass
class PostAlreadyUploadedError(SzurubooruException): # noqa: D101
pass
class InvalidPostIdError(SzurubooruException): # noqa: D101
pass
class InvalidPostSafetyError(SzurubooruException): # noqa: D101
pass
class InvalidPostSourceError(SzurubooruException): # noqa: D101
pass
class InvalidPostContentError(SzurubooruException): # noqa: D101
pass
class InvalidPostRelationError(SzurubooruException): # noqa: D101
pass
class InvalidPostNoteError(SzurubooruException): # noqa: D101
pass
class InvalidPostFlagError(SzurubooruException): # noqa: D101
pass
class InvalidFavoriteTargetError(SzurubooruException): # noqa: D101
pass
class InvalidCommentIdError(SzurubooruException): # noqa: D101
pass
class CommentNotFoundError(SzurubooruException): # noqa: D101
pass
class EmptyCommentTextError(SzurubooruException): # noqa: D101
pass
class InvalidScoreTargetError(SzurubooruException): # noqa: D101
pass
class InvalidScoreValueError(SzurubooruException): # noqa: D101
pass
class TagCategoryNotFoundError(SzurubooruException): # noqa: D101
pass
class TagCategoryAlreadyExistsError(SzurubooruException): # noqa: D101
pass
class TagCategoryIsInUseError(SzurubooruException): # noqa: D101
pass
class InvalidTagCategoryNameError(SzurubooruException): # noqa: D101
pass
class InvalidTagCategoryColorError(SzurubooruException): # noqa: D101
pass
class TagNotFoundError(SzurubooruException): # noqa: D101
pass
class TagAlreadyExistsError(SzurubooruException): # noqa: D101
pass
class TagIsInUseError(SzurubooruException): # noqa: D101
pass
class InvalidTagNameError(SzurubooruException): # noqa: D101
pass
class InvalidTagRelationError(SzurubooruException): # noqa: D101
pass
class InvalidTagCategoryError(SzurubooruException): # noqa: D101
pass
class InvalidTagDescriptionError(SzurubooruException): # noqa: D101
pass
class UserNotFoundError(SzurubooruException): # noqa: D101
pass
class UserAlreadyExistsError(SzurubooruException): # noqa: D101
pass
class InvalidUserNameError(SzurubooruException): # noqa: D101
pass
class InvalidEmailError(SzurubooruException): # noqa: D101
pass
class InvalidPasswordError(SzurubooruException): # noqa: D101
pass
class InvalidRankError(SzurubooruException): # noqa: D101
pass
class InvalidAvatarError(SzurubooruException): # noqa: D101
pass
class ProcessingError(SzurubooruException): # noqa: D101
pass
class ValidationError(SzurubooruException): # noqa: D101
pass
|