summaryrefslogtreecommitdiff
path: root/pykwalify/errors.py
blob: c9f9b54c89167e9e1b5cdfdd4ddf6aa800b28f9c (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# -*- coding: utf-8 -*-

""" pyKwalify - errors.py """

# python stdlib
from pykwalify.compat import basestring

retcodes = {
    # PyKwalifyExit
    0: 'noerror',

    # UnknownError
    1: 'unknownerror',

    # SchemaError
    # e.g. when a rule or the core finds an error
    2: 'schemaerror',

    # CoreError
    # e.g. when the core finds an error that is not a SchemaError
    3: 'coreerror',

    # RuleError
    # e.g. when the rule class finds an error that is not a SchemaError, similar to CoreError
    4: 'ruleerror',

    # SchemaConflict
    # e.g. when a schema conflict occurs
    5: 'schemaconflict',

    # NotMappingError
    # e.g. when a value is not a mapping when it was expected it should be
    6: 'notmaperror',

    # NotSequenceError
    # e.g. when a value is not a sequence when it was expected it should be
    7: 'notsequenceerror',
}


retnames = dict((v, k) for (k, v) in retcodes.items())


class PyKwalifyException(RuntimeError):
    """
    """

    def __init__(self, msg=None, error_key=None, retcode=None, path=None):
        """
        Arguments:
        - `msg`: a string
        - `error_key`: a unique string that makes it easier to identify what error it is
        - `retcode`: an integer, defined in PyKwalify.errors.retcodes
        """
        self.msg = msg or ""
        self.retcode = retcode or retnames['unknownerror']
        self.retname = retcodes[retcode]
        self.error_key = error_key
        self.path = path or "/"

    def __str__(self):
        """
        """
        # <PyKwalifyException msg='foo bar' retcode=1>
        # kwargs = []
        # if self.msg:
        #        kwargs.append("msg='{0}'".format(self.msg))
        # if self.retcode != retnames['noerror']:
        #        kwargs.append("retcode=%d" % self.retcode)
        # if kwargs:
        #        kwargs.insert(0, '')
        # return "<{0}{1}>".format(self.__class__.__name__, ' '.join(kwargs))

        # <PyKwalifyException: error code 1: foo bar>
        kwargs = []
        if self.retcode != retnames['noerror']:
            kwargs.append("error code {0}".format(self.retcode))
        if self.msg:
            kwargs.append(self.msg)
        if kwargs:
            kwargs.insert(0, '')
        if self.path:
            kwargs.append("Path: '{0}'".format(self.path))
        return "<{0}{1}>".format(self.__class__.__name__, ': '.join(kwargs))

    def __repr__(self):
        """
        """
        kwargs = []
        if self.msg:
            kwargs.append("msg='{0}'".format(self.msg))
        return "{0}({1})".format(self.__class__.__name__, ', '.join(kwargs))

    def msg():
        doc = """ """

        def fget(self):
            return self._msg

        def fset(self, value):
            assert isinstance(value, basestring), "argument is not string"
            self._msg = value

        return locals()
    msg = property(**msg())

    def retcode():
        doc = """ """

        def fget(self):
            return self._retcode

        def fset(self, value):
            assert isinstance(value, int), "argument is not integer"
            self._retcode = value

        return locals()
    retcode = property(**retcode())

    def retname():
        doc = """ """

        def fget(self):
            return self._retname

        def fset(self, value):
            assert isinstance(value, str), "argument is not string"
            self._retname = value

        return locals()
    retname = property(**retname())


class UnknownError(PyKwalifyException):
    """
    """
    def __init__(self, *args, **kwargs):
        """
        """
        assert 'retcode' not in kwargs, "keyword retcode implicitly defined"
        super(self.__class__, self).__init__(
            retcode=retnames['unknownerror'],
            *args, **kwargs
        )


class SchemaError(PyKwalifyException):
    """
    """
    class SchemaErrorEntry(object):
        """
        """
        def __init__(self, msg, path, value, **kwargs):
            """
            """
            self.msg = msg
            self.path = path
            self.value = value
            for key, value in kwargs.items():
                self.__setattr__(key, value)

        def __repr__(self):
            return self.msg.format(**self.__dict__)

    def __init__(self, *args, **kwargs):
        """
        """
        assert "retcode" not in kwargs, "keyword retcode implicitly defined"
        super(self.__class__, self).__init__(
            retcode=retnames["schemaerror"],
            *args, **kwargs
        )


class CoreError(PyKwalifyException):
    """
    """
    def __init__(self, *args, **kwargs):
        """
        """
        assert "retcode" not in kwargs, "keyword retcode implicitly defined"
        super(self.__class__, self).__init__(
            retcode=retnames["coreerror"],
            *args, **kwargs
        )


class NotMappingError(PyKwalifyException):
    """
    """
    def __init__(self, *args, **kwargs):
        """
        """
        assert "retcode" not in kwargs, "keyword retcode implicitly defined"
        super(self.__class__, self).__init__(
            retcode=retnames['notmaperror'],
            *args, **kwargs
        )


class NotSequenceError(PyKwalifyException):
    """
    """
    def __init__(self, *args, **kwargs):
        """
        """
        assert "retcode" not in kwargs, "keyword retcode implicitly defined"
        super(self.__class__, self).__init__(
            retcode=retnames['notsequenceerror'],
            *args, **kwargs
        )


class RuleError(PyKwalifyException):
    """
    """
    def __init__(self, *args, **kwargs):
        """
        """
        assert "retcode" not in kwargs, "keyword retcode implicitly defined"
        super(self.__class__, self).__init__(
            retcode=retnames["ruleerror"],
            *args, **kwargs
        )


class SchemaConflict(PyKwalifyException):
    """
    """
    def __init__(self, *args, **kwargs):
        """
        """
        assert "retcode" not in kwargs, "keyword retcode implicitly defined"
        super(self.__class__, self).__init__(
            retcode=retnames["schemaconflict"],
            *args, **kwargs
        )