Skip to content

Commit

Permalink
Added several HTTP error exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
ElSaico committed Jun 28, 2016
1 parent a4994eb commit e2aff93
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 2 deletions.
14 changes: 14 additions & 0 deletions restless/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,22 @@

BAD_REQUEST = 400
UNAUTHORIZED = 401
FORBIDDEN = 403
NOT_FOUND = 404
METHOD_NOT_ALLOWED = 405
NOT_ACCEPTABLE = 406
CONFLICT = 409
GONE = 410
PRECONDITION_FAILED = 412
UNSUPPORTED_MEDIA_TYPE = 415
EXPECTATION_FAILED = 417
I_AM_A_TEAPOT = 418
UNPROCESSABLE_ENTITY = 422
LOCKED = 423
FAILED_DEPENDENCY = 424
TOO_MANY_REQUESTS = 429
UNAVAILABLE_FOR_LEGAL_REASONS = 451

APPLICATION_ERROR = 500
METHOD_NOT_IMPLEMENTED = 501
UNAVAILABLE = 503
80 changes: 78 additions & 2 deletions restless/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
from .constants import APPLICATION_ERROR, UNAUTHORIZED, NOT_FOUND, BAD_REQUEST
from .constants import METHOD_NOT_ALLOWED, METHOD_NOT_IMPLEMENTED
from .constants import (APPLICATION_ERROR, UNAUTHORIZED, NOT_FOUND, BAD_REQUEST,
FORBIDDEN, NOT_ACCEPTABLE, GONE, PRECONDITION_FAILED,
CONFLICT, UNSUPPORTED_MEDIA_TYPE, EXPECTATION_FAILED,
I_AM_A_TEAPOT, TOO_MANY_REQUESTS, UNPROCESSABLE_ENTITY,
UNAVAILABLE_FOR_LEGAL_REASONS, FAILED_DEPENDENCY,
LOCKED)
from .constants import METHOD_NOT_ALLOWED, METHOD_NOT_IMPLEMENTED, UNAVAILABLE


class RestlessError(Exception):
Expand Down Expand Up @@ -44,6 +49,11 @@ class Unauthorized(HttpError):
msg = "Unauthorized."


class Forbidden(HttpError):
status = FORBIDDEN
msg = "Permission denied."


class NotFound(HttpError):
status = NOT_FOUND
msg = "Resource not found."
Expand All @@ -54,6 +64,72 @@ class MethodNotAllowed(HttpError):
msg = "The specified HTTP method is not allowed."


class NotAcceptable(HttpError):
# TODO: make serializers handle it?
status = NOT_ACCEPTABLE
msg = "Unable to send content specified on the request's Accept header(s)."


class Conflict(HttpError):
status = CONFLICT
msg = "There was a conflict when processing the request."


class Gone(HttpError):
status = GONE
msg = "Resource removed permanently."


class PreconditionFailed(HttpError):
status = PRECONDITION_FAILED
msg = "Unable to satisfy one or more request preconditions."


class UnsupportedMediaType(HttpError):
status = UNSUPPORTED_MEDIA_TYPE
msg = "Type of media provided on request is not supported."


class ExpectationFailed(HttpError):
status = EXPECTATION_FAILED
msg = "Unable to satisfy requirements of Expect header."


class IAmATeapot(HttpError):
status = I_AM_A_TEAPOT
msg = "This is a teapot; do not attempt to brew coffee with it."


class UnprocessableEntity(HttpError):
status = UNPROCESSABLE_ENTITY
msg = "Request cannot be followed due to a semantic error."


class Locked(HttpError):
status = LOCKED
msg = "Resource is locked."


class FailedDependency(HttpError):
status = FAILED_DEPENDENCY
msg = "Request failed due to a previous failed request."


class TooManyRequests(HttpError):
status = TOO_MANY_REQUESTS
msg = "There was a conflict when processing the request."


class UnavailableForLegalReasons(HttpError):
status = UNAVAILABLE_FOR_LEGAL_REASONS
msg = "Resource made unavailable by a legal decision."


class MethodNotImplemented(HttpError):
status = METHOD_NOT_IMPLEMENTED
msg = "The specified HTTP method is not implemented."


class Unavailable(HttpError):
status = UNAVAILABLE
msg = "There was a conflict when processing the request."

0 comments on commit e2aff93

Please sign in to comment.