Soaplib – catch exceptions inside de web service

We've developed a decorator intended to be placed on top of the "soapmethod" decorator that allows you to catch any exception raised by the web method.

Usage:

  1.  
  2. @catchWsExceptions
  3. @soapmethod(String, _returns=Array(String))
  4. def srv_whatever(self, someParam):
  5. # just business logic, no exception handling required.
  6.  

Decorator code:

  1.  
  2. import inspect
  3. import traceback
  4.  
  5. def catchWsExceptions(func):
  6. def wrapped(*args, **kwargs):
  7. if func.__module__ <> "soaplib.service":
  8. raise ApplicationException("catchWsExceptions decorator can only be used before soapmethod decorator")
  9. try:
  10. result = func(*args, **kwargs)
  11. return result
  12. except Exception, e:
  13. tb= sys.exc_info()[2]
  14. tpl = traceback.extract_tb(tb)
  15. failedCall = str(tpl[-1][0])
  16. if re.search("soaplib", failedCall):
  17. Logger.getInstance().logException(e)
  18. raise # this is not an exception within the function but before calling the actual function
  19. if type(e) == type(TypeError):
  20. raise # API mistmach exception should not be filtered
  21. msg = ""
  22. if hasattr(e, "message"):
  23. msg = e.message
  24. Logger.getInstance().logException(e)
  25. return ['2', msg + str(e.args)]
  26. wrapped.__doc__ = func.__doc__
  27. wrapped.func_name = func.func_name
  28. wrapped._is_soap_method = True
  29. return wrapped
  30.  

Logger and ApplicationException belong in our code base but you can just replace them. You may also raise the caught exception rather than return an array with an error code. It is a design decision.

Enjoyed reading this post?
Subscribe to the RSS feed and have all new posts delivered straight to you.
  • http://www.sellwinterboots.com boots

    In Case B it tries to lock the files in the Bin-directory, which then leads to the locking-conflict described above.