Former-commit-id:133dc97f67
[formerlya02aeb236c
] [formerly9f19e3f712
] [formerly06a8b51d6d
[formerly9f19e3f712
[formerly 64fa9254b946eae7e61bbc3f513b7c3696c4f54f]]] Former-commit-id:06a8b51d6d
Former-commit-id:377dcd10b9
[formerly3360eb6c5f
] Former-commit-id:8e80217e59
383 lines
No EOL
26 KiB
HTML
Executable file
383 lines
No EOL
26 KiB
HTML
Executable file
|
|
<!DOCTYPE HTML>
|
|
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
|
<title>Werkzeug Documentation</title>
|
|
<link rel="stylesheet" href="_static/style.css" type="text/css">
|
|
<link rel="stylesheet" href="_static/print.css" type="text/css" media="print">
|
|
<link rel="stylesheet" href="_static/pygments.css" type="text/css">
|
|
<script type="text/javascript">
|
|
var DOCUMENTATION_OPTIONS = {
|
|
URL_ROOT: '#',
|
|
VERSION: '0.6.1'
|
|
};
|
|
</script>
|
|
<script type="text/javascript" src="_static/jquery.js"></script>
|
|
<script type="text/javascript" src="_static/interface.js"></script>
|
|
<script type="text/javascript" src="_static/doctools.js"></script>
|
|
<script type="text/javascript" src="_static/werkzeug.js"></script>
|
|
<link rel="contents" title="Global table of contents" href="contents.html">
|
|
<link rel="index" title="Global index" href="genindex.html">
|
|
<link rel="search" title="Search" href="search.html">
|
|
<link rel="top" title="Werkzeug v0.6.1 documentation" href="index.html">
|
|
<link rel="next" title="Application Deployment" href="deployment/index.html">
|
|
<link rel="prev" title="Middlewares" href="middlewares.html">
|
|
|
|
</head>
|
|
<body>
|
|
<div class="page">
|
|
<div class="header">
|
|
<h1 class="heading"><a href="index.html"
|
|
title="back to the documentation overview"><span>Werkzeug</span></a></h1>
|
|
</div>
|
|
<ul class="navigation">
|
|
<li class="indexlink"><a href="index.html">Overview</a></li>
|
|
<li><a href="middlewares.html">« Middlewares</a></li>
|
|
<li class="active"><a href="#">HTTP Exceptions</a></li>
|
|
<li><a href="deployment/index.html">Application Deployment »</a></li>
|
|
</ul>
|
|
<div class="body">
|
|
<div id="toc">
|
|
<h3>Table Of Contents</h3>
|
|
<div class="inner"><ul>
|
|
<li><a class="reference external" href="#">HTTP Exceptions</a><ul>
|
|
<li><a class="reference external" href="#usage-example">Usage Example</a></li>
|
|
<li><a class="reference external" href="#error-classes">Error Classes</a></li>
|
|
<li><a class="reference external" href="#baseclass">Baseclass</a></li>
|
|
<li><a class="reference external" href="#special-http-exceptions">Special HTTP Exceptions</a></li>
|
|
<li><a class="reference external" href="#simple-aborting">Simple Aborting</a></li>
|
|
<li><a class="reference external" href="#custom-errors">Custom Errors</a></li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="section" id="module-werkzeug.exceptions">
|
|
<span id="http-exceptions"></span><h1>HTTP Exceptions<a class="headerlink" href="#module-werkzeug.exceptions" title="Permalink to this headline">¶</a></h1>
|
|
<p>This module implements a number of Python exceptions you can raise from
|
|
within your views to trigger a standard non-200 response.</p>
|
|
<div class="section" id="usage-example">
|
|
<h2>Usage Example<a class="headerlink" href="#usage-example" title="Permalink to this headline">¶</a></h2>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">werkzeug</span> <span class="kn">import</span> <span class="n">BaseRequest</span><span class="p">,</span> <span class="n">responder</span>
|
|
<span class="kn">from</span> <span class="nn">werkzeug.exceptions</span> <span class="kn">import</span> <span class="n">HTTPException</span><span class="p">,</span> <span class="n">NotFound</span>
|
|
|
|
<span class="k">def</span> <span class="nf">view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
|
|
<span class="k">raise</span> <span class="n">NotFound</span><span class="p">()</span>
|
|
|
|
<span class="nd">@responder</span>
|
|
<span class="k">def</span> <span class="nf">application</span><span class="p">(</span><span class="n">environ</span><span class="p">,</span> <span class="n">start_response</span><span class="p">):</span>
|
|
<span class="n">request</span> <span class="o">=</span> <span class="n">BaseRequest</span><span class="p">(</span><span class="n">environ</span><span class="p">)</span>
|
|
<span class="k">try</span><span class="p">:</span>
|
|
<span class="k">return</span> <span class="n">view</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
|
|
<span class="k">except</span> <span class="n">HTTPException</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span>
|
|
<span class="k">return</span> <span class="n">e</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>As you can see from this example those exceptions are callable WSGI
|
|
applications. Because of Python 2.4 compatibility those do not extend
|
|
from the response objects but only from the python exception class.</p>
|
|
<p>As a matter of fact they are not Werkzeug response objects. However you
|
|
can get a response object by calling <tt class="docutils literal"><span class="pre">get_response()</span></tt> on a HTTP
|
|
exception.</p>
|
|
<p>Keep in mind that you have to pass an environment to <tt class="docutils literal"><span class="pre">get_response()</span></tt>
|
|
because some errors fetch additional information from the WSGI
|
|
environment.</p>
|
|
<p>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:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="nd">@responder</span>
|
|
<span class="k">def</span> <span class="nf">application</span><span class="p">(</span><span class="n">environ</span><span class="p">,</span> <span class="n">start_response</span><span class="p">):</span>
|
|
<span class="n">request</span> <span class="o">=</span> <span class="n">BaseRequest</span><span class="p">(</span><span class="n">environ</span><span class="p">)</span>
|
|
<span class="k">try</span><span class="p">:</span>
|
|
<span class="k">return</span> <span class="n">view</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
|
|
<span class="k">except</span> <span class="n">NotFound</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span>
|
|
<span class="k">return</span> <span class="n">not_found</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
|
|
<span class="k">except</span> <span class="n">HTTPException</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span>
|
|
<span class="k">return</span> <span class="n">e</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="error-classes">
|
|
<h2>Error Classes<a class="headerlink" href="#error-classes" title="Permalink to this headline">¶</a></h2>
|
|
<p>The following error classes exist in Werkzeug:</p>
|
|
<dl class="exception">
|
|
<dt id="werkzeug.exceptions.BadRequest">
|
|
<em class="property">exception </em><tt class="descclassname">werkzeug.exceptions.</tt><tt class="descname">BadRequest</tt><big>(</big><em>description=None</em><big>)</big><a class="headerlink" href="#werkzeug.exceptions.BadRequest" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p><em>400</em> <cite>Bad Request</cite></p>
|
|
<p>Raise if the browser sends something to the application the application
|
|
or server cannot handle.</p>
|
|
</dd></dl>
|
|
|
|
<dl class="exception">
|
|
<dt id="werkzeug.exceptions.Unauthorized">
|
|
<em class="property">exception </em><tt class="descclassname">werkzeug.exceptions.</tt><tt class="descname">Unauthorized</tt><big>(</big><em>description=None</em><big>)</big><a class="headerlink" href="#werkzeug.exceptions.Unauthorized" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p><em>401</em> <cite>Unauthorized</cite></p>
|
|
<p>Raise if the user is not authorized. Also used if you want to use HTTP
|
|
basic auth.</p>
|
|
</dd></dl>
|
|
|
|
<dl class="exception">
|
|
<dt id="werkzeug.exceptions.Forbidden">
|
|
<em class="property">exception </em><tt class="descclassname">werkzeug.exceptions.</tt><tt class="descname">Forbidden</tt><big>(</big><em>description=None</em><big>)</big><a class="headerlink" href="#werkzeug.exceptions.Forbidden" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p><em>403</em> <cite>Forbidden</cite></p>
|
|
<p>Raise if the user doesn’t have the permission for the requested resource
|
|
but was authenticated.</p>
|
|
</dd></dl>
|
|
|
|
<dl class="exception">
|
|
<dt id="werkzeug.exceptions.NotFound">
|
|
<em class="property">exception </em><tt class="descclassname">werkzeug.exceptions.</tt><tt class="descname">NotFound</tt><big>(</big><em>description=None</em><big>)</big><a class="headerlink" href="#werkzeug.exceptions.NotFound" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p><em>404</em> <cite>Not Found</cite></p>
|
|
<p>Raise if a resource does not exist and never existed.</p>
|
|
</dd></dl>
|
|
|
|
<dl class="exception">
|
|
<dt id="werkzeug.exceptions.MethodNotAllowed">
|
|
<em class="property">exception </em><tt class="descclassname">werkzeug.exceptions.</tt><tt class="descname">MethodNotAllowed</tt><big>(</big><em>valid_methods=None</em>, <em>description=None</em><big>)</big><a class="headerlink" href="#werkzeug.exceptions.MethodNotAllowed" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p><em>405</em> <cite>Method Not Allowed</cite></p>
|
|
<p>Raise if the server used a method the resource does not handle. For
|
|
example <cite>POST</cite> if the resource is view only. Especially useful for REST.</p>
|
|
<p>The first argument for this exception should be a list of allowed methods.
|
|
Strictly speaking the response would be invalid if you don’t provide valid
|
|
methods in the header which you can do with that list.</p>
|
|
</dd></dl>
|
|
|
|
<dl class="exception">
|
|
<dt id="werkzeug.exceptions.NotAcceptable">
|
|
<em class="property">exception </em><tt class="descclassname">werkzeug.exceptions.</tt><tt class="descname">NotAcceptable</tt><big>(</big><em>description=None</em><big>)</big><a class="headerlink" href="#werkzeug.exceptions.NotAcceptable" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p><em>406</em> <cite>Not Acceptable</cite></p>
|
|
<p>Raise if the server can’t return any content conforming to the
|
|
<cite>Accept</cite> headers of the client.</p>
|
|
</dd></dl>
|
|
|
|
<dl class="exception">
|
|
<dt id="werkzeug.exceptions.RequestTimeout">
|
|
<em class="property">exception </em><tt class="descclassname">werkzeug.exceptions.</tt><tt class="descname">RequestTimeout</tt><big>(</big><em>description=None</em><big>)</big><a class="headerlink" href="#werkzeug.exceptions.RequestTimeout" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p><em>408</em> <cite>Request Timeout</cite></p>
|
|
<p>Raise to signalize a timeout.</p>
|
|
</dd></dl>
|
|
|
|
<dl class="exception">
|
|
<dt id="werkzeug.exceptions.Gone">
|
|
<em class="property">exception </em><tt class="descclassname">werkzeug.exceptions.</tt><tt class="descname">Gone</tt><big>(</big><em>description=None</em><big>)</big><a class="headerlink" href="#werkzeug.exceptions.Gone" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p><em>410</em> <cite>Gone</cite></p>
|
|
<p>Raise if a resource existed previously and went away without new location.</p>
|
|
</dd></dl>
|
|
|
|
<dl class="exception">
|
|
<dt id="werkzeug.exceptions.LengthRequired">
|
|
<em class="property">exception </em><tt class="descclassname">werkzeug.exceptions.</tt><tt class="descname">LengthRequired</tt><big>(</big><em>description=None</em><big>)</big><a class="headerlink" href="#werkzeug.exceptions.LengthRequired" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p><em>411</em> <cite>Length Required</cite></p>
|
|
<p>Raise if the browser submitted data but no <tt class="docutils literal"><span class="pre">Content-Length</span></tt> header which
|
|
is required for the kind of processing the server does.</p>
|
|
</dd></dl>
|
|
|
|
<dl class="exception">
|
|
<dt id="werkzeug.exceptions.PreconditionFailed">
|
|
<em class="property">exception </em><tt class="descclassname">werkzeug.exceptions.</tt><tt class="descname">PreconditionFailed</tt><big>(</big><em>description=None</em><big>)</big><a class="headerlink" href="#werkzeug.exceptions.PreconditionFailed" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p><em>412</em> <cite>Precondition Failed</cite></p>
|
|
<p>Status code used in combination with <tt class="docutils literal"><span class="pre">If-Match</span></tt>, <tt class="docutils literal"><span class="pre">If-None-Match</span></tt>, or
|
|
<tt class="docutils literal"><span class="pre">If-Unmodified-Since</span></tt>.</p>
|
|
</dd></dl>
|
|
|
|
<dl class="exception">
|
|
<dt id="werkzeug.exceptions.RequestEntityTooLarge">
|
|
<em class="property">exception </em><tt class="descclassname">werkzeug.exceptions.</tt><tt class="descname">RequestEntityTooLarge</tt><big>(</big><em>description=None</em><big>)</big><a class="headerlink" href="#werkzeug.exceptions.RequestEntityTooLarge" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p><em>413</em> <cite>Request Entity Too Large</cite></p>
|
|
<p>The status code one should return if the data submitted exceeded a given
|
|
limit.</p>
|
|
</dd></dl>
|
|
|
|
<dl class="exception">
|
|
<dt id="werkzeug.exceptions.RequestURITooLarge">
|
|
<em class="property">exception </em><tt class="descclassname">werkzeug.exceptions.</tt><tt class="descname">RequestURITooLarge</tt><big>(</big><em>description=None</em><big>)</big><a class="headerlink" href="#werkzeug.exceptions.RequestURITooLarge" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p><em>414</em> <cite>Request URI Too Large</cite></p>
|
|
<p>Like <em>413</em> but for too long URLs.</p>
|
|
</dd></dl>
|
|
|
|
<dl class="exception">
|
|
<dt id="werkzeug.exceptions.UnsupportedMediaType">
|
|
<em class="property">exception </em><tt class="descclassname">werkzeug.exceptions.</tt><tt class="descname">UnsupportedMediaType</tt><big>(</big><em>description=None</em><big>)</big><a class="headerlink" href="#werkzeug.exceptions.UnsupportedMediaType" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p><em>415</em> <cite>Unsupported Media Type</cite></p>
|
|
<p>The status code returned if the server is unable to handle the media type
|
|
the client transmitted.</p>
|
|
</dd></dl>
|
|
|
|
<dl class="exception">
|
|
<dt id="werkzeug.exceptions.InternalServerError">
|
|
<em class="property">exception </em><tt class="descclassname">werkzeug.exceptions.</tt><tt class="descname">InternalServerError</tt><big>(</big><em>description=None</em><big>)</big><a class="headerlink" href="#werkzeug.exceptions.InternalServerError" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p><em>500</em> <cite>Internal Server Error</cite></p>
|
|
<p>Raise if an internal server error occurred. This is a good fallback if an
|
|
unknown error occurred in the dispatcher.</p>
|
|
</dd></dl>
|
|
|
|
<dl class="exception">
|
|
<dt id="werkzeug.exceptions.NotImplemented">
|
|
<em class="property">exception </em><tt class="descclassname">werkzeug.exceptions.</tt><tt class="descname">NotImplemented</tt><big>(</big><em>description=None</em><big>)</big><a class="headerlink" href="#werkzeug.exceptions.NotImplemented" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p><em>501</em> <cite>Not Implemented</cite></p>
|
|
<p>Raise if the application does not support the action requested by the
|
|
browser.</p>
|
|
</dd></dl>
|
|
|
|
<dl class="exception">
|
|
<dt id="werkzeug.exceptions.BadGateway">
|
|
<em class="property">exception </em><tt class="descclassname">werkzeug.exceptions.</tt><tt class="descname">BadGateway</tt><big>(</big><em>description=None</em><big>)</big><a class="headerlink" href="#werkzeug.exceptions.BadGateway" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p><em>502</em> <cite>Bad Gateway</cite></p>
|
|
<p>If you do proxying in your application you should return this status code
|
|
if you received an invalid response from the upstream server it accessed
|
|
in attempting to fulfill the request.</p>
|
|
</dd></dl>
|
|
|
|
<dl class="exception">
|
|
<dt id="werkzeug.exceptions.ServiceUnavailable">
|
|
<em class="property">exception </em><tt class="descclassname">werkzeug.exceptions.</tt><tt class="descname">ServiceUnavailable</tt><big>(</big><em>description=None</em><big>)</big><a class="headerlink" href="#werkzeug.exceptions.ServiceUnavailable" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p><em>503</em> <cite>Service Unavailable</cite></p>
|
|
<p>Status code you should return if a service is temporarily unavailable.</p>
|
|
</dd></dl>
|
|
|
|
<dl class="exception">
|
|
<dt id="werkzeug.exceptions.HTTPUnicodeError">
|
|
<em class="property">exception </em><tt class="descclassname">werkzeug.exceptions.</tt><tt class="descname">HTTPUnicodeError</tt><a class="headerlink" href="#werkzeug.exceptions.HTTPUnicodeError" title="Permalink to this definition">¶</a></dt>
|
|
<dd>This exception is used to signal unicode decode errors of request
|
|
data. For more information see the <a class="reference external" href="unicode.html#unicode"><em>Unicode</em></a> chapter.</dd></dl>
|
|
|
|
</div>
|
|
<div class="section" id="baseclass">
|
|
<h2>Baseclass<a class="headerlink" href="#baseclass" title="Permalink to this headline">¶</a></h2>
|
|
<p>All the exceptions implement this common interface:</p>
|
|
<dl class="exception">
|
|
<dt id="werkzeug.exceptions.HTTPException">
|
|
<em class="property">exception </em><tt class="descclassname">werkzeug.exceptions.</tt><tt class="descname">HTTPException</tt><big>(</big><em>description=None</em><big>)</big><a class="headerlink" href="#werkzeug.exceptions.HTTPException" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>Baseclass for all HTTP exceptions. This exception can be called as WSGI
|
|
application to render a default error page or you can catch the subclasses
|
|
of it independently and render nicer error messages.</p>
|
|
<dl class="method">
|
|
<dt id="werkzeug.exceptions.HTTPException.get_response">
|
|
<tt class="descname">get_response</tt><big>(</big><em>environ</em><big>)</big><a class="headerlink" href="#werkzeug.exceptions.HTTPException.get_response" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>Get a response object.</p>
|
|
<table class="docutils field-list" frame="void" rules="none">
|
|
<col class="field-name" />
|
|
<col class="field-body" />
|
|
<tbody valign="top">
|
|
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
|
|
<li><strong>environ</strong> – the environ for the request.</li>
|
|
</ul>
|
|
</td>
|
|
</tr>
|
|
<tr class="field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last">a <tt class="xref py py-class docutils literal"><span class="pre">BaseResponse</span></tt> object or a subclass thereof.</p>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</dd></dl>
|
|
|
|
<dl class="method">
|
|
<dt id="werkzeug.exceptions.HTTPException.__call__">
|
|
<tt class="descname">__call__</tt><big>(</big><em>environ</em>, <em>start_response</em><big>)</big><a class="headerlink" href="#werkzeug.exceptions.HTTPException.__call__" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>Call the exception as WSGI application.</p>
|
|
<table class="docutils field-list" frame="void" rules="none">
|
|
<col class="field-name" />
|
|
<col class="field-body" />
|
|
<tbody valign="top">
|
|
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
|
|
<li><strong>environ</strong> – the WSGI environment.</li>
|
|
<li><strong>start_response</strong> – the response callable provided by the WSGI
|
|
server.</li>
|
|
</ul>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</dd></dl>
|
|
|
|
</dd></dl>
|
|
|
|
</div>
|
|
<div class="section" id="special-http-exceptions">
|
|
<h2>Special HTTP Exceptions<a class="headerlink" href="#special-http-exceptions" title="Permalink to this headline">¶</a></h2>
|
|
<p>Starting with Werkzeug 0.3 some of the builtin classes raise exceptions that
|
|
look like regular python exceptions (eg <tt class="xref py py-exc docutils literal"><span class="pre">KeyError</span></tt>) but are
|
|
<a title="werkzeug.exceptions.BadRequest" class="reference internal" href="#werkzeug.exceptions.BadRequest"><tt class="xref py py-exc docutils literal"><span class="pre">BadRequest</span></tt></a> 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 <tt class="docutils literal"><span class="pre">400</span> <span class="pre">BAD</span> <span class="pre">REQUEST</span></tt>.</p>
|
|
<p>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:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">new_post</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
|
|
<span class="n">post</span> <span class="o">=</span> <span class="n">Post</span><span class="p">(</span><span class="n">title</span><span class="o">=</span><span class="n">request</span><span class="o">.</span><span class="n">form</span><span class="p">[</span><span class="s">'title'</span><span class="p">],</span> <span class="n">body</span><span class="o">=</span><span class="n">request</span><span class="o">.</span><span class="n">form</span><span class="p">[</span><span class="s">'body'</span><span class="p">])</span>
|
|
<span class="n">post</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>
|
|
<span class="k">return</span> <span class="n">redirect</span><span class="p">(</span><span class="n">post</span><span class="o">.</span><span class="n">url</span><span class="p">)</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>If <cite>title</cite> or <cite>body</cite> are missing in the form a special key error will be
|
|
raised which behaves like a <tt class="xref py py-exc docutils literal"><span class="pre">KeyError</span></tt> but also a <a title="werkzeug.exceptions.BadRequest" class="reference internal" href="#werkzeug.exceptions.BadRequest"><tt class="xref py py-exc docutils literal"><span class="pre">BadRequest</span></tt></a>
|
|
exception.</p>
|
|
</div>
|
|
<div class="section" id="simple-aborting">
|
|
<h2>Simple Aborting<a class="headerlink" href="#simple-aborting" title="Permalink to this headline">¶</a></h2>
|
|
<p>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 <tt class="xref py py-func docutils literal"><span class="pre">abort()</span></tt> function.</p>
|
|
<p>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:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="n">abort</span><span class="p">(</span><span class="mi">404</span><span class="p">)</span>
|
|
<span class="n">abort</span><span class="p">(</span><span class="n">Response</span><span class="p">(</span><span class="s">'Hello World'</span><span class="p">))</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>If you want to use this functionality with custom excetions you can
|
|
create an instance of the aborter class:</p>
|
|
<dl class="class">
|
|
<dt id="werkzeug.exceptions.Aborter">
|
|
<em class="property">class </em><tt class="descclassname">werkzeug.exceptions.</tt><tt class="descname">Aborter</tt><big>(</big><em>mapping=None</em>, <em>extra=None</em><big>)</big><a class="headerlink" href="#werkzeug.exceptions.Aborter" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>When passed a dict of code -> exception items it can be used as
|
|
callable that raises exceptions. If the first argument to the
|
|
callable is an integer it will be looked up in the mapping, if it’s
|
|
a WSGI application it will be raised in a proxy exception.</p>
|
|
<p>The rest of the arguments are forwarded to the exception constructor.</p>
|
|
</dd></dl>
|
|
|
|
</div>
|
|
<div class="section" id="custom-errors">
|
|
<h2>Custom Errors<a class="headerlink" href="#custom-errors" title="Permalink to this headline">¶</a></h2>
|
|
<p>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 <tt class="xref py py-func docutils literal"><span class="pre">redirect()</span></tt> function from the utilities.</p>
|
|
<p>If you want to add an error yourself you can subclass <a title="werkzeug.exceptions.HTTPException" class="reference internal" href="#werkzeug.exceptions.HTTPException"><tt class="xref py py-exc docutils literal"><span class="pre">HTTPException</span></tt></a>:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">werkzeug.exceptions</span> <span class="kn">import</span> <span class="n">HTTPException</span>
|
|
|
|
<span class="k">class</span> <span class="nc">PaymentRequred</span><span class="p">(</span><span class="n">HTTPException</span><span class="p">):</span>
|
|
<span class="n">code</span> <span class="o">=</span> <span class="mi">402</span>
|
|
<span class="n">description</span> <span class="o">=</span> <span class="s">'<p>Payment required.</p>'</span>
|
|
</pre></div>
|
|
</div>
|
|
<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
|
|
<tt class="xref py py-meth docutils literal"><span class="pre">get_description()</span></tt>, <tt class="xref py py-meth docutils literal"><span class="pre">get_body()</span></tt>,
|
|
<tt class="xref py py-meth docutils literal"><span class="pre">get_headers()</span></tt> and <a title="werkzeug.exceptions.HTTPException.get_response" class="reference internal" href="#werkzeug.exceptions.HTTPException.get_response"><tt class="xref py py-meth docutils literal"><span class="pre">get_response()</span></tt></a>
|
|
methods. In any case you should have a look at the sourcecode of the
|
|
exceptions module.</p>
|
|
<p>You can override the default description in the constructor with the
|
|
<cite>description</cite> parameter (it’s the first argument for all exceptions
|
|
except of the <a title="werkzeug.exceptions.MethodNotAllowed" class="reference internal" href="#werkzeug.exceptions.MethodNotAllowed"><tt class="xref py py-exc docutils literal"><span class="pre">MethodNotAllowed</span></tt></a> which accepts a list of allowed methods
|
|
as first argument):</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="k">raise</span> <span class="n">BadRequest</span><span class="p">(</span><span class="s">'Request failed because X was not present'</span><span class="p">)</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
<div style="clear: both"></div>
|
|
</div>
|
|
<div class="footer">
|
|
© Copyright 2008 by the <a href="http://pocoo.org/">Pocoo Team</a>,
|
|
documentation generated by <a href="http://sphinx.pocoo.org/">Sphinx</a>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html> |