exceptions.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. ''' Core exceptions raised by the ONVIF Client '''
  2. from suds import WebFault, MethodNotFound, PortNotFound, \
  3. ServiceNotFound, TypeNotFound, BuildError
  4. # Some version dont have this
  5. try:
  6. from suds import SoapHeadersNotPermitted
  7. with_soap_exc = True
  8. except ImportError:
  9. with_soap_exc = False
  10. # Error codes setting
  11. # Error unknown, e.g, HTTP errors
  12. ERR_ONVIF_UNKNOWN = 1
  13. # Protocol error returned by WebService,
  14. # e.g:DataEncodingUnknown, MissingAttr, InvalidArgs, ...
  15. ERR_ONVIF_PROTOCOL = 2
  16. # Error about WSDL instance
  17. ERR_ONVIF_WSDL = 3
  18. # Error about Build
  19. ERR_ONVIF_BUILD = 4
  20. class ONVIFError(Exception):
  21. def __init__(self, err):
  22. if isinstance(err, (WebFault, SoapHeadersNotPermitted) if with_soap_exc else WebFault):
  23. self.reason = err.fault.Reason.Text
  24. self.fault = err.fault
  25. self.code = ERR_ONVIF_PROTOCOL
  26. elif isinstance(err, (ServiceNotFound, PortNotFound,
  27. MethodNotFound, TypeNotFound)):
  28. self.reason = str(err)
  29. self.code = ERR_ONVIF_PROTOCOL
  30. elif isinstance(err, BuildError):
  31. self.reason = str(err)
  32. self.code = ERR_ONVIF_BUILD
  33. else:
  34. self.reason = 'Unknown error: ' + str(err)
  35. self.code = ERR_ONVIF_UNKNOWN
  36. def __str__(self):
  37. return self.reason