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