Skip to content

Commit e2aff93

Browse files
committed
Added several HTTP error exceptions
1 parent a4994eb commit e2aff93

File tree

2 files changed

+92
-2
lines changed

2 files changed

+92
-2
lines changed

Diff for: restless/constants.py

+14
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,22 @@
66

77
BAD_REQUEST = 400
88
UNAUTHORIZED = 401
9+
FORBIDDEN = 403
910
NOT_FOUND = 404
1011
METHOD_NOT_ALLOWED = 405
12+
NOT_ACCEPTABLE = 406
13+
CONFLICT = 409
14+
GONE = 410
15+
PRECONDITION_FAILED = 412
16+
UNSUPPORTED_MEDIA_TYPE = 415
17+
EXPECTATION_FAILED = 417
18+
I_AM_A_TEAPOT = 418
19+
UNPROCESSABLE_ENTITY = 422
20+
LOCKED = 423
21+
FAILED_DEPENDENCY = 424
22+
TOO_MANY_REQUESTS = 429
23+
UNAVAILABLE_FOR_LEGAL_REASONS = 451
1124

1225
APPLICATION_ERROR = 500
1326
METHOD_NOT_IMPLEMENTED = 501
27+
UNAVAILABLE = 503

Diff for: restless/exceptions.py

+78-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
from .constants import APPLICATION_ERROR, UNAUTHORIZED, NOT_FOUND, BAD_REQUEST
2-
from .constants import METHOD_NOT_ALLOWED, METHOD_NOT_IMPLEMENTED
1+
from .constants import (APPLICATION_ERROR, UNAUTHORIZED, NOT_FOUND, BAD_REQUEST,
2+
FORBIDDEN, NOT_ACCEPTABLE, GONE, PRECONDITION_FAILED,
3+
CONFLICT, UNSUPPORTED_MEDIA_TYPE, EXPECTATION_FAILED,
4+
I_AM_A_TEAPOT, TOO_MANY_REQUESTS, UNPROCESSABLE_ENTITY,
5+
UNAVAILABLE_FOR_LEGAL_REASONS, FAILED_DEPENDENCY,
6+
LOCKED)
7+
from .constants import METHOD_NOT_ALLOWED, METHOD_NOT_IMPLEMENTED, UNAVAILABLE
38

49

510
class RestlessError(Exception):
@@ -44,6 +49,11 @@ class Unauthorized(HttpError):
4449
msg = "Unauthorized."
4550

4651

52+
class Forbidden(HttpError):
53+
status = FORBIDDEN
54+
msg = "Permission denied."
55+
56+
4757
class NotFound(HttpError):
4858
status = NOT_FOUND
4959
msg = "Resource not found."
@@ -54,6 +64,72 @@ class MethodNotAllowed(HttpError):
5464
msg = "The specified HTTP method is not allowed."
5565

5666

67+
class NotAcceptable(HttpError):
68+
# TODO: make serializers handle it?
69+
status = NOT_ACCEPTABLE
70+
msg = "Unable to send content specified on the request's Accept header(s)."
71+
72+
73+
class Conflict(HttpError):
74+
status = CONFLICT
75+
msg = "There was a conflict when processing the request."
76+
77+
78+
class Gone(HttpError):
79+
status = GONE
80+
msg = "Resource removed permanently."
81+
82+
83+
class PreconditionFailed(HttpError):
84+
status = PRECONDITION_FAILED
85+
msg = "Unable to satisfy one or more request preconditions."
86+
87+
88+
class UnsupportedMediaType(HttpError):
89+
status = UNSUPPORTED_MEDIA_TYPE
90+
msg = "Type of media provided on request is not supported."
91+
92+
93+
class ExpectationFailed(HttpError):
94+
status = EXPECTATION_FAILED
95+
msg = "Unable to satisfy requirements of Expect header."
96+
97+
98+
class IAmATeapot(HttpError):
99+
status = I_AM_A_TEAPOT
100+
msg = "This is a teapot; do not attempt to brew coffee with it."
101+
102+
103+
class UnprocessableEntity(HttpError):
104+
status = UNPROCESSABLE_ENTITY
105+
msg = "Request cannot be followed due to a semantic error."
106+
107+
108+
class Locked(HttpError):
109+
status = LOCKED
110+
msg = "Resource is locked."
111+
112+
113+
class FailedDependency(HttpError):
114+
status = FAILED_DEPENDENCY
115+
msg = "Request failed due to a previous failed request."
116+
117+
118+
class TooManyRequests(HttpError):
119+
status = TOO_MANY_REQUESTS
120+
msg = "There was a conflict when processing the request."
121+
122+
123+
class UnavailableForLegalReasons(HttpError):
124+
status = UNAVAILABLE_FOR_LEGAL_REASONS
125+
msg = "Resource made unavailable by a legal decision."
126+
127+
57128
class MethodNotImplemented(HttpError):
58129
status = METHOD_NOT_IMPLEMENTED
59130
msg = "The specified HTTP method is not implemented."
131+
132+
133+
class Unavailable(HttpError):
134+
status = UNAVAILABLE
135+
msg = "There was a conflict when processing the request."

0 commit comments

Comments
 (0)