|
1 | 1 | ############################################################################### |
2 | 2 | # interface for translating GAMMA errors messages into Python error types |
3 | 3 |
|
4 | | -# Copyright (c) 2015-2019, the pyroSAR Developers. |
| 4 | +# Copyright (c) 2015-2026, the pyroSAR Developers. |
5 | 5 |
|
6 | 6 | # This file is part of the pyroSAR Project. It is subject to the |
7 | 7 | # license terms in the LICENSE.txt file found in the top-level |
|
13 | 13 | ############################################################################### |
14 | 14 |
|
15 | 15 | import re |
| 16 | +import signal |
16 | 17 |
|
17 | 18 |
|
18 | | -def gammaErrorHandler(out, err): |
| 19 | +def gammaErrorHandler(returncode: int, out: str, err: str) -> None: |
19 | 20 | """ |
20 | | - Function to raise errors in Python. This function is not intended for direct use, but as part of function gamma.util.process |
21 | | - Args: |
22 | | - out: the stdout message returned by a subprocess call of a gamma command |
23 | | - err: the stderr message returned by a subprocess call of a gamma command |
| 21 | + Function to raise errors in Python. This function is not intended |
| 22 | + for direct use but as part of function :func:`pyroSAR.gamma.auxil.process`. |
| 23 | + |
| 24 | + Parameters |
| 25 | + ---------- |
| 26 | + returncode: |
| 27 | + the subprocess return code |
| 28 | + out: |
| 29 | + the stdout message returned by a subprocess call of a gamma command |
| 30 | + err: |
| 31 | + the stderr message returned by a subprocess call of a gamma command |
24 | 32 |
|
25 | | - Raises: IOError | ValueError | RuntimeError | None |
| 33 | + Raises: IOError | ValueError | RuntimeError |
26 | 34 |
|
27 | 35 | """ |
28 | 36 |
|
@@ -59,26 +67,33 @@ def gammaErrorHandler(out, err): |
59 | 67 | 'cannot create ISP image parameter file': OSError} |
60 | 68 |
|
61 | 69 | # check if the error message is known and throw the mapped error from knownErrors accordingly. |
62 | | - # Otherwise throw an GammaUnknownError. |
63 | | - # The actual message is passed to the error and thus visible for backtracing |
64 | | - if len(errormessages) > 0: |
65 | | - errormessage = errormessages[-1] |
66 | | - err_out = '\n\n'.join([re.sub('ERROR[: ]*', '', x) for x in errormessages]) |
67 | | - for error in knownErrors: |
68 | | - if re.search(error, errormessage): |
69 | | - errortype = knownErrors[error] |
70 | | - if errortype: |
71 | | - raise errortype(err_out) |
72 | | - else: |
73 | | - return |
| 70 | + # Otherwise raise a RuntimeError if killed by a signal and a GammaUnknownError in all other cases. |
| 71 | + # The actual message is passed to the error and thus visible for backtracing. |
| 72 | + if returncode != 0: |
| 73 | + if len(errormessages) > 0: |
| 74 | + errormessage = errormessages[-1] |
| 75 | + err_out = '\n\n'.join([re.sub('ERROR[: ]*', '', x) for x in errormessages]) |
| 76 | + for error in knownErrors: |
| 77 | + if re.search(error, errormessage): |
| 78 | + errortype = knownErrors[error] |
| 79 | + if errortype: |
| 80 | + raise errortype(err_out) |
| 81 | + else: |
| 82 | + return |
| 83 | + else: |
| 84 | + err_out = f'failed with return code {returncode}' |
| 85 | + if returncode < 0: |
| 86 | + # handle signal kills like SIGSEGV (segmentation fault) |
| 87 | + sig = signal.Signals(-returncode) |
| 88 | + raise RuntimeError(err_out + f' ({sig.name})') |
74 | 89 | raise GammaUnknownError(err_out) |
75 | 90 |
|
76 | 91 |
|
77 | 92 | class GammaUnknownError(Exception): |
78 | 93 | """ |
79 | 94 | This is a general error, which is raised if the error message is not yet integrated |
80 | | - into the known errors of function gammaErrorHandler. |
81 | | - If this error occurs the message should be included in function gammaErrorHandler. |
| 95 | + into the known errors of function :func:`gammaErrorHandler`. |
| 96 | + If this error occurs, the message should be included in this function. |
82 | 97 | """ |
83 | 98 |
|
84 | 99 | def __init__(self, errormessage): |
|
0 commit comments