aboutsummaryrefslogtreecommitdiff
path: root/thcolor/errors.py
blob: 5c6e0f468f361ab72f7530415b92ba7493a1f53b (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
#!/usr/bin/env python3
# *****************************************************************************
# Copyright (C) 2019-2022 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
# This file is part of the thcolor project, which is MIT-licensed.
# *****************************************************************************
"""Exception definitions."""

__all__ = ['ColorExpressionSyntaxError']


class ColorExpressionSyntaxError(Exception):
    """An error has occurred while decoding a color expression.

    Such an error can happen during parsing or evaluating.
    """

    def __init__(self, text, column=None, func=None):
        self._column = column if column is not None and column >= 0 else None
        self._func = str(func) if func else None
        self._text = str(text) if text else ''

    def __str__(self):
        msg = ''

        if self._column is not None:
            msg += f'at column {self._column}'
            if self._func is not None:
                msg += ', '
        if self._func is not None:
            msg += f'for function {self._func!r}'
        if msg:
            msg += ': '

        return msg + self._text

    @property
    def text(self):
        """Exception message, usually linked to the context."""

        return self._text

    @property
    def column(self):
        """Column of the expression at which the exception has occurred.

        ``None`` if the error has occurred on an unknown column or on
        the whole exception.
        """

        return self._column

    @property
    def func(self):
        """Name of the function we were calling when the error occurred.

        Either on arguments decoding or erroneous argument type or value.
        Is ``None`` if the context is unknown or the error hasn't
        occurred while calling a function or decoding its
        arguments.
        """

        return self._func


# End of file.