Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A set of HTTP Exceptions for use in Request Handlers
# -*- coding: utf-8 -*-
#
# Author: Craig Russell <craig@craig-russell.co.uk>
# Simple exceptions for HTTP Errors
# http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
#
# Example:
#
# try:
# raise BadRequest()
# except BaseHttpException as e:
# print e.status_code
# print e.message
class BaseHttpException(Exception):
def __init__(self, message=None, status_code=None):
Exception.__init__(self)
if message: self.message = message
if status_code: self.status_code = status_code
class BadRequest(BaseHttpException):
status_code = 400
message = "Bad Request"
class Unauthorized(BaseHttpException):
status_code = 401
message = "Unauthorized"
class PaymentRequired(BaseHttpException):
status_code = 402
message = "Payment Required"
class Forbidden(BaseHttpException):
status_code = 403
message = "Forbidden"
class NotFound(BaseHttpException):
status_code = 404
message = "Not Found"
class MethodNotAllowed(BaseHttpException):
status_code = 405
message = "Method Not Allowed"
class NotAcceptable(BaseHttpException):
status_code = 406
message = "Not Acceptable"
class ProxyAuthenticationRequired(BaseHttpException):
status_code = 407
message = "Proxy Authentication Required"
class RequestTimeout(BaseHttpException):
status_code = 408
message = "Request Timeout"
class Conflict(BaseHttpException):
status_code = 409
message = "Conflict"
class Gone(BaseHttpException):
status_code = 410
message = "Gone"
class LengthRequired(BaseHttpException):
status_code = 411
message = "Length Required"
class PreconditionFailed(BaseHttpException):
status_code = 412
message = "Precondition Failed"
class PayloadTooLarge(BaseHttpException):
status_code = 413
message = "Payload Too Large"
class URITooLong(BaseHttpException):
status_code = 414
message = "URI Too Long"
class UnsupportedMediaType(BaseHttpException):
status_code = 415
message = "Unsupported Media Type"
class RangeNotSatisfiable(BaseHttpException):
status_code = 416
message = "Range Not Satisfiable"
class ExpectationFailed(BaseHttpException):
status_code = 417
message = "Expectation Failed"
class UnprocessableEntity(BaseHttpException):
status_code = 422
message = "Unprocessable Entity"
class Locked(BaseHttpException):
status_code = 423
message = "Locked"
class FailedDependency(BaseHttpException):
status_code = 424
message = "Failed Dependency"
class UpgradeRequired(BaseHttpException):
status_code = 426
message = "Upgrade Required"
class PreconditionRequired(BaseHttpException):
status_code = 428
message = "Precondition Required"
class TooManyRequests(BaseHttpException):
status_code = 429
message = "Too Many Requests"
class RequestHeaderFieldsTooLarge(BaseHttpException):
status_code = 431
message = "Request Header Fields Too Large"
class InternalServerError(BaseHttpException):
status_code = 500
message = "Internal Server Error"
class NotImplemented(BaseHttpException):
status_code = 501
message = "Not Implemented"
class BadGateway(BaseHttpException):
status_code = 502
message = "Bad Gateway"
class ServiceUnavailable(BaseHttpException):
status_code = 503
message = "Service Unavailable"
class GatewayTimeout(BaseHttpException):
status_code = 504
message = "Gateway Timeout"
class HTTPVersionNotSupported(BaseHttpException):
status_code = 505
message = "HTTP Version Not Supported"
class VariantAlsoNegotiates(BaseHttpException):
status_code = 506
message = "Variant Also Negotiates"
class InsufficientStorage(BaseHttpException):
status_code = 507
message = "Insufficient Storage"
class LoopDetected(BaseHttpException):
status_code = 508
message = "Loop Detected"
class NotExtended(BaseHttpException):
status_code = 510
message = "Not Extended"
class NetworkAuthenticationRequired(BaseHttpException):
status_code = 511
message = "Network Authentication Required"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment