-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_errors.py
More file actions
64 lines (40 loc) · 1.36 KB
/
Copy pathapi_errors.py
File metadata and controls
64 lines (40 loc) · 1.36 KB
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
"""Structured API errors with stable machine-readable codes.
Handlers raise these; the dispatcher maps each class to an HTTP status and a JSON body with error/code/request_id.
"""
class ApiError(Exception):
"""Base class for all structured API errors."""
status = 500
code = "INTERNAL_ERROR"
def __init__(self, message, *, code=None, detail=None):
super().__init__(message)
self.message = str(message)
self.code = code or self.code
self.detail = detail
def to_payload(self, request_id=""):
payload = {"error": self.message, "code": self.code}
if self.detail is not None:
payload["detail"] = self.detail
if request_id:
payload["request_id"] = request_id
return payload
class BadRequest(ApiError):
status = 400
code = "BAD_REQUEST"
class NotFound(ApiError):
status = 404
code = "NOT_FOUND"
class GameNotFound(NotFound):
code = "GAME_NOT_FOUND"
class MediaNotFound(NotFound):
code = "MEDIA_NOT_FOUND"
class DocumentNotFound(NotFound):
code = "DOCUMENT_NOT_FOUND"
class BadgeNotFound(NotFound):
code = "BADGE_NOT_FOUND"
class PlatformDocumentNotFound(NotFound):
code = "PLATFORM_DOCUMENT_NOT_FOUND"
class RouteNotFound(NotFound):
code = "ROUTE_NOT_FOUND"
class Conflict(ApiError):
status = 409
code = "CONFLICT"