OLD | NEW |
| (Empty) |
1 # Copyright (c) 2001-2008 Twisted Matrix Laboratories. | |
2 # See LICENSE for details. | |
3 | |
4 | |
5 """ | |
6 Exceptions and errors for use in twisted.internet modules. | |
7 | |
8 Maintainer: U{Itamar Shtull-Trauring<mailto:twisted@itamarst.org>} | |
9 """ | |
10 | |
11 import socket | |
12 | |
13 | |
14 class BindError(Exception): | |
15 """An error occurred binding to an interface""" | |
16 | |
17 def __str__(self): | |
18 s = self.__doc__ | |
19 if self.args: | |
20 s = '%s: %s' % (s, ' '.join(self.args)) | |
21 s = '%s.' % s | |
22 return s | |
23 | |
24 class CannotListenError(BindError): | |
25 """This gets raised by a call to startListening, when the object cannot star
t listening. | |
26 | |
27 @ivar interface: the interface I tried to listen on | |
28 @ivar port: the port I tried to listen on | |
29 @ivar socketError: the exception I got when I tried to listen | |
30 @type socketError: L{socket.error} | |
31 """ | |
32 def __init__(self, interface, port, socketError): | |
33 BindError.__init__(self, interface, port, socketError) | |
34 self.interface = interface | |
35 self.port = port | |
36 self.socketError = socketError | |
37 | |
38 def __str__(self): | |
39 iface = self.interface or 'any' | |
40 return "Couldn't listen on %s:%s: %s." % (iface, self.port, | |
41 self.socketError) | |
42 | |
43 | |
44 class MulticastJoinError(Exception): | |
45 """ | |
46 An attempt to join a multicast group failed. | |
47 """ | |
48 | |
49 | |
50 class MessageLengthError(Exception): | |
51 """Message is too long to send""" | |
52 | |
53 def __str__(self): | |
54 s = self.__doc__ | |
55 if self.args: | |
56 s = '%s: %s' % (s, ' '.join(self.args)) | |
57 s = '%s.' % s | |
58 return s | |
59 | |
60 | |
61 class DNSLookupError(IOError): | |
62 """DNS lookup failed""" | |
63 | |
64 def __str__(self): | |
65 s = self.__doc__ | |
66 if self.args: | |
67 s = '%s: %s' % (s, ' '.join(self.args)) | |
68 s = '%s.' % s | |
69 return s | |
70 | |
71 | |
72 class ConnectInProgressError(Exception): | |
73 """A connect operation was started and isn't done yet.""" | |
74 | |
75 | |
76 # connection errors | |
77 | |
78 class ConnectError(Exception): | |
79 """An error occurred while connecting""" | |
80 | |
81 def __init__(self, osError=None, string=""): | |
82 self.osError = osError | |
83 Exception.__init__(self, string) | |
84 | |
85 def __str__(self): | |
86 s = self.__doc__ or self.__class__.__name__ | |
87 if self.osError: | |
88 s = '%s: %s' % (s, self.osError) | |
89 if self[0]: | |
90 s = '%s: %s' % (s, self[0]) | |
91 s = '%s.' % s | |
92 return s | |
93 | |
94 | |
95 class ConnectBindError(ConnectError): | |
96 """Couldn't bind""" | |
97 | |
98 | |
99 class UnknownHostError(ConnectError): | |
100 """Hostname couldn't be looked up""" | |
101 | |
102 | |
103 class NoRouteError(ConnectError): | |
104 """No route to host""" | |
105 | |
106 | |
107 class ConnectionRefusedError(ConnectError): | |
108 """Connection was refused by other side""" | |
109 | |
110 | |
111 class TCPTimedOutError(ConnectError): | |
112 """TCP connection timed out""" | |
113 | |
114 | |
115 class BadFileError(ConnectError): | |
116 """File used for UNIX socket is no good""" | |
117 | |
118 | |
119 class ServiceNameUnknownError(ConnectError): | |
120 """Service name given as port is unknown""" | |
121 | |
122 | |
123 class UserError(ConnectError): | |
124 """User aborted connection""" | |
125 | |
126 | |
127 class TimeoutError(UserError): | |
128 """User timeout caused connection failure""" | |
129 | |
130 class SSLError(ConnectError): | |
131 """An SSL error occurred""" | |
132 | |
133 class VerifyError(Exception): | |
134 """Could not verify something that was supposed to be signed. | |
135 """ | |
136 | |
137 class PeerVerifyError(VerifyError): | |
138 """The peer rejected our verify error. | |
139 """ | |
140 | |
141 class CertificateError(Exception): | |
142 """ | |
143 We did not find a certificate where we expected to find one. | |
144 """ | |
145 | |
146 try: | |
147 import errno | |
148 errnoMapping = { | |
149 errno.ENETUNREACH: NoRouteError, | |
150 errno.ECONNREFUSED: ConnectionRefusedError, | |
151 errno.ETIMEDOUT: TCPTimedOutError, | |
152 } | |
153 if hasattr(errno, "WSAECONNREFUSED"): | |
154 errnoMapping[errno.WSAECONNREFUSED] = ConnectionRefusedError | |
155 errnoMapping[errno.WSAENETUNREACH] = NoRouteError | |
156 except ImportError: | |
157 errnoMapping = {} | |
158 | |
159 def getConnectError(e): | |
160 """Given a socket exception, return connection error.""" | |
161 try: | |
162 number, string = e | |
163 except ValueError: | |
164 return ConnectError(string=e) | |
165 | |
166 if hasattr(socket, 'gaierror') and isinstance(e, socket.gaierror): | |
167 # only works in 2.2 | |
168 klass = UnknownHostError | |
169 else: | |
170 klass = errnoMapping.get(number, ConnectError) | |
171 return klass(number, string) | |
172 | |
173 | |
174 | |
175 class ConnectionClosed(Exception): | |
176 """ | |
177 Connection was closed, whether cleanly or non-cleanly. | |
178 """ | |
179 | |
180 | |
181 | |
182 class ConnectionLost(ConnectionClosed): | |
183 """Connection to the other side was lost in a non-clean fashion""" | |
184 | |
185 def __str__(self): | |
186 s = self.__doc__ | |
187 if self.args: | |
188 s = '%s: %s' % (s, ' '.join(self.args)) | |
189 s = '%s.' % s | |
190 return s | |
191 | |
192 | |
193 | |
194 class ConnectionDone(ConnectionClosed): | |
195 """Connection was closed cleanly""" | |
196 | |
197 def __str__(self): | |
198 s = self.__doc__ | |
199 if self.args: | |
200 s = '%s: %s' % (s, ' '.join(self.args)) | |
201 s = '%s.' % s | |
202 return s | |
203 | |
204 | |
205 class ConnectionFdescWentAway(ConnectionLost): | |
206 """Uh""" #TODO | |
207 | |
208 | |
209 class AlreadyCalled(ValueError): | |
210 """Tried to cancel an already-called event""" | |
211 | |
212 def __str__(self): | |
213 s = self.__doc__ | |
214 if self.args: | |
215 s = '%s: %s' % (s, ' '.join(self.args)) | |
216 s = '%s.' % s | |
217 return s | |
218 | |
219 | |
220 class AlreadyCancelled(ValueError): | |
221 """Tried to cancel an already-cancelled event""" | |
222 | |
223 def __str__(self): | |
224 s = self.__doc__ | |
225 if self.args: | |
226 s = '%s: %s' % (s, ' '.join(self.args)) | |
227 s = '%s.' % s | |
228 return s | |
229 | |
230 | |
231 | |
232 class PotentialZombieWarning(Warning): | |
233 """ | |
234 Emitted when L{IReactorProcess.spawnProcess} is called in a way which may | |
235 result in termination of the created child process not being reported. | |
236 """ | |
237 MESSAGE = ( | |
238 "spawnProcess called, but the SIGCHLD handler is not " | |
239 "installed. This probably means you have not yet " | |
240 "called reactor.run, or called " | |
241 "reactor.run(installSignalHandler=0). You will probably " | |
242 "never see this process finish, and it may become a " | |
243 "zombie process.") | |
244 | |
245 | |
246 | |
247 class ProcessDone(ConnectionDone): | |
248 """A process has ended without apparent errors""" | |
249 | |
250 def __init__(self, status): | |
251 Exception.__init__(self, "process finished with exit code 0") | |
252 self.exitCode = 0 | |
253 self.signal = None | |
254 self.status = status | |
255 | |
256 | |
257 class ProcessTerminated(ConnectionLost): | |
258 """A process has ended with a probable error condition""" | |
259 | |
260 def __init__(self, exitCode=None, signal=None, status=None): | |
261 self.exitCode = exitCode | |
262 self.signal = signal | |
263 self.status = status | |
264 s = "process ended" | |
265 if exitCode is not None: s = s + " with exit code %s" % exitCode | |
266 if signal is not None: s = s + " by signal %s" % signal | |
267 Exception.__init__(self, s) | |
268 | |
269 | |
270 class ProcessExitedAlready(Exception): | |
271 """The process has already excited, and the operation requested can no longe
r be performed.""" | |
272 | |
273 | |
274 class NotConnectingError(RuntimeError): | |
275 """The Connector was not connecting when it was asked to stop connecting""" | |
276 | |
277 def __str__(self): | |
278 s = self.__doc__ | |
279 if self.args: | |
280 s = '%s: %s' % (s, ' '.join(self.args)) | |
281 s = '%s.' % s | |
282 return s | |
283 | |
284 class NotListeningError(RuntimeError): | |
285 """The Port was not listening when it was asked to stop listening""" | |
286 | |
287 def __str__(self): | |
288 s = self.__doc__ | |
289 if self.args: | |
290 s = '%s: %s' % (s, ' '.join(self.args)) | |
291 s = '%s.' % s | |
292 return s | |
293 | |
294 | |
295 class ReactorNotRunning(RuntimeError): | |
296 """ | |
297 Error raised when trying to stop a reactor which is not running. | |
298 """ | |
299 | |
300 | |
301 class ReactorAlreadyRunning(RuntimeError): | |
302 """ | |
303 Error raised when trying to start the reactor multiple times. | |
304 """ | |
305 | |
OLD | NEW |