core_shell_microgelscostrafo411magnetic_modelrelease_v0.94release_v0.95ticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change
on this file since cf404cb was
eaca9eb,
checked in by Paul Kienzle <pkienzle@…>, 8 years ago
|
error message cleanup
|
-
Property mode set to
100644
|
File size:
1.2 KB
|
Rev | Line | |
---|
[9e430d0] | 1 | """ |
---|
| 2 | Utility to add annotations to python exceptions. |
---|
| 3 | """ |
---|
| 4 | |
---|
[0763009] | 5 | # Platform cruft: WindowsError is only defined on Windows. |
---|
| 6 | try: |
---|
| 7 | WindowsError |
---|
| 8 | except NameError: |
---|
| 9 | class WindowsError(Exception): |
---|
| 10 | pass |
---|
| 11 | |
---|
[9e430d0] | 12 | def 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. |
---|
[d138d43] | 17 | |
---|
[9e430d0] | 18 | Example:: |
---|
[d138d43] | 19 | |
---|
[9e430d0] | 20 | >>> D = {} |
---|
| 21 | >>> try: |
---|
[9404dd3] | 22 | ... print(D['hello']) |
---|
| 23 | ... except Exception as exc: |
---|
[9e430d0] | 24 | ... annotate_exception(exc, "while accessing 'D'") |
---|
| 25 | ... raise |
---|
| 26 | Traceback (most recent call last): |
---|
| 27 | ... |
---|
| 28 | KeyError: "hello while accessing 'D'" |
---|
| 29 | """ |
---|
[0763009] | 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): |
---|
[eaca9eb] | 33 | raise OSError(str(exc)+" "+msg) |
---|
[0763009] | 34 | |
---|
[9e430d0] | 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.