Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 154 lines (95 sloc) 4.247 kb

HTTP Exceptions

System Message: ERROR/3 (<string>, line 5)

Unknown directive type "automodule".

.. automodule:: werkzeug.exceptions


Error Classes

The following error classes exist in Werkzeug:

System Message: ERROR/3 (<string>, line 13)

Unknown directive type "autoexception".

.. autoexception:: BadRequest

System Message: ERROR/3 (<string>, line 15)

Unknown directive type "autoexception".

.. autoexception:: Unauthorized

System Message: ERROR/3 (<string>, line 17)

Unknown directive type "autoexception".

.. autoexception:: Forbidden

System Message: ERROR/3 (<string>, line 19)

Unknown directive type "autoexception".

.. autoexception:: NotFound

System Message: ERROR/3 (<string>, line 21)

Unknown directive type "autoexception".

.. autoexception:: MethodNotAllowed

System Message: ERROR/3 (<string>, line 23)

Unknown directive type "autoexception".

.. autoexception:: NotAcceptable

System Message: ERROR/3 (<string>, line 25)

Unknown directive type "autoexception".

.. autoexception:: RequestTimeout

System Message: ERROR/3 (<string>, line 27)

Unknown directive type "autoexception".

.. autoexception:: Conflict

System Message: ERROR/3 (<string>, line 29)

Unknown directive type "autoexception".

.. autoexception:: Gone

System Message: ERROR/3 (<string>, line 31)

Unknown directive type "autoexception".

.. autoexception:: LengthRequired

System Message: ERROR/3 (<string>, line 33)

Unknown directive type "autoexception".

.. autoexception:: PreconditionFailed

System Message: ERROR/3 (<string>, line 35)

Unknown directive type "autoexception".

.. autoexception:: RequestEntityTooLarge

System Message: ERROR/3 (<string>, line 37)

Unknown directive type "autoexception".

.. autoexception:: RequestURITooLarge

System Message: ERROR/3 (<string>, line 39)

Unknown directive type "autoexception".

.. autoexception:: UnsupportedMediaType

System Message: ERROR/3 (<string>, line 41)

Unknown directive type "autoexception".

.. autoexception:: RequestedRangeNotSatisfiable

System Message: ERROR/3 (<string>, line 43)

Unknown directive type "autoexception".

.. autoexception:: ExpectationFailed

System Message: ERROR/3 (<string>, line 45)

Unknown directive type "autoexception".

.. autoexception:: ImATeapot

System Message: ERROR/3 (<string>, line 47)

Unknown directive type "autoexception".

.. autoexception:: PreconditionRequired

System Message: ERROR/3 (<string>, line 49)

Unknown directive type "autoexception".

.. autoexception:: TooManyRequests

System Message: ERROR/3 (<string>, line 51)

Unknown directive type "autoexception".

.. autoexception:: RequestHeaderFieldsTooLarge

System Message: ERROR/3 (<string>, line 53)

Unknown directive type "autoexception".

.. autoexception:: InternalServerError

System Message: ERROR/3 (<string>, line 55)

Unknown directive type "autoexception".

.. autoexception:: NotImplemented

System Message: ERROR/3 (<string>, line 57)

Unknown directive type "autoexception".

.. autoexception:: BadGateway

System Message: ERROR/3 (<string>, line 59)

Unknown directive type "autoexception".

.. autoexception:: ServiceUnavailable

System Message: ERROR/3 (<string>, line 61)

Unknown directive type "exception".

.. exception:: HTTPUnicodeError

   This exception is used to signal unicode decode errors of request
   data.  For more information see the :ref:`unicode` chapter.

System Message: ERROR/3 (<string>, line 66)

Unknown directive type "autoexception".

.. autoexception:: ClientDisconnected


Baseclass

All the exceptions implement this common interface:

System Message: ERROR/3 (<string>, line 74)

Unknown directive type "autoexception".

.. autoexception:: HTTPException
   :members: get_response, __call__


Special HTTP Exceptions

Starting with Werkzeug 0.3 some of the builtin classes raise exceptions that look like regular python exceptions (eg :exc:`KeyError`) but are :exc:`BadRequest` HTTP exceptions at the same time. This decision was made to simplify a common pattern where you want to abort if the client tampered with the submitted form data in a way that the application can't recover properly and should abort with 400 BAD REQUEST.

System Message: ERROR/3 (<string>, line 81); backlink

Unknown interpreted text role "exc".

System Message: ERROR/3 (<string>, line 81); backlink

Unknown interpreted text role "exc".

Assuming the application catches all HTTP exceptions and reacts to them properly a view function could do the following savely and doesn't have to check if the keys exist:

def new_post(request):
    post = Post(title=request.form['title'], body=request.form['body'])
    post.save()
    return redirect(post.url)

If title or body are missing in the form a special key error will be raised which behaves like a :exc:`KeyError` but also a :exc:`BadRequest` exception.

System Message: ERROR/3 (<string>, line 97); backlink

Unknown interpreted text role "exc".

System Message: ERROR/3 (<string>, line 97); backlink

Unknown interpreted text role "exc".

Simple Aborting

Sometimes it's convenient to just raise an exception by the error code, without importing the exception and looking up the name etc. For this purpose there is the :func:`abort` function.

System Message: ERROR/3 (<string>, line 105); backlink

Unknown interpreted text role "func".

System Message: ERROR/3 (<string>, line 109)

Unknown directive type "function".

.. function:: abort(status)

   It can be passed a WSGI application or a status code.  If a status code
   is given it's looked up in the list of exceptions from above and will
   raise that exception, if passed a WSGI application it will wrap it in
   a proxy WSGI exception and raise that::

       abort(404)
       abort(Response('Hello World'))

If you want to use this functionality with custom exceptions you can create an instance of the aborter class:

System Message: ERROR/3 (<string>, line 122)

Unknown directive type "autoclass".

.. autoclass:: Aborter


Custom Errors

As you can see from the list above not all status codes are available as errors. Especially redirects and ather non 200 status codes that represent do not represent errors are missing. For redirects you can use the :func:`redirect` function from the utilities.

System Message: ERROR/3 (<string>, line 128); backlink

Unknown interpreted text role "func".

If you want to add an error yourself you can subclass :exc:`HTTPException`:

System Message: ERROR/3 (<string>, line 133); backlink

Unknown interpreted text role "exc".
from werkzeug.exceptions import HTTPException

class PaymentRequired(HTTPException):
    code = 402
    description = '<p>Payment required.</p>'

This is the minimal code you need for your own exception. If you want to add more logic to the errors you can override the :meth:`~HTTPException.get_description`, :meth:`~HTTPException.get_body`, :meth:`~HTTPException.get_headers` and :meth:`~HTTPException.get_response` methods. In any case you should have a look at the sourcecode of the exceptions module.

System Message: ERROR/3 (<string>, line 141); backlink

Unknown interpreted text role "meth".

System Message: ERROR/3 (<string>, line 141); backlink

Unknown interpreted text role "meth".

System Message: ERROR/3 (<string>, line 141); backlink

Unknown interpreted text role "meth".

System Message: ERROR/3 (<string>, line 141); backlink

Unknown interpreted text role "meth".

You can override the default description in the constructor with the description parameter (it's the first argument for all exceptions except of the :exc:`MethodNotAllowed` which accepts a list of allowed methods as first argument):

System Message: ERROR/3 (<string>, line 148); backlink

Unknown interpreted text role "exc".
raise BadRequest('Request failed because X was not present')
Something went wrong with that request. Please try again.