This file is indexed.

/usr/share/doc/python-werkzeug-doc/html/_sources/exceptions.txt is in python-werkzeug-doc 0.10.4+dfsg1-1ubuntu1.1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
===============
HTTP Exceptions
===============

.. automodule:: werkzeug.exceptions


Error Classes
=============

The following error classes exist in Werkzeug:

.. autoexception:: BadRequest

.. autoexception:: Unauthorized

.. autoexception:: Forbidden

.. autoexception:: NotFound

.. autoexception:: MethodNotAllowed

.. autoexception:: NotAcceptable

.. autoexception:: RequestTimeout

.. autoexception:: Conflict

.. autoexception:: Gone

.. autoexception:: LengthRequired

.. autoexception:: PreconditionFailed

.. autoexception:: RequestEntityTooLarge

.. autoexception:: RequestURITooLarge

.. autoexception:: UnsupportedMediaType

.. autoexception:: RequestedRangeNotSatisfiable

.. autoexception:: ExpectationFailed

.. autoexception:: ImATeapot

.. autoexception:: PreconditionRequired

.. autoexception:: TooManyRequests

.. autoexception:: RequestHeaderFieldsTooLarge

.. autoexception:: InternalServerError

.. autoexception:: NotImplemented

.. autoexception:: BadGateway

.. autoexception:: ServiceUnavailable

.. exception:: HTTPUnicodeError

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

.. autoexception:: ClientDisconnected

.. autoexception:: SecurityError


Baseclass
=========

All the exceptions implement this common interface:

.. 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``.

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.


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.

.. 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:

.. 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.

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

    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.

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)::

    raise BadRequest('Request failed because X was not present')