aboutsummaryrefslogtreecommitdiff
path: root/pyfingerd/errors.py
blob: 796325c962b31f22af4c8006f8ac7984726edf55 (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
#!/usr/bin/env python3
# *****************************************************************************
# Copyright (C) 2017-2022 Thomas Touhey <thomas@touhey.fr>
# This file is part of the pyfingerd project, which is MIT-licensed.
# *****************************************************************************
"""This file defines the exceptions used throughout the module."""

__all__ = [
    'BindError', 'ConfigurationError', 'HostnameError',
    'InvalidBindError', 'MalformedQueryError', 'NoBindsError',
]

# ---
# Configuration-related errors.
# ---


class ConfigurationError(Exception):
    """Raised when an invalid configuration option is set."""

    pass


class HostnameError(ConfigurationError):
    """Raised when a host name is invalid."""

    def __init__(self, hostname):
        super().__init__(f'Invalid host name {hostname!r}.')


class BindError(ConfigurationError):
    """Raised when an error has occurred with the provided binds."""

    def __init__(self, msg):
        self.original_message = msg
        super().__init__(
            f'An error has occurred with the provided binds: {msg}',
        )


class NoBindsError(BindError):
    """Raised when no binds were provided."""

    def __init__(self):
        super().__init__('No valid bind')


class InvalidBindError(BindError):
    """Raised when one of the provided binds came out erroneous."""

    def __init__(self, bind, msg=None):
        super().__init__(
            f'One of the provided bind ({bind!r}) '
            + f'was invalid{": " + msg if msg else ""}',
        )

        if msg is not None:
            self.original_message = msg


class MalformedQueryError(Exception):
    """Raised when a malformed query is received."""

    def __init__(self, query, msg=None):
        self.query = query
        self.msg = msg

        super().__init__(
            msg + (f': {query!r}' if query else '') + '.',
        )