Commit 9fa43955 authored by Julien Muchembled's avatar Julien Muchembled

Simplify EventHandler by removing 'error_dispatch_table'

git-svn-id: https://svn.erp5.org/repos/neo/trunk@2688 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 3bfbf89b
......@@ -26,7 +26,6 @@ class EventHandler(object):
def __init__(self, app):
self.app = app
self.error_dispatch_table = self.__initErrorDispatchTable()
def __repr__(self):
return self.__class__.__name__
......@@ -62,7 +61,7 @@ class EventHandler(object):
conn.abort()
self.peerBroken(conn)
except BrokenNodeDisallowedError:
conn.answer(Errors.Broken('go away'))
conn.answer(Errors.BrokenNode('go away'))
conn.abort()
self.connectionClosed(conn)
except NotReadyError, message:
......@@ -142,23 +141,10 @@ class EventHandler(object):
def error(self, conn, code, message):
try:
method = self.error_dispatch_table[code]
method(conn, message)
except ValueError:
getattr(self, Errors[code])(conn, message)
except (AttributeError, ValueError):
raise UnexpectedPacketError(message)
def notReady(self, conn, message):
raise UnexpectedPacketError
def oidNotFound(self, conn, message):
raise UnexpectedPacketError
def oidDoesNotExist(self, conn, message):
raise UnexpectedPacketError
def tidNotFound(self, conn, message):
raise UnexpectedPacketError
def protocolError(self, conn, message):
# the connection should have been closed by the remote peer
neo.lib.logging.error('protocol error: %s' % (message,))
......@@ -174,21 +160,3 @@ class EventHandler(object):
def ack(self, conn, message):
neo.lib.logging.debug("no error message : %s" % (message))
# Fetch tables initialization
def __initErrorDispatchTable(self):
d = {}
d[ErrorCodes.ACK] = self.ack
d[ErrorCodes.NOT_READY] = self.notReady
d[ErrorCodes.OID_NOT_FOUND] = self.oidNotFound
d[ErrorCodes.OID_DOES_NOT_EXIST] = self.oidDoesNotExist
d[ErrorCodes.TID_NOT_FOUND] = self.tidNotFound
d[ErrorCodes.PROTOCOL_ERROR] = self.protocolError
d[ErrorCodes.BROKEN_NODE] = self.brokenNodeDisallowedError
d[ErrorCodes.ALREADY_PENDING] = self.alreadyPendingError
return d
......@@ -27,8 +27,6 @@ class PacketLogger(object):
""" Logger at packet level (for debugging purpose) """
def __init__(self):
_temp = EventHandler(None)
self.error_dispatch_table = _temp.error_dispatch_table
self.enable(LOGGER_ENABLED)
def enable(self, enabled):
......
......@@ -1535,26 +1535,16 @@ class Packets(dict):
NotifyTransactionFinished = register(
0x003E, NotifyTransactionFinished)
def register_error(code):
def wrapper(registry, message=''):
return Error(code, message)
return wrapper
class ErrorRegistry(dict):
"""
Error packet packet registry
"""
def __init__(self):
dict.__init__(self)
Ack = register_error(ErrorCodes.ACK)
ProtocolError = register_error(ErrorCodes.PROTOCOL_ERROR)
TidNotFound = register_error(ErrorCodes.TID_NOT_FOUND)
OidNotFound = register_error(ErrorCodes.OID_NOT_FOUND)
OidDoesNotExist = register_error(ErrorCodes.OID_DOES_NOT_EXIST)
NotReady = register_error(ErrorCodes.NOT_READY)
Broken = register_error(ErrorCodes.BROKEN_NODE)
AlreadyPending = register_error(ErrorCodes.ALREADY_PENDING)
Errors = ErrorRegistry()
def Errors():
registry_dict = {}
handler_method_name_dict = {}
def register_error(code):
return lambda self, message='': Error(code, message)
for code, error in ErrorCodes.iteritems():
name = ''.join(part.capitalize() for part in str(error).split('_'))
registry_dict[name] = register_error(error)
handler_method_name_dict[code] = name[0].lower() + name[1:]
return type('ErrorRegistry', (dict,),
registry_dict)(handler_method_name_dict)
Errors = Errors()
......@@ -44,7 +44,7 @@ class ProtocolTests(NeoUnitTestBase):
self.assertEqual(error_msg, "wait")
def test_06_brokenNodeDisallowedError(self):
p = Errors.Broken("broken")
p = Errors.BrokenNode("broken")
error_code, error_msg = p.decode()
self.assertEqual(error_code, ErrorCodes.BROKEN_NODE)
self.assertEqual(error_msg, "broken")
......
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