1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
5 Exceptions and errors for use in twisted.internet modules.
10 from twisted.python import deprecate
11 from twisted.python.versions import Version
15 class BindError(Exception):
16 """An error occurred binding to an interface"""
21 s = '%s: %s' % (s, ' '.join(self.args))
27 class CannotListenError(BindError):
29 This gets raised by a call to startListening, when the object cannotstart
32 @ivar interface: the interface I tried to listen on
33 @ivar port: the port I tried to listen on
34 @ivar socketError: the exception I got when I tried to listen
35 @type socketError: L{socket.error}
37 def __init__(self, interface, port, socketError):
38 BindError.__init__(self, interface, port, socketError)
39 self.interface = interface
41 self.socketError = socketError
44 iface = self.interface or 'any'
45 return "Couldn't listen on %s:%s: %s." % (iface, self.port,
50 class MulticastJoinError(Exception):
52 An attempt to join a multicast group failed.
57 class MessageLengthError(Exception):
58 """Message is too long to send"""
63 s = '%s: %s' % (s, ' '.join(self.args))
69 class DNSLookupError(IOError):
70 """DNS lookup failed"""
75 s = '%s: %s' % (s, ' '.join(self.args))
81 class ConnectInProgressError(Exception):
82 """A connect operation was started and isn't done yet."""
87 class ConnectError(Exception):
88 """An error occurred while connecting"""
90 def __init__(self, osError=None, string=""):
91 self.osError = osError
92 Exception.__init__(self, string)
95 s = self.__doc__ or self.__class__.__name__
97 s = '%s: %s' % (s, self.osError)
99 s = '%s: %s' % (s, self.args[0])
105 class ConnectBindError(ConnectError):
110 class UnknownHostError(ConnectError):
111 """Hostname couldn't be looked up"""
115 class NoRouteError(ConnectError):
116 """No route to host"""
120 class ConnectionRefusedError(ConnectError):
121 """Connection was refused by other side"""
125 class TCPTimedOutError(ConnectError):
126 """TCP connection timed out"""
130 class BadFileError(ConnectError):
131 """File used for UNIX socket is no good"""
135 class ServiceNameUnknownError(ConnectError):
136 """Service name given as port is unknown"""
140 class UserError(ConnectError):
141 """User aborted connection"""
145 class TimeoutError(UserError):
146 """User timeout caused connection failure"""
150 class SSLError(ConnectError):
151 """An SSL error occurred"""
155 class VerifyError(Exception):
156 """Could not verify something that was supposed to be signed.
161 class PeerVerifyError(VerifyError):
162 """The peer rejected our verify error.
167 class CertificateError(Exception):
169 We did not find a certificate where we expected to find one.
177 errno.ENETUNREACH: NoRouteError,
178 errno.ECONNREFUSED: ConnectionRefusedError,
179 errno.ETIMEDOUT: TCPTimedOutError,
181 if hasattr(errno, "WSAECONNREFUSED"):
182 errnoMapping[errno.WSAECONNREFUSED] = ConnectionRefusedError
183 errnoMapping[errno.WSAENETUNREACH] = NoRouteError
189 def getConnectError(e):
190 """Given a socket exception, return connection error."""
194 return ConnectError(string=e)
196 if hasattr(socket, 'gaierror') and isinstance(e, socket.gaierror):
198 klass = UnknownHostError
200 klass = errnoMapping.get(number, ConnectError)
201 return klass(number, string)
205 class ConnectionClosed(Exception):
207 Connection was closed, whether cleanly or non-cleanly.
212 class ConnectionLost(ConnectionClosed):
213 """Connection to the other side was lost in a non-clean fashion"""
218 s = '%s: %s' % (s, ' '.join(self.args))
224 class ConnectionAborted(ConnectionLost):
226 Connection was aborted locally, using
227 L{twisted.internet.interfaces.ITCPTransport.abortConnection}.
234 class ConnectionDone(ConnectionClosed):
235 """Connection was closed cleanly"""
240 s = '%s: %s' % (s, ' '.join(self.args))
246 class FileDescriptorOverrun(ConnectionLost):
248 A mis-use of L{IUNIXTransport.sendFileDescriptor} caused the connection to
251 Each file descriptor sent using C{sendFileDescriptor} must be associated
252 with at least one byte sent using L{ITransport.write}. If at any point
253 fewer bytes have been written than file descriptors have been sent, the
254 connection is closed with this exception.
259 class ConnectionFdescWentAway(ConnectionLost):
264 class AlreadyCalled(ValueError):
265 """Tried to cancel an already-called event"""
270 s = '%s: %s' % (s, ' '.join(self.args))
276 class AlreadyCancelled(ValueError):
277 """Tried to cancel an already-cancelled event"""
282 s = '%s: %s' % (s, ' '.join(self.args))
288 class PotentialZombieWarning(Warning):
290 Emitted when L{IReactorProcess.spawnProcess} is called in a way which may
291 result in termination of the created child process not being reported.
293 Deprecated in Twisted 10.0.
296 "spawnProcess called, but the SIGCHLD handler is not "
297 "installed. This probably means you have not yet "
298 "called reactor.run, or called "
299 "reactor.run(installSignalHandler=0). You will probably "
300 "never see this process finish, and it may become a "
303 deprecate.deprecatedModuleAttribute(
304 Version("Twisted", 10, 0, 0),
305 "There is no longer any potential for zombie process.",
307 "PotentialZombieWarning")
311 class ProcessDone(ConnectionDone):
312 """A process has ended without apparent errors"""
314 def __init__(self, status):
315 Exception.__init__(self, "process finished with exit code 0")
322 class ProcessTerminated(ConnectionLost):
323 """A process has ended with a probable error condition"""
325 def __init__(self, exitCode=None, signal=None, status=None):
326 self.exitCode = exitCode
330 if exitCode is not None: s = s + " with exit code %s" % exitCode
331 if signal is not None: s = s + " by signal %s" % signal
332 Exception.__init__(self, s)
336 class ProcessExitedAlready(Exception):
338 The process has already exited and the operation requested can no longer
344 class NotConnectingError(RuntimeError):
345 """The Connector was not connecting when it was asked to stop connecting"""
350 s = '%s: %s' % (s, ' '.join(self.args))
356 class NotListeningError(RuntimeError):
357 """The Port was not listening when it was asked to stop listening"""
362 s = '%s: %s' % (s, ' '.join(self.args))
368 class ReactorNotRunning(RuntimeError):
370 Error raised when trying to stop a reactor which is not running.
374 class ReactorNotRestartable(RuntimeError):
376 Error raised when trying to run a reactor which was stopped.
381 class ReactorAlreadyRunning(RuntimeError):
383 Error raised when trying to start the reactor multiple times.
387 class ReactorAlreadyInstalledError(AssertionError):
389 Could not install reactor because one is already installed.
394 class ConnectingCancelledError(Exception):
396 An C{Exception} that will be raised when an L{IStreamClientEndpoint} is
397 cancelled before it connects.
399 @ivar address: The L{IAddress} that is the destination of the
400 cancelled L{IStreamClientEndpoint}.
403 def __init__(self, address):
405 @param address: The L{IAddress} that is the destination of the
406 L{IStreamClientEndpoint} that was cancelled.
408 Exception.__init__(self, address)
409 self.address = address
413 class UnsupportedAddressFamily(Exception):
415 An attempt was made to use a socket with an address family (eg I{AF_INET},
416 I{AF_INET6}, etc) which is not supported by the reactor.
421 class UnsupportedSocketType(Exception):
423 An attempt was made to use a socket of a type (eg I{SOCK_STREAM},
424 I{SOCK_DGRAM}, etc) which is not supported by the reactor.
428 class AlreadyListened(Exception):
430 An attempt was made to listen on a file descriptor which can only be
436 'BindError', 'CannotListenError', 'MulticastJoinError',
437 'MessageLengthError', 'DNSLookupError', 'ConnectInProgressError',
438 'ConnectError', 'ConnectBindError', 'UnknownHostError', 'NoRouteError',
439 'ConnectionRefusedError', 'TCPTimedOutError', 'BadFileError',
440 'ServiceNameUnknownError', 'UserError', 'TimeoutError', 'SSLError',
441 'VerifyError', 'PeerVerifyError', 'CertificateError',
442 'getConnectError', 'ConnectionClosed', 'ConnectionLost',
443 'ConnectionDone', 'ConnectionFdescWentAway', 'AlreadyCalled',
444 'AlreadyCancelled', 'PotentialZombieWarning', 'ProcessDone',
445 'ProcessTerminated', 'ProcessExitedAlready', 'NotConnectingError',
446 'NotListeningError', 'ReactorNotRunning', 'ReactorAlreadyRunning',
447 'ReactorAlreadyInstalledError', 'ConnectingCancelledError',
448 'UnsupportedAddressFamily', 'UnsupportedSocketType']