summaryrefslogtreecommitdiff
path: root/docs/source/errors.rst
blob: 2b78b025e2b173aebe39f92839f92722b7d6fe65 (plain)
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
.. _errors:

Custom Error Documents
======================
In this article we will configure a Pecan application to display a custom
error page whenever the server returns a ``404 Page Not Found`` status.

This article assumes that you have already created a test application as
described in :ref:`quick_start`.

.. note::
    While this example focuses on the ``HTTP 404`` message, the same
    technique may be applied to define custom actions for any of the ``HTTP``
    status response codes in the 400 and 500 range. You are well advised to use
    this power judiciously.

.. _overview:

Overview
--------

Pecan makes it simple to customize error documents in two simple steps:

   * :ref:`configure`  of the HTTP status messages you want to handle
     in your application's ``config.py``
   * :ref:`controllers` to handle the status messages you have configured

.. _configure:

Configure Routing
-----------------
Let's configure our application ``test_project`` to route ``HTTP 404 Page 
Not Found`` messages to a custom controller.

First, let's update ``test_project/config.py`` to specify a new
error-handler.

::

    # Pecan Application Configurations
    app = {
        'root'            : 'test_project.controllers.root.RootController',
        'modules'         : ['test_project'],
        'static_root'     : '%(confdir)s/public', 
        'template_path'   : '%(confdir)s/test_project/templates',
        'reload'          : True,
        'debug'           : True,
        
        # modify the 'errors' key to direct HTTP status codes to a custom 
        # controller
        'errors'          : {
            #404           : '/error/404',
            404           : '/notfound',
            '__force_dict__' : True
        }
    }

Instead of the default error page, Pecan will now route 404 messages
to the controller method ``notfound``.

.. _controllers:

Write Custom Controllers
------------------------

The easiest way to implement the error handler is to 
add it to :class:`test_project.root.RootController` class
(typically in ``test_project/controllers/root.py``).

::
    
    from pecan import expose
    from webob.exc import status_map


    class RootController(object):

        @expose(generic=True, template='index.html')
        def index(self):
            return dict()

        @index.when(method='POST')
        def index_post(self, q):
            redirect('http://pecan.readthedocs.org/en/latest/search.html?q=%s' % q)


        ## custom handling of '404 Page Not Found' messages
        @expose('error.html')
        def notfound(self):
            return dict(status=404, message="test_project does not have this page")


        @expose('error.html')
        def error(self, status):
            try:
                status = int(status)
            except ValueError:
                status = 0
            message = getattr(status_map.get(status), 'explanation', '')
            return dict(status=status, message=message)


And that's it!

Notice that the only bit of code we added to our :class:`RootController` was::

        ## custom handling of '404 Page Not Found' messages
        @expose('error.html')
        def notfound(self):
            return dict(status=404, message="test_project does not have this page")

We simply :func:`@expose` the ``notfound`` controller with the ``error.html`` 
template (which was conveniently generated for us and placed under
``test_project/templates/`` when we created ``test_project``).  As with any
Pecan controller, we return a dictionary of variables for interpolation by the 
template renderer.  

Now we can modify the error template, or write a brand new one to make the 404
error status page of ``test_project`` as pretty or fancy as we want.