oop - Python error handling in class hierarchy -


i working on subsystem in python, has following structure:

  1. facade, provides interface communication other subsystems (name myfacade)
  2. engine (singleton) - receives requests facade , dispathes them "working" classes (myengine)
  3. working modules - such myfilesystem, mynetwork, mydb , on.

thus there 3 general levels: facade - engine - worker. if error occurs somewhere, facade must , show client, , application must not crash.

what best-practice way organize error handling errors on 3 levels of abstraction in python? if error occurs on lowest level (worker) should catch it, or return none/false? should done on upper levels provide necessary functionality , avoid violating dry, srp, , on?

suppose have function/method worker_method on worker level. inside method perform number of operations. of them critical - if fail, want stop executing worker_method , report failure engine. however, have 1 call inside worker_method don't care of, it's allowed fail. should this:

def worker_method(self):     """..."""     critical_call()     another_critical_call()     try:          dummy_call()     except exception, e:          # caught non-critical exception            logger.log_error(e.message)     """...""" 

consider method in engine calls above one:

def engine_method(self):     try:            some_engine_internal_stuff()          """..."""          data_from_worker = worker_instance.worker_method()          """..."""          self.set_last_error(none)          return engine_operating(data_from_worker)     except exception, e:          self.set_last_error(e.message)          return none 

consider method in facade calls above one:

def facade_method(self):     try:         """..."""         data_from_engine = engine_instance.engine_method()         if data_from_engine none , engine_instance.get_last_error() not none:             return facade_prepare_data_to_return_to_client(engine_instance.get_last_error())          return facade_prepare_data_to_return_to_client(data_from_engine)     except exception, e:         return facade_prepare_data_to_return_to_client(e.message) 

in case engine instance works in "bulletproof" mode , prevents exceptions approaching facade level. failures indicated return values. on facade level again use try/catch because despite fact engine "bulletproof" there still can exceptions in facade methods , must catch them in order prevent app possible crash.

does model satisfy python best-practice , general design standards? can improved or simplified?


Comments