Skip to content

Instantly share code, notes, and snippets.

@williamsjj
Created March 10, 2011 22:41
Show Gist options
  • Save williamsjj/865097 to your computer and use it in GitHub Desktop.
Save williamsjj/865097 to your computer and use it in GitHub Desktop.
class Error(Exception):
"""
A basic HTTP error.
@type status: C{str}
@ivar status: Refers to an HTTP status code, for example L{http.NOT_FOUND}.
@type message: C{str}
@param message: A short error message, for example "NOT FOUND".
@type response: C{str}
@ivar response: A complete HTML document for an error page.
"""
def __init__(self, code, message=None, response=None):
"""
Initializes a basic exception.
@type code: C{str}
@param code: Refers to an HTTP status code, for example
L{http.NOT_FOUND}. If no C{message} is given, C{code} is mapped to a
descriptive string that is used instead.
@type message: C{str}
@param message: A short error message, for example "NOT FOUND".
@type response: C{str}
@param response: A complete HTML document for an error page.
"""
if not message:
try:
message = http.responses.get(int(code))
except ValueError:
# If code wasn't a stringified int, can't map the
# status code to a descriptive string so keep message
# unchanged.
pass
Exception.__init__(self, code, message, response)
self.status = code
self.message = message
self.response = response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment