-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path__init__.py
More file actions
97 lines (75 loc) · 2.83 KB
/
Copy path__init__.py
File metadata and controls
97 lines (75 loc) · 2.83 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
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
import typing as t
from starlette import status
from starlette.exceptions import HTTPException
from .base import APIException, ErrorDetail
from .validation import RequestValidationError, WebSocketRequestValidationError
__all__ = [
"HTTPException",
"ImproperConfiguration",
"APIException",
"WebSocketRequestValidationError",
"RequestValidationError",
"AuthenticationFailed",
"NotAuthenticated",
"PermissionDenied",
"NotFound",
"MethodNotAllowed",
"NotAcceptable",
"UnsupportedMediaType",
]
class ImproperConfiguration(Exception):
pass
class AuthenticationFailed(APIException):
status_code = status.HTTP_401_UNAUTHORIZED
default_detail = "Incorrect authentication credentials."
default_code = "authentication_failed"
class NotAuthenticated(APIException):
status_code = status.HTTP_401_UNAUTHORIZED
default_detail = "Authentication credentials were not provided."
default_code = "not_authenticated"
class PermissionDenied(APIException):
status_code = status.HTTP_403_FORBIDDEN
default_detail = "You do not have permission to perform this action."
default_code = "permission_denied"
class NotFound(APIException):
status_code = status.HTTP_404_NOT_FOUND
default_detail = "Not found."
default_code = "not_found"
class MethodNotAllowed(APIException):
status_code = status.HTTP_405_METHOD_NOT_ALLOWED
default_detail = 'Method "{method}" not allowed.'
default_code = "method_not_allowed"
def __init__(
self,
method: str,
detail: t.Optional[t.Union[t.List, t.Dict, ErrorDetail, str]] = None,
code: t.Optional[t.Union[str, int]] = None,
):
if detail is None:
detail = str(self.default_detail).format(method=method)
super().__init__(detail, code)
class NotAcceptable(APIException):
status_code = status.HTTP_406_NOT_ACCEPTABLE
default_detail = "Could not satisfy the request Accept header."
default_code = "not_acceptable"
def __init__(
self,
detail: t.Optional[t.Union[t.List, t.Dict, ErrorDetail, str]] = None,
code: t.Optional[t.Union[str, int]] = None,
available_renderers: t.Optional[str] = None,
):
self.available_renderers = available_renderers
super().__init__(detail, code)
class UnsupportedMediaType(APIException):
status_code = status.HTTP_415_UNSUPPORTED_MEDIA_TYPE
default_detail = 'Unsupported media type "{media_type}" in request.'
default_code = "unsupported_media_type"
def __init__(
self,
media_type: str,
detail: t.Optional[t.Union[t.List, t.Dict, ErrorDetail, str]] = None,
code: t.Optional[t.Union[str, int]] = None,
):
if detail is None:
detail = str(self.default_detail).format(media_type=media_type)
super().__init__(detail, code)