Files
CN2021/httplib/exceptions.py

164 lines
3.1 KiB
Python

class HTTPException(Exception):
"""
Base class for HTTP exceptions
"""
class UnhandledHTTPCode(Exception):
"""
Exception thrown if HTTP codes are not further processed.
"""
status_code: str
headers: str
cause: str
def __init__(self, status, headers, cause):
self.status_code = status
self.headers = headers
self.cause = cause
class InvalidResponse(HTTPException):
"""
Response message cannot be parsed
"""
def __init__(self, message):
self.message = message
class InvalidStatusLine(HTTPException):
"""
Response status line is invalid
"""
def __init__(self, line):
self.line = line
class UnsupportedEncoding(HTTPException):
"""
Encoding not supported
"""
def __init__(self, enc_type, encoding):
self.enc_type = enc_type
self.encoding = encoding
class UnsupportedProtocol(HTTPException):
"""
Protocol is not supported
"""
def __init__(self, protocol):
self.protocol = protocol
class IncompleteResponse(HTTPException):
def __init__(self, cause):
self.cause = cause
class HTTPServerException(HTTPException):
"""
Base class for HTTP Server exceptions
"""
status_code: str
message: str
arg: str
def __init__(self, arg):
self.arg = arg
class HTTPServerCloseException(HTTPServerException):
"""
When raised, the connection should be closed
"""
class BadRequest(HTTPServerCloseException):
"""
Malformed HTTP request
"""
status_code = 400
message = "Bad Request"
class Forbidden(HTTPServerException):
"""
Request not allowed
"""
status_code = 403
message = "Forbidden"
class NotFound(HTTPServerException):
"""
Resource not found
"""
status_code = 404
message = "Not Found"
class MethodNotAllowed(HTTPServerException):
"""
Method is not allowed
"""
status_code = 405
message = "Method Not Allowed"
def __init__(self, allowed_methods):
self.allowed_methods = allowed_methods
class InternalServerError(HTTPServerCloseException):
"""
Internal Server Error
"""
status_code = 500
message = "Internal Server Error"
class NotImplemented(HTTPServerException):
"""
Functionality not implemented
"""
status_code = 501
message = "Not Implemented"
class HTTPVersionNotSupported(HTTPServerCloseException):
"""
The server does not support the major version HTTP used in the request message
"""
status_code = 505
message = "HTTP Version Not Supported"
class Conflict(HTTPServerException):
"""
Conflict in the current state of the target resource
"""
status_code = 409
message = "Conflict"
class NotModified(HTTPServerException):
"""
Requested resource was not modified
"""
status_code = 304
message = "Not Modified"
class InvalidRequestLine(BadRequest):
"""
Request start-line is invalid
"""
def __init__(self, line, arg):
super().__init__(arg)
self.request_line = line