summaryrefslogtreecommitdiff
path: root/src/zope/configuration/exceptions.py
blob: faa9bfb5cc8df2abf126f4551d36087dd08274f7 (plain)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
##############################################################################
#
# Copyright (c) 2002 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Standard configuration errors
"""

import traceback


__all__ = [
    'ConfigurationError',
]


class ConfigurationError(Exception):
    """There was an error in a configuration
    """

    # A list of strings or things that can be converted to strings,
    # added by append_details as we walk back up the call/include stack.
    # We will print them in reverse order so that the most recent detail is
    # last.
    _details = ()

    def add_details(self, info):
        if isinstance(info, BaseException):
            info = traceback.format_exception_only(type(info), info)
            # Trim trailing newline
            info[-1] = info[-1].rstrip()
            self._details += tuple(info)
        else:
            self._details += (info,)
        return self

    def _with_details(self, opening, detail_formatter):
        lines = ['    ' + detail_formatter(detail) for detail in self._details]
        lines.append(opening)
        lines.reverse()
        return '\n'.join(lines)

    def __str__(self):
        s = super(ConfigurationError, self).__str__()
        return self._with_details(s, str)

    def __repr__(self):
        s = super(ConfigurationError, self).__repr__()
        return self._with_details(s, repr)


class ConfigurationWrapperError(ConfigurationError):

    USE_INFO_REPR = False

    def __init__(self, info, exception):
        super(ConfigurationWrapperError, self).__init__(
            repr(info) if self.USE_INFO_REPR else info)
        self.add_details(exception)

        # This stuff is undocumented and not used but we store
        # for backwards compatibility
        self.info = info
        self.etype = type(exception)
        self.evalue = exception