1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 """Common DNS Exceptions.
19
20 Dnspython modules may also define their own exceptions, which will
21 always be subclasses of ``DNSException``.
22 """
23
25 """Abstract base class shared by all dnspython exceptions.
26
27 It supports two basic modes of operation:
28
29 a) Old/compatible mode is used if ``__init__`` was called with
30 empty *kwargs*. In compatible mode all *args* are passed
31 to the standard Python Exception class as before and all *args* are
32 printed by the standard ``__str__`` implementation. Class variable
33 ``msg`` (or doc string if ``msg`` is ``None``) is returned from ``str()``
34 if *args* is empty.
35
36 b) New/parametrized mode is used if ``__init__`` was called with
37 non-empty *kwargs*.
38 In the new mode *args* must be empty and all kwargs must match
39 those set in class variable ``supp_kwargs``. All kwargs are stored inside
40 ``self.kwargs`` and used in a new ``__str__`` implementation to construct
41 a formatted message based on the ``fmt`` class variable, a ``string``.
42
43 In the simplest case it is enough to override the ``supp_kwargs``
44 and ``fmt`` class variables to get nice parametrized messages.
45 """
46
47 msg = None
48 supp_kwargs = set()
49 fmt = None
50
65
67 """Old exceptions supported only args and not kwargs.
68
69 For sanity we do not allow to mix old and new behavior."""
70 if args or kwargs:
71 assert bool(args) != bool(kwargs), \
72 'keyword arguments are mutually exclusive with positional args'
73
75 if kwargs:
76 assert set(kwargs.keys()) == self.supp_kwargs, \
77 'following set of keyword args is required: %s' % (
78 self.supp_kwargs)
79 return kwargs
80
82 """Format kwargs before printing them.
83
84 Resulting dictionary has to have keys necessary for str.format call
85 on fmt class variable.
86 """
87 fmtargs = {}
88 for kw, data in kwargs.items():
89 if isinstance(data, (list, set)):
90
91 fmtargs[kw] = list(map(str, data))
92 if len(fmtargs[kw]) == 1:
93
94 fmtargs[kw] = fmtargs[kw].pop()
95 else:
96 fmtargs[kw] = data
97 return fmtargs
98
100 if self.kwargs and self.fmt:
101
102 fmtargs = self._fmt_kwargs(**self.kwargs)
103 return self.fmt.format(**fmtargs)
104 else:
105
106 return super(DNSException, self).__str__()
107
108
111
112
114 """Text input is malformed."""
115
116
118 """Text input ended unexpectedly."""
119
120
122 """The DNS message is too big."""
123
124
126 """The DNS operation timed out."""
127 supp_kwargs = {'timeout'}
128 fmt = "The DNS operation timed out after {timeout} seconds"
129