File: exceptions.py

package info (click to toggle)
python-prance 25.4.8.0%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,140 kB
  • sloc: python: 3,381; makefile: 205
file content (26 lines) | stat: -rw-r--r-- 833 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
"""This submodule contains helpers for exception handling."""

__author__ = "Jens Finkhaeuser"
__copyright__ = "Copyright (c) 2018,2019 Jens Finkhaeuser"
__license__ = "MIT"
__all__ = ()


# Raise the given exception class from the caught exception, preserving
# stack trace and message as much as possible.
def raise_from(klass, from_value, extra_message=None):
    try:
        if from_value is None:
            if extra_message is not None:
                raise klass(extra_message)
            raise klass()

        args = list(from_value.args)
        if extra_message is not None:
            if len(args) and isinstance(args[0], str):
                args[0] += " -- " + extra_message
            else:
                args.append(extra_message)
        raise klass(*args) from from_value
    finally:
        klass = None