__init__.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 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. # perhaps it would be better to inherit from Exception or from ScriptError
  34. class CalledModuleError(subprocess.CalledProcessError):
  35. """Raised when a called module ends with error (non-zero return code)
  36. :param module: module name
  37. :param code: some code snipped which contains parameters
  38. :param rc: process returncode
  39. :param error: errors provided by the module (stderr)
  40. """
  41. def __init__(self, module, code, returncode, errors=None):
  42. # CalledProcessError has undocumented constructor
  43. super(CalledModuleError, self).__init__(returncode, module)
  44. msg = _("Module run %s %s ended with error") % (module, code)
  45. msg += _("\nProcess ended with non-zero return code %s") % returncode
  46. if errors:
  47. msg += _(". See the following errors:\n%s") % errors
  48. else:
  49. # here could be written "above" but it wouldn't work in some cases
  50. # e.g., for testing framework
  51. msg += _(". See errors in the (error) output.")
  52. self.msg = msg
  53. # TODO: handle other parameters
  54. def __str__(self):
  55. return self.msg