Implements a number of Python exceptions which can be raised from within a view to trigger a standard HTTP non-200 response.
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 | BadGateway |
502 Bad Gateway |
Class | BadHost |
Raised if the submitted host is badly formatted. |
Class | BadRequest |
400 Bad Request |
Class | BadRequestKeyError |
An exception that is used to signal both a KeyError and a BadRequest . Used by many of the datastructures. |
Class | ClientDisconnected |
No summary |
Class | Conflict |
409 Conflict |
Class | ExpectationFailed |
417 Expectation Failed |
Class | FailedDependency |
424 Failed Dependency |
Class | Forbidden |
403 Forbidden |
Class | GatewayTimeout |
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 | HTTPVersionNotSupported |
505 HTTP Version Not Supported |
Class | ImATeapot |
418 I'm a teapot |
Class | InternalServerError |
500 Internal Server Error |
Class | LengthRequired |
411 Length Required |
Class | Locked |
423 Locked |
Class | MethodNotAllowed |
405 Method Not Allowed |
Class | NotAcceptable |
406 Not Acceptable |
Class | NotFound |
404 Not Found |
Class | NotImplemented |
501 Not Implemented |
Class | PreconditionFailed |
412 Precondition Failed |
Class | PreconditionRequired |
428 Precondition Required |
Class | RequestedRangeNotSatisfiable |
416 Requested Range Not Satisfiable |
Class | RequestEntityTooLarge |
413 Request Entity Too Large |
Class | RequestHeaderFieldsTooLarge |
431 Request Header Fields Too Large |
Class | RequestTimeout |
408 Request Timeout |
Class | RequestURITooLarge |
414 Request URI Too Large |
Class | SecurityError |
Raised if something triggers a security error. This is otherwise exactly like a bad request error. |
Class | ServiceUnavailable |
503 Service Unavailable |
Class | TooManyRequests |
429 Too Many Requests |
Class | Unauthorized |
401 Unauthorized |
Class | UnavailableForLegalReasons |
451 Unavailable For Legal Reasons |
Class | UnprocessableEntity |
422 Unprocessable Entity |
Class | UnsupportedMediaType |
415 Unsupported Media Type |
Function | abort |
Raises an HTTPException for the given status code or WSGI application. |
Variable | default_exceptions |
Undocumented |
Class | _RetryAfter |
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 |
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[ | Undocumented |
*args:t.Any | Undocumented |
**kwargs:t.Any | Undocumented |
Returns | |
te.NoReturn | Undocumented |