| 1 |
.. _errors: |
| 2 |
|
| 3 |
Custom Error Documents |
| 4 |
====================== |
| 5 |
In this article we will configure a Pecan application to display a custom |
| 6 |
error page whenever the server returns a ``404 Page Not Found`` status. |
| 7 |
|
| 8 |
This article assumes that you have already created a test application as |
| 9 |
described in :ref:`quick_start`. |
| 10 |
|
| 11 |
.. note:: |
| 12 |
While this example focuses on the ``HTTP 404`` message, the same |
| 13 |
technique may be applied to define custom actions for any of the ``HTTP`` |
| 14 |
status response codes in the 400 and 500 range. You are well advised to use |
| 15 |
this power judiciously. |
| 16 |
|
| 17 |
.. _overview: |
| 18 |
|
| 19 |
Overview |
| 20 |
-------- |
| 21 |
|
| 22 |
Pecan makes it simple to customize error documents in two simple steps: |
| 23 |
|
| 24 |
* :ref:`configure` of the HTTP status messages you want to handle |
| 25 |
in your application's ``config.py`` |
| 26 |
* :ref:`controllers` to handle the status messages you have configured |
| 27 |
|
| 28 |
.. _configure: |
| 29 |
|
| 30 |
Configure Routing |
| 31 |
----------------- |
| 32 |
Let's configure our application ``test_project`` to route ``HTTP 404 Page |
| 33 |
Not Found`` messages to a custom controller. |
| 34 |
|
| 35 |
First, let's update ``test_project/config.py`` to specify a new |
| 36 |
error-handler. |
| 37 |
|
| 38 |
:: |
| 39 |
|
| 40 |
# Pecan Application Configurations |
| 41 |
app = { |
| 42 |
'root' : 'test_project.controllers.root.RootController', |
| 43 |
'modules' : ['test_project'], |
| 44 |
'static_root' : '%(confdir)s/public', |
| 45 |
'template_path' : '%(confdir)s/test_project/templates', |
| 46 |
'reload' : True, |
| 47 |
'debug' : True, |
| 48 |
|
| 49 |
# modify the 'errors' key to direct HTTP status codes to a custom |
| 50 |
# controller |
| 51 |
'errors' : { |
| 52 |
#404 : '/error/404', |
| 53 |
404 : '/notfound', |
| 54 |
'__force_dict__' : True |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
Instead of the default error page, Pecan will now route 404 messages |
| 59 |
to the controller method ``notfound``. |
| 60 |
|
| 61 |
.. _controllers: |
| 62 |
|
| 63 |
Write Custom Controllers |
| 64 |
------------------------ |
| 65 |
|
| 66 |
The easiest way to implement the error handler is to |
| 67 |
add it to :class:`test_project.root.RootController` class |
| 68 |
(typically in ``test_project/controllers/root.py``). |
| 69 |
|
| 70 |
:: |
| 71 |
|
| 72 |
from pecan import expose |
| 73 |
from webob.exc import status_map |
| 74 |
|
| 75 |
|
| 76 |
class RootController(object): |
| 77 |
|
| 78 |
@expose(generic=True, template='index.html') |
| 79 |
def index(self): |
| 80 |
return dict() |
| 81 |
|
| 82 |
@index.when(method='POST') |
| 83 |
def index_post(self, q): |
| 84 |
redirect('https://pecan.readthedocs.io/en/latest/search.html?q=%s' % q) |
| 85 |
|
| 86 |
|
| 87 |
## custom handling of '404 Page Not Found' messages |
| 88 |
@expose('error.html') |
| 89 |
def notfound(self): |
| 90 |
return dict(status=404, message="test_project does not have this page") |
| 91 |
|
| 92 |
|
| 93 |
@expose('error.html') |
| 94 |
def error(self, status): |
| 95 |
try: |
| 96 |
status = int(status) |
| 97 |
except ValueError: |
| 98 |
status = 0 |
| 99 |
message = getattr(status_map.get(status), 'explanation', '') |
| 100 |
return dict(status=status, message=message) |
| 101 |
|
| 102 |
|
| 103 |
And that's it! |
| 104 |
|
| 105 |
Notice that the only bit of code we added to our :class:`RootController` was:: |
| 106 |
|
| 107 |
## custom handling of '404 Page Not Found' messages |
| 108 |
@expose('error.html') |
| 109 |
def notfound(self): |
| 110 |
return dict(status=404, message="test_project does not have this page") |
| 111 |
|
| 112 |
We simply :func:`~pecan.decorators.expose` the ``notfound`` controller with the |
| 113 |
``error.html`` template (which was conveniently generated for us and placed |
| 114 |
under ``test_project/templates/`` when we created ``test_project``). As with |
| 115 |
any Pecan controller, we return a dictionary of variables for interpolation by |
| 116 |
the template renderer. |
| 117 |
|
| 118 |
Now we can modify the error template, or write a brand new one to make the 404 |
| 119 |
error status page of ``test_project`` as pretty or fancy as we want. |