AdvancedClimateSystems / uModbus

Python implementation of the Modbus protocol.
Mozilla Public License 2.0
211 stars 82 forks source link

`RequestHandler.process()` should not catch exceptions. #35

Open OrangeTux opened 8 years ago

OrangeTux commented 8 years ago

It's currently hard to catch exceptions raised during processing a Modbus request in a subclass of RequestHandler.process() because RequestHandler.process() catches all exceptions.

It would be better to create a method RequestHandler.process_exceptions which takes the exception and the request ADU and returns the reponse ADU.

RequestHandler(BaseRequestHandler):
  def handle(self):
    try:
      req_adu = socket.recv()
      resp_adu = self.process(req_adu)
    except Exception as e: 
      resp_adu =  self.process_exception(e, req_adu)

    self.respond(resp_adu)

  def process_exception(self, e, req_adu):
    """ Based on error occured return correct response ADU. """
    pass