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