__init__.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. """GRASS GIS interface to Python exceptions
  2. """
  3. import subprocess
  4. class DBError(Exception):
  5. pass
  6. class FatalError(Exception):
  7. pass
  8. class FlagError(Exception):
  9. pass
  10. class GrassError(Exception):
  11. pass
  12. class ImplementationError(Exception):
  13. pass
  14. class OpenError(Exception):
  15. pass
  16. class ParameterError(Exception):
  17. pass
  18. class ScriptError(Exception):
  19. """Raised during script execution. ::
  20. >>> error = ScriptError('My error message!')
  21. >>> error.value
  22. 'My error message!'
  23. >>> print(error)
  24. My error message!
  25. """
  26. def __init__(self, value):
  27. self.value = value
  28. def __str__(self):
  29. return str(self.value)
  30. class Usage(Exception):
  31. pass
  32. # TODO: we inherit from subprocess to be aligned with check_call but it is needed?
  33. class CalledModuleError(subprocess.CalledProcessError):
  34. """Raised when a called module ends with error (non-zero return code)
  35. :param module: module name
  36. :param code: some code snipped which contains parameters
  37. :param rc: process returncode
  38. :param error: errors provided by the module (stderr)
  39. """
  40. def __init__(self, module, code, returncode, errors=None):
  41. super(CalledModuleError, self).__init__(returncode, module)
  42. msg = _("Module run %s %s ended with error") % (module, code)
  43. msg += _("\nProcess ended with non-zero return code %s") % returncode
  44. if errors:
  45. msg += _(". See the following errors:\n%s") % errors
  46. else:
  47. # here could be written "above" but it wouldn't work in some cases
  48. # e.g., for testing framework
  49. msg += _(". See errors in the (error) output.")
  50. self.msg = msg
  51. # TODO: handle other parameters
  52. def __str__(self):
  53. return self.msg