~cbehrens/nova/lp844160-build-works-with-zones

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/twisted/web/error.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- test-case-name: twisted.web.test.test_error -*-
 
2
# Copyright (c) 2001-2010 Twisted Matrix Laboratories.
 
3
# See LICENSE for details.
 
4
 
 
5
"""
 
6
Exception definitions for L{twisted.web}.
 
7
"""
 
8
 
 
9
import operator, warnings
 
10
 
 
11
from twisted.web import http
 
12
 
 
13
 
 
14
class Error(Exception):
 
15
    """
 
16
    A basic HTTP error.
 
17
 
 
18
    @type status: C{str}
 
19
    @ivar status: Refers to an HTTP status code, for example L{http.NOT_FOUND}.
 
20
 
 
21
    @type message: C{str}
 
22
    @param message: A short error message, for example "NOT FOUND".
 
23
 
 
24
    @type response: C{str}
 
25
    @ivar response: A complete HTML document for an error page.
 
26
    """
 
27
    def __init__(self, code, message=None, response=None):
 
28
        """
 
29
        Initializes a basic exception.
 
30
 
 
31
        @type code: C{str}
 
32
        @param code: Refers to an HTTP status code, for example
 
33
            L{http.NOT_FOUND}. If no C{message} is given, C{code} is mapped to a
 
34
            descriptive string that is used instead.
 
35
 
 
36
        @type message: C{str}
 
37
        @param message: A short error message, for example "NOT FOUND".
 
38
 
 
39
        @type response: C{str}
 
40
        @param response: A complete HTML document for an error page.
 
41
        """
 
42
        if not message:
 
43
            try:
 
44
                message = http.responses.get(int(code))
 
45
            except ValueError:
 
46
                # If code wasn't a stringified int, can't map the
 
47
                # status code to a descriptive string so keep message
 
48
                # unchanged.
 
49
                pass
 
50
 
 
51
        Exception.__init__(self, code, message, response)
 
52
        self.status = code
 
53
        self.message = message
 
54
        self.response = response
 
55
 
 
56
 
 
57
    def __str__(self):
 
58
        return '%s %s' % (self[0], self[1])
 
59
 
 
60
 
 
61
 
 
62
class PageRedirect(Error):
 
63
    """
 
64
    A request resulted in an HTTP redirect.
 
65
 
 
66
    @type location: C{str}
 
67
    @ivar location: The location of the redirect which was not followed.
 
68
    """
 
69
    def __init__(self, code, message=None, response=None, location=None):
 
70
        """
 
71
        Initializes a page redirect exception.
 
72
 
 
73
        @type code: C{str}
 
74
        @param code: Refers to an HTTP status code, for example
 
75
            L{http.NOT_FOUND}. If no C{message} is given, C{code} is mapped to a
 
76
            descriptive string that is used instead.
 
77
 
 
78
        @type message: C{str}
 
79
        @param message: A short error message, for example "NOT FOUND".
 
80
 
 
81
        @type response: C{str}
 
82
        @param response: A complete HTML document for an error page.
 
83
 
 
84
        @type location: C{str}
 
85
        @param location: The location response-header field value. It is an
 
86
            absolute URI used to redirect the receiver to a location other than
 
87
            the Request-URI so the request can be completed.
 
88
        """
 
89
        if not message:
 
90
            try:
 
91
                message = http.responses.get(int(code))
 
92
            except ValueError:
 
93
                # If code wasn't a stringified int, can't map the
 
94
                # status code to a descriptive string so keep message
 
95
                # unchanged.
 
96
                pass
 
97
 
 
98
        if location and message:
 
99
            message = "%s to %s" % (message, location)
 
100
 
 
101
        Error.__init__(self, code, message, response)
 
102
        self.location = location
 
103
 
 
104
 
 
105
 
 
106
class InfiniteRedirection(Error):
 
107
    """
 
108
    HTTP redirection is occurring endlessly.
 
109
 
 
110
    @type location: C{str}
 
111
    @ivar location: The first URL in the series of redirections which was
 
112
        not followed.
 
113
    """
 
114
    def __init__(self, code, message=None, response=None, location=None):
 
115
        """
 
116
        Initializes an infinite redirection exception.
 
117
 
 
118
        @type code: C{str}
 
119
        @param code: Refers to an HTTP status code, for example
 
120
            L{http.NOT_FOUND}. If no C{message} is given, C{code} is mapped to a
 
121
            descriptive string that is used instead.
 
122
 
 
123
        @type message: C{str}
 
124
        @param message: A short error message, for example "NOT FOUND".
 
125
 
 
126
        @type response: C{str}
 
127
        @param response: A complete HTML document for an error page.
 
128
 
 
129
        @type location: C{str}
 
130
        @param location: The location response-header field value. It is an
 
131
            absolute URI used to redirect the receiver to a location other than
 
132
            the Request-URI so the request can be completed.
 
133
        """
 
134
        if not message:
 
135
            try:
 
136
                message = http.responses.get(int(code))
 
137
            except ValueError:
 
138
                # If code wasn't a stringified int, can't map the
 
139
                # status code to a descriptive string so keep message
 
140
                # unchanged.
 
141
                pass
 
142
 
 
143
        if location and message:
 
144
            message = "%s to %s" % (message, location)
 
145
 
 
146
        Error.__init__(self, code, message, response)
 
147
        self.location = location
 
148
 
 
149
 
 
150
 
 
151
class UnsupportedMethod(Exception):
 
152
    """
 
153
    Raised by a resource when faced with a strange request method.
 
154
 
 
155
    RFC 2616 (HTTP 1.1) gives us two choices when faced with this situtation:
 
156
    If the type of request is known to us, but not allowed for the requested
 
157
    resource, respond with NOT_ALLOWED.  Otherwise, if the request is something
 
158
    we don't know how to deal with in any case, respond with NOT_IMPLEMENTED.
 
159
 
 
160
    When this exception is raised by a Resource's render method, the server
 
161
    will make the appropriate response.
 
162
 
 
163
    This exception's first argument MUST be a sequence of the methods the
 
164
    resource *does* support.
 
165
    """
 
166
 
 
167
    allowedMethods = ()
 
168
 
 
169
    def __init__(self, allowedMethods, *args):
 
170
        Exception.__init__(self, allowedMethods, *args)
 
171
        self.allowedMethods = allowedMethods
 
172
 
 
173
        if not operator.isSequenceType(allowedMethods):
 
174
            why = "but my first argument is not a sequence."
 
175
            s = ("First argument must be a sequence of"
 
176
                 " supported methods, %s" % (why,))
 
177
            raise TypeError, s
 
178
 
 
179
 
 
180
 
 
181
class SchemeNotSupported(Exception):
 
182
    """
 
183
    The scheme of a URI was not one of the supported values.
 
184
    """
 
185
 
 
186
 
 
187
 
 
188
from twisted.web import resource as _resource
 
189
 
 
190
class ErrorPage(_resource.ErrorPage):
 
191
    """
 
192
    Deprecated alias for L{twisted.web.resource.ErrorPage}.
 
193
    """
 
194
    def __init__(self, *args, **kwargs):
 
195
        warnings.warn(
 
196
            "twisted.web.error.ErrorPage is deprecated since Twisted 9.0.  "
 
197
            "See twisted.web.resource.ErrorPage.", DeprecationWarning,
 
198
            stacklevel=2)
 
199
        _resource.ErrorPage.__init__(self, *args, **kwargs)
 
200
 
 
201
 
 
202
 
 
203
class NoResource(_resource.NoResource):
 
204
    """
 
205
    Deprecated alias for L{twisted.web.resource.NoResource}.
 
206
    """
 
207
    def __init__(self, *args, **kwargs):
 
208
        warnings.warn(
 
209
            "twisted.web.error.NoResource is deprecated since Twisted 9.0.  "
 
210
            "See twisted.web.resource.NoResource.", DeprecationWarning,
 
211
            stacklevel=2)
 
212
        _resource.NoResource.__init__(self, *args, **kwargs)
 
213
 
 
214
 
 
215
 
 
216
class ForbiddenResource(_resource.ForbiddenResource):
 
217
    """
 
218
    Deprecated alias for L{twisted.web.resource.ForbiddenResource}.
 
219
    """
 
220
    def __init__(self, *args, **kwargs):
 
221
        warnings.warn(
 
222
            "twisted.web.error.ForbiddenResource is deprecated since Twisted "
 
223
            "9.0.  See twisted.web.resource.ForbiddenResource.",
 
224
            DeprecationWarning, stacklevel=2)
 
225
        _resource.ForbiddenResource.__init__(self, *args, **kwargs)
 
226
 
 
227
 
 
228
__all__ = [
 
229
    'Error', 'PageRedirect', 'InfiniteRedirection',
 
230
    'ErrorPage', 'NoResource', 'ForbiddenResource']