1
# -*- test-case-name: twisted.web.test.test_error -*-
2
# Copyright (c) 2001-2010 Twisted Matrix Laboratories.
3
# See LICENSE for details.
6
Exception definitions for L{twisted.web}.
9
import operator, warnings
11
from twisted.web import http
14
class Error(Exception):
19
@ivar status: Refers to an HTTP status code, for example L{http.NOT_FOUND}.
22
@param message: A short error message, for example "NOT FOUND".
24
@type response: C{str}
25
@ivar response: A complete HTML document for an error page.
27
def __init__(self, code, message=None, response=None):
29
Initializes a basic exception.
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.
37
@param message: A short error message, for example "NOT FOUND".
39
@type response: C{str}
40
@param response: A complete HTML document for an error page.
44
message = http.responses.get(int(code))
46
# If code wasn't a stringified int, can't map the
47
# status code to a descriptive string so keep message
51
Exception.__init__(self, code, message, response)
53
self.message = message
54
self.response = response
58
return '%s %s' % (self[0], self[1])
62
class PageRedirect(Error):
64
A request resulted in an HTTP redirect.
66
@type location: C{str}
67
@ivar location: The location of the redirect which was not followed.
69
def __init__(self, code, message=None, response=None, location=None):
71
Initializes a page redirect exception.
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.
79
@param message: A short error message, for example "NOT FOUND".
81
@type response: C{str}
82
@param response: A complete HTML document for an error page.
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.
91
message = http.responses.get(int(code))
93
# If code wasn't a stringified int, can't map the
94
# status code to a descriptive string so keep message
98
if location and message:
99
message = "%s to %s" % (message, location)
101
Error.__init__(self, code, message, response)
102
self.location = location
106
class InfiniteRedirection(Error):
108
HTTP redirection is occurring endlessly.
110
@type location: C{str}
111
@ivar location: The first URL in the series of redirections which was
114
def __init__(self, code, message=None, response=None, location=None):
116
Initializes an infinite redirection exception.
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.
123
@type message: C{str}
124
@param message: A short error message, for example "NOT FOUND".
126
@type response: C{str}
127
@param response: A complete HTML document for an error page.
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.
136
message = http.responses.get(int(code))
138
# If code wasn't a stringified int, can't map the
139
# status code to a descriptive string so keep message
143
if location and message:
144
message = "%s to %s" % (message, location)
146
Error.__init__(self, code, message, response)
147
self.location = location
151
class UnsupportedMethod(Exception):
153
Raised by a resource when faced with a strange request method.
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.
160
When this exception is raised by a Resource's render method, the server
161
will make the appropriate response.
163
This exception's first argument MUST be a sequence of the methods the
164
resource *does* support.
169
def __init__(self, allowedMethods, *args):
170
Exception.__init__(self, allowedMethods, *args)
171
self.allowedMethods = allowedMethods
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,))
181
class SchemeNotSupported(Exception):
183
The scheme of a URI was not one of the supported values.
188
from twisted.web import resource as _resource
190
class ErrorPage(_resource.ErrorPage):
192
Deprecated alias for L{twisted.web.resource.ErrorPage}.
194
def __init__(self, *args, **kwargs):
196
"twisted.web.error.ErrorPage is deprecated since Twisted 9.0. "
197
"See twisted.web.resource.ErrorPage.", DeprecationWarning,
199
_resource.ErrorPage.__init__(self, *args, **kwargs)
203
class NoResource(_resource.NoResource):
205
Deprecated alias for L{twisted.web.resource.NoResource}.
207
def __init__(self, *args, **kwargs):
209
"twisted.web.error.NoResource is deprecated since Twisted 9.0. "
210
"See twisted.web.resource.NoResource.", DeprecationWarning,
212
_resource.NoResource.__init__(self, *args, **kwargs)
216
class ForbiddenResource(_resource.ForbiddenResource):
218
Deprecated alias for L{twisted.web.resource.ForbiddenResource}.
220
def __init__(self, *args, **kwargs):
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)
229
'Error', 'PageRedirect', 'InfiniteRedirection',
230
'ErrorPage', 'NoResource', 'ForbiddenResource']