Commit b46d561c authored by Grégory Wisniewski's avatar Grégory Wisniewski

Remove except statements that catch all exceptions in connection.py and

connector.py by adding ConnectorException as base exception class and a 
ConnectorConnectionRefusedException raised in makeClientConnection() or 
recv(). All sockets error are wrapped in ConnectorException and special
(unknown) cases are not catched in connection.py


git-svn-id: https://svn.erp5.org/repos/neo/branches/prototype3@492 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent e8db4fc3
......@@ -23,7 +23,8 @@ import traceback
from neo import protocol
from neo.protocol import Packet, ProtocolError
from neo.event import IdleEvent
from neo.connector import ConnectorTryAgainException, ConnectorInProgressException
from neo.connector import ConnectorException, ConnectorTryAgainException, \
ConnectorInProgressException, ConnectorConnectionRefusedException
from neo.util import dump
from neo.exception import OperationFailure
......@@ -238,43 +239,43 @@ class Connection(BaseConnection):
def recv(self):
"""Receive data from a connector."""
try:
r = self.connector.receive()
if not r:
logging.error('cannot read')
data = self.connector.receive()
if not data:
self.handler.connectionClosed(self)
self.close()
else:
self.read_buf += r
return
self.read_buf += data
except ConnectorTryAgainException:
pass
except OperationFailure:
raise
except:
traceback.print_exc()
logging.warning('recv called on %s(%s) failed.'%(self, self.getAddress()))
except ConnectorConnectionRefusedException:
# should only occur while connecting
assert self.connecting
self.handler.connectionFailed(self)
self.close()
except ConnectorException:
self.handler.connectionClosed(self)
self.close()
# unhandled connector exception
raise
def send(self):
"""Send data to a connector."""
if self.write_buf:
if not self.write_buf:
return
try:
r = self.connector.send(self.write_buf)
if not r:
logging.error('cannot write')
n = self.connector.send(self.write_buf)
if not n:
self.handler.connectionClosed(self)
self.close()
else:
self.write_buf = self.write_buf[r:]
except ConnectorTryAgainException:
return
except OperationFailure:
raise
except:
traceback.print_exc()
logging.warning('send called on %s(%s) failed.'%(self, self.getAddress()))
self.write_buf = self.write_buf[n:]
except ConnectorTryAgainException:
pass
except ConnectorException:
self.handler.connectionClosed(self)
self.close()
# unhandled connector exception
raise
def addPacket(self, packet):
"""Add a packet into the write buffer."""
......@@ -363,11 +364,14 @@ class ClientConnection(Connection):
self.connecting = False
self.handler.connectionCompleted(self)
event_manager.addReader(self)
except:
traceback.print_exc()
logging.warning('init called on %s(%s) failed.'%(self, self.getAddress()))
except ConnectorConnectionRefusedException:
handler.connectionFailed(self)
self.close()
except ConnectorException, msg:
# unhandled connector exception
handler.connectionFailed(self)
self.close()
raise
def writable(self):
"""Called when self is writable."""
......
......@@ -49,12 +49,12 @@ class SocketConnector:
try:
self.socket.setblocking(0)
self.socket.connect(addr)
except socket.error, m:
if m[0] == errno.EINPROGRESS:
except socket.error, (err, errmsg):
if err == errno.EINPROGRESS:
raise ConnectorInProgressException
else:
logging.error('makeClientConnection: %s', m[1])
raise
if err == errno.ECONNREFUSED:
raise ConnectorConnectionRefusedException
raise ConnectorException, 'makeClientConnection failed: %s:%s' % (err, errmsg)
def makeListeningConnection(self, addr):
try:
......@@ -62,9 +62,9 @@ class SocketConnector:
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.bind(addr)
self.socket.listen(5)
except:
except socket.error, (err, errmsg):
self.socket.close()
raise
raise ConnectorException, 'makeListeningConnection failed: %s:%s' % (err, errmsg)
def getError(self):
return self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
......@@ -80,9 +80,7 @@ class SocketConnector:
except socket.error, m:
if m[0] == errno.EAGAIN:
raise ConnectorTryAgainException
else:
logging.error('getNewConnection: %s', m[1])
raise
raise ConnectorException, 'getNewConnection failed: %s:%s' % (err, errmsg)
def shutdown(self):
# This may fail if the socket is not connected.
......@@ -94,41 +92,28 @@ class SocketConnector:
def receive(self):
try:
return self.socket.recv(4096)
except socket.error, m:
if m[0] == errno.EAGAIN:
except socket.error, (err, errmsg):
if err == errno.EAGAIN:
raise ConnectorTryAgainException
else:
logging.error('receive: %s', m[1])
raise
if err == errno.ECONNREFUSED:
raise ConnectorConnectionRefusedException
raise ConnectorException, 'receive failed: %s:%s' % (err, errmsg)
def send(self, msg):
try:
return self.socket.send(msg)
except socket.error, m:
if m[0] == errno.EAGAIN:
except socket.error, (err, errmsg):
if err == errno.EAGAIN:
raise ConnectorTryAgainException
else:
logging.error('send: %s', m[1])
raise
raise ConnectorException, 'send failed: %s:%s' % (err, errmsg)
def close(self):
return self.socket.close()
registerConnectorHandler(SocketConnector)
class ConnectorTryAgainException(Exception): pass
class ConnectorInProgressException(Exception): pass
class ConnectorException(Exception): pass
class ConnectorTryAgainException(ConnectorException): pass
class ConnectorInProgressException(ConnectorException): pass
class ConnectorConnectionRefusedException(ConnectorException): pass
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment