source: sasmodels/sasmodels/exception.py @ eaca9eb

core_shell_microgelscostrafo411magnetic_modelrelease_v0.94release_v0.95ticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since eaca9eb was eaca9eb, checked in by Paul Kienzle <pkienzle@…>, 8 years ago

error message cleanup

  • Property mode set to 100644
File size: 1.2 KB
Line 
1"""
2Utility to add annotations to python exceptions.
3"""
4
5# Platform cruft: WindowsError is only defined on Windows.
6try:
7    WindowsError
8except NameError:
9    class WindowsError(Exception):
10        pass
11
12def annotate_exception(exc, msg):
13    """
14    Add an annotation to the current exception, which can then be forwarded
15    to the caller using a bare "raise" statement to reraise the annotated
16    exception.
17
18    Example::
19
20        >>> D = {}
21        >>> try:
22        ...    print(D['hello'])
23        ... except Exception as exc:
24        ...    annotate_exception(exc, "while accessing 'D'")
25        ...    raise
26        Traceback (most recent call last):
27            ...
28        KeyError: "hello while accessing 'D'"
29    """
30    # Can't extend WindowsError exceptions; instead raise a new exception.
31    # TODO: try to incorporate current stack trace in the raised exception
32    if isinstance(exc, WindowsError):
33        raise OSError(str(exc)+" "+msg)
34
35    args = exc.args
36    if not args:
37        exc.args = (msg,)
38    else:
39        try:
40            arg0 = " ".join((args[0],msg))
41            exc.args = tuple([arg0] + list(args[1:]))
42        except:
43            exc.args = (" ".join((str(exc),msg)),)
Note: See TracBrowser for help on using the repository browser.