module documentation

Implements a number of Python exceptions which can be raised from within a view to trigger a standard HTTP non-200 response.

Usage Example

from werkzeug.wrappers.request import Request
from werkzeug.exceptions import HTTPException, NotFound

def view(request):
    raise NotFound()

@Request.application
def application(request):
    try:
        return view(request)
    except HTTPException as e:
        return e

As you can see from this example those exceptions are callable WSGI applications. However, they are not Werkzeug response objects. You can get a response object by calling get_response() on a HTTP exception.

Keep in mind that you may have to pass an environ (WSGI) or scope (ASGI) to get_response() because some errors fetch additional information relating to the request.

If you want to hook in a different exception page to say, a 404 status code, you can add a second except for a specific subclass of an error:

@Request.application
def application(request):
    try:
        return view(request)
    except NotFound as e:
        return not_found(request)
    except HTTPException as e:
        return e
Class ​Aborter No summary
Class ​Bad​Gateway 502 Bad Gateway
Class ​Bad​Host Raised if the submitted host is badly formatted.
Class ​Bad​Request 400 Bad Request
Class ​Bad​Request​Key​Error An exception that is used to signal both a KeyError and a BadRequest. Used by many of the datastructures.
Class ​Client​Disconnected No summary
Class ​Conflict 409 Conflict
Class ​Expectation​Failed 417 Expectation Failed
Class ​Failed​Dependency 424 Failed Dependency
Class ​Forbidden 403 Forbidden
Class ​Gateway​Timeout 504 Gateway Timeout
Class ​Gone 410 Gone
Class ​HTTPException The base class for all HTTP exceptions. This exception can be called as a WSGI application to render a default error page or you can catch the subclasses of it independently and render nicer error messages.
Class ​HTTPVersion​Not​Supported 505 HTTP Version Not Supported
Class ​Im​ATeapot 418 I'm a teapot
Class ​Internal​Server​Error 500 Internal Server Error
Class ​Length​Required 411 Length Required
Class ​Locked 423 Locked
Class ​Method​Not​Allowed 405 Method Not Allowed
Class ​Not​Acceptable 406 Not Acceptable
Class ​Not​Found 404 Not Found
Class ​Not​Implemented 501 Not Implemented
Class ​Precondition​Failed 412 Precondition Failed
Class ​Precondition​Required 428 Precondition Required
Class ​Requested​Range​Not​Satisfiable 416 Requested Range Not Satisfiable
Class ​Request​Entity​Too​Large 413 Request Entity Too Large
Class ​Request​Header​Fields​Too​Large 431 Request Header Fields Too Large
Class ​Request​Timeout 408 Request Timeout
Class ​Request​URIToo​Large 414 Request URI Too Large
Class ​Security​Error Raised if something triggers a security error. This is otherwise exactly like a bad request error.
Class ​Service​Unavailable 503 Service Unavailable
Class ​Too​Many​Requests 429 Too Many Requests
Class ​Unauthorized 401 Unauthorized
Class ​Unavailable​For​Legal​Reasons 451 Unavailable For Legal Reasons
Class ​Unprocessable​Entity 422 Unprocessable Entity
Class ​Unsupported​Media​Type 415 Unsupported Media Type
Function abort Raises an HTTPException for the given status code or WSGI application.
Variable default​_exceptions Undocumented
Class _​Retry​After Adds an optional retry_after parameter which will set the Retry-After header. May be an int number of seconds or a ~datetime.datetime.
Function ​_find​_exceptions Undocumented
Variable ​_aborter Undocumented
def abort(status, *args, **kwargs):

Raises an HTTPException for the given status code or WSGI application.

If a status code is given, it will be looked up in the list of exceptions and will raise that exception. If passed a WSGI application, it will wrap it in a proxy WSGI exception and raise that:

abort(404)  # 404 Not Found
abort(Response('Hello World'))
Parameters
status:t.Union[int, Response]Undocumented
*args:t.AnyUndocumented
**kwargs:t.AnyUndocumented
Returns
te.NoReturnUndocumented
default_exceptions: t.Dict[int, t.Type[HTTPException]] =

Undocumented

def _find_exceptions():

Undocumented

_aborter: Aborter =

Undocumented