~certify-web-dev/twisted/certify-trunk

« back to all changes in this revision

Viewing changes to twisted/web/error.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-01-17 14:52:35 UTC
  • mfrom: (1.1.5 upstream) (2.1.2 etch)
  • Revision ID: james.westby@ubuntu.com-20070117145235-btmig6qfmqfen0om
Tags: 2.5.0-0ubuntu1
New upstream version, compatible with python2.5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
 
3
# See LICENSE for details.
 
4
 
 
5
 
 
6
"""I am the Twisted.Web error resources and exceptions."""
 
7
 
 
8
#t.w imports
 
9
import resource
 
10
 
 
11
from twisted.web import http
 
12
 
 
13
class Error(Exception):
 
14
    def __init__(self, code, message = None, response = None):
 
15
        message = message or http.responses.get(code)
 
16
        Exception.__init__(self, code, message, response)
 
17
        self.status = code
 
18
        self.response = response
 
19
    
 
20
    def __str__(self):
 
21
        return '%s %s' % (self[0], self[1])
 
22
 
 
23
class PageRedirect(Error):
 
24
    """A request that resulted in a http redirect """
 
25
    def __init__(self, code, message = None, response = None, location = None):
 
26
        message = message or ("%s to %s" % (http.responses.get(code), location))
 
27
        Error.__init__(self, code, message, response)
 
28
        self.location = location
 
29
 
 
30
class ErrorPage(resource.Resource):
 
31
    def __init__(self, status, brief, detail):
 
32
        resource.Resource.__init__(self)
 
33
        self.code = status
 
34
        self.brief = brief
 
35
        self.detail = detail
 
36
 
 
37
    def render(self, request):
 
38
        request.setResponseCode(self.code)
 
39
        request.setHeader("content-type", "text/html")
 
40
        return ("""<html>
 
41
        <head><title>%s - %s</title></head>
 
42
        <body><h1>%s</h1>
 
43
            <p>%s</p>
 
44
        </body></html>\n\n""" %
 
45
                (self.code, self.brief, self.brief, self.detail))
 
46
 
 
47
    def getChild(self, chnam, request):
 
48
        return self
 
49
 
 
50
 
 
51
class NoResource(ErrorPage):
 
52
    def __init__(self, message="Sorry. No luck finding that resource."):
 
53
        ErrorPage.__init__(self, http.NOT_FOUND,
 
54
                           "No Such Resource",
 
55
                           message)
 
56
 
 
57
class ForbiddenResource(ErrorPage):
 
58
    def __init__(self, message="Sorry, resource is forbidden."):
 
59
        ErrorPage.__init__(self, http.FORBIDDEN,
 
60
                           "Forbidden Resource",
 
61
                           message)