This file is indexed.

/usr/share/doc/python-twisted-web/howto/web-in-60/error-handling.rst is in python-twisted-web 17.9.0-2.

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
:LastChangedDate: $LastChangedDate$
:LastChangedRevision: $LastChangedRevision$
:LastChangedBy: $LastChangedBy$

Error Handling
==============





In this example we'll extend dynamic dispatch to return a 404 (not found)
response when a client requests a non-existent URL.




As in the previous examples, we'll start with :api:`twisted.web.server.Site <Site>` , :api:`twisted.web.resource.Resource <Resource>` , :api:`twisted.internet.reactor <reactor>`, and :api:`twisted.internet.endpoints <endpoints>` imports:





.. code-block:: python


    from twisted.web.server import Site
    from twisted.web.resource import Resource
    from twisted.internet import reactor, endpoints




Next, we'll add one more import. :api:`twisted.web.resource.NoResource <NoResource>` is one of the pre-defined error
resources provided by Twisted Web. It generates the necessary 404 response code
and renders a simple html page telling the client there is no such resource.





.. code-block:: python


    from twisted.web.resource import NoResource




Next, we'll define a custom resource which does some dynamic URL
dispatch. This example is going to be just like
the :doc:`previous one <dynamic-dispatch>` , where the path segment is
interpreted as a year; the difference is that this time we'll handle requests
which don't conform to that pattern by returning the not found response:





.. code-block:: python


    class Calendar(Resource):
        def getChild(self, name, request):
            try:
                year = int(name)
            except ValueError:
                return NoResource()
            else:
                return YearPage(year)




Aside from including the definition of ``YearPage`` from
the previous example, the only other thing left to do is the
normal ``Site`` and ``reactor`` setup. Here's the
complete code for this example:





.. code-block:: python


    from twisted.web.server import Site
    from twisted.web.resource import Resource
    from twisted.internet import reactor, endpoints
    from twisted.web.resource import NoResource

    from calendar import calendar

    class YearPage(Resource):
        def __init__(self, year):
            Resource.__init__(self)
            self.year = year

        def render_GET(self, request):
            return "<html><body><pre>%s</pre></body></html>" % (calendar(self.year),)

    class Calendar(Resource):
        def getChild(self, name, request):
            try:
                year = int(name)
            except ValueError:
                return NoResource()
            else:
                return YearPage(year)

    root = Calendar()
    factory = Site(root)
    endpoint = endpoints.TCP4ServerEndpoint(reactor, 8880)
    endpoint.listen(factory)
    reactor.run()




This server hands out the same calendar views as the one from the previous
installment, but it will also hand out a nice error page with a 404 response
when a request is made for a URL which cannot be interpreted as a year.