bin: add explicit --version options for Python 3.3.
[pyassuan.git] / pyassuan / error.py
1 # Copyright (C) 2012 W. Trevor King <wking@drexel.edu>
2 #
3 # This file is part of pyassuan.
4 #
5 # pyassuan is free software: you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation, either version 3 of the License, or (at your option) any later
8 # version.
9 #
10 # pyassuan is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # pyassuan.  If not, see <http://www.gnu.org/licenses/>.
16
17 """Assuan errors as defined in `libgpg-error`_.
18
19 The Assuan_ docs_ suggest these error codes.
20
21 .. _libgpg-error: http://www.gnupg.org/related_software/libgpg-error/
22 .. _Assuan:
23   http://www.gnupg.org/documentation/manuals/assuan/Server-responses.html
24 .. _docs: http://www.gnupg.org/documentation/manuals/assuan/Error-codes.html
25 """
26
27 MESSAGE = {  # extracted from libgpg-error-1.10/src/err-codes.h and gpg-error.h
28     0: 'Success',
29     1: 'General error',
30     2: 'Unknown packet',
31     3: 'Unknown version in packet',
32     4: 'Invalid public key algorithm',
33     5: 'Invalid digest algorithm',
34     6: 'Bad public key',
35     7: 'Bad secret key',
36     8: 'Bad signature',
37     9: 'No public key',
38     10: 'Checksum error',
39     11: 'Bad passphrase',
40     12: 'Invalid cipher algorithm',
41     13: 'Keyring open',
42     14: 'Invalid packet',
43     15: 'Invalid armor',
44     16: 'No user ID',
45     17: 'No secret key',
46     18: 'Wrong secret key used',
47     19: 'Bad session key',
48     20: 'Unknown compression algorithm',
49     21: 'Number is not prime',
50     22: 'Invalid encoding method',
51     23: 'Invalid encryption scheme',
52     24: 'Invalid signature scheme',
53     25: 'Invalid attribute',
54     26: 'No value',
55     27: 'Not found',
56     28: 'Value not found',
57     29: 'Syntax error',
58     30: 'Bad MPI value',
59     31: 'Invalid passphrase',
60     32: 'Invalid signature class',
61     33: 'Resources exhausted',
62     34: 'Invalid keyring',
63     35: 'Trust DB error',
64     36: 'Bad certificate',
65     37: 'Invalid user ID',
66     38: 'Unexpected error',
67     39: 'Time conflict',
68     40: 'Keyserver error',
69     41: 'Wrong public key algorithm',
70     42: 'Tribute to D. A.',
71     43: 'Weak encryption key',
72     44: 'Invalid key length',
73     45: 'Invalid argument',
74     46: 'Syntax error in URI',
75     47: 'Invalid URI',
76     48: 'Network error',
77     49: 'Unknown host',
78     50: 'Selftest failed',
79     51: 'Data not encrypted',
80     52: 'Data not processed',
81     53: 'Unusable public key',
82     54: 'Unusable secret key',
83     55: 'Invalid value',
84     56: 'Bad certificate chain',
85     57: 'Missing certificate',
86     58: 'No data',
87     59: 'Bug',
88     60: 'Not supported',
89     61: 'Invalid operation code',
90     62: 'Timeout',
91     63: 'Internal error',
92     64: 'EOF (gcrypt)',
93     65: 'Invalid object',
94     66: 'Provided object is too short',
95     67: 'Provided object is too large',
96     68: 'Missing item in object',
97     69: 'Not implemented',
98     70: 'Conflicting use',
99     71: 'Invalid cipher mode',
100     72: 'Invalid flag',
101     73: 'Invalid handle',
102     74: 'Result truncated',
103     75: 'Incomplete line',
104     76: 'Invalid response',
105     77: 'No agent running',
106     78: 'agent error',
107     79: 'Invalid data',
108     80: 'Unspecific Assuan server fault',
109     81: 'General Assuan error',
110     82: 'Invalid session key',
111     83: 'Invalid S-expression',
112     84: 'Unsupported algorithm',
113     85: 'No pinentry',
114     86: 'pinentry error',
115     87: 'Bad PIN',
116     88: 'Invalid name',
117     89: 'Bad data',
118     90: 'Invalid parameter',
119     91: 'Wrong card',
120     92: 'No dirmngr',
121     93: 'dirmngr error',
122     94: 'Certificate revoked',
123     95: 'No CRL known',
124     96: 'CRL too old',
125     97: 'Line too long',
126     98: 'Not trusted',
127     99: 'Operation cancelled',
128     100: 'Bad CA certificate',
129     101: 'Certificate expired',
130     102: 'Certificate too young',
131     103: 'Unsupported certificate',
132     104: 'Unknown S-expression',
133     105: 'Unsupported protection',
134     106: 'Corrupted protection',
135     107: 'Ambiguous name',
136     108: 'Card error',
137     109: 'Card reset required',
138     110: 'Card removed',
139     111: 'Invalid card',
140     112: 'Card not present',
141     113: 'No PKCS15 application',
142     114: 'Not confirmed',
143     115: 'Configuration error',
144     116: 'No policy match',
145     117: 'Invalid index',
146     118: 'Invalid ID',
147     119: 'No SmartCard daemon',
148     120: 'SmartCard daemon error',
149     121: 'Unsupported protocol',
150     122: 'Bad PIN method',
151     123: 'Card not initialized',
152     124: 'Unsupported operation',
153     125: 'Wrong key usage',
154     126: 'Nothing found',
155     127: 'Wrong blob type',
156     128: 'Missing value',
157     129: 'Hardware problem',
158     130: 'PIN blocked',
159     131: 'Conditions of use not satisfied',
160     132: 'PINs are not synced',
161     133: 'Invalid CRL',
162     134: 'BER error',
163     135: 'Invalid BER',
164     136: 'Element not found',
165     137: 'Identifier not found',
166     138: 'Invalid tag',
167     139: 'Invalid length',
168     140: 'Invalid key info',
169     141: 'Unexpected tag',
170     142: 'Not DER encoded',
171     143: 'No CMS object',
172     144: 'Invalid CMS object',
173     145: 'Unknown CMS object',
174     146: 'Unsupported CMS object',
175     147: 'Unsupported encoding',
176     148: 'Unsupported CMS version',
177     149: 'Unknown algorithm',
178     150: 'Invalid crypto engine',
179     151: 'Public key not trusted',
180     152: 'Decryption failed',
181     153: 'Key expired',
182     154: 'Signature expired',
183     155: 'Encoding problem',
184     156: 'Invalid state',
185     157: 'Duplicated value',
186     158: 'Missing action',
187     159: 'ASN.1 module not found',
188     160: 'Invalid OID string',
189     161: 'Invalid time',
190     162: 'Invalid CRL object',
191     163: 'Unsupported CRL version',
192     164: 'Invalid certificate object',
193     165: 'Unknown name',
194     166: 'A locale function failed',
195     167: 'Not locked',
196     168: 'Protocol violation',
197     169: 'Invalid MAC',
198     170: 'Invalid request',
199     171: 'Unknown extension',
200     172: 'Unknown critical extension',
201     173: 'Locked',
202     174: 'Unknown option',
203     175: 'Unknown command',
204     176: 'Not operational',
205     177: 'No passphrase given',
206     178: 'No PIN given',
207     179: 'Not enabled',
208     180: 'No crypto engine',
209     181: 'Missing key',
210     182: 'Too many objects',
211     183: 'Limit reached',
212     184: 'Not initialized',
213     185: 'Missing issuer certificate',
214     198: 'Operation fully cancelled',
215     199: 'Operation not yet finished',
216     200: 'Buffer too short',
217     201: 'Invalid length specifier in S-expression',
218     202: 'String too long in S-expression',
219     203: 'Unmatched parentheses in S-expression',
220     204: 'S-expression not canonical',
221     205: 'Bad character in S-expression',
222     206: 'Bad quotation in S-expression',
223     207: 'Zero prefix in S-expression',
224     208: 'Nested display hints in S-expression',
225     209: 'Unmatched display hints',
226     210: 'Unexpected reserved punctuation in S-expression',
227     211: 'Bad hexadecimal character in S-expression',
228     212: 'Odd hexadecimal numbers in S-expression',
229     213: 'Bad octal character in S-expression',
230     257: 'General IPC error',
231     258: 'IPC accept call failed',
232     259: 'IPC connect call failed',
233     260: 'Invalid IPC response',
234     261: 'Invalid value passed to IPC',
235     262: 'Incomplete line passed to IPC',
236     263: 'Line passed to IPC too long',
237     264: 'Nested IPC commands',
238     265: 'No data callback in IPC',
239     266: 'No inquire callback in IPC',
240     267: 'Not an IPC server',
241     268: 'Not an IPC client',
242     269: 'Problem starting IPC server',
243     270: 'IPC read error',
244     271: 'IPC write error',
245     273: 'Too much data for IPC layer',
246     274: 'Unexpected IPC command',
247     275: 'Unknown IPC command',
248     276: 'IPC syntax error',
249     277: 'IPC call has been cancelled',
250     278: 'No input source for IPC',
251     279: 'No output source for IPC',
252     280: 'IPC parameter error',
253     281: 'Unknown IPC inquire',
254     1024: 'User defined error code 1',
255     1025: 'User defined error code 2',
256     1026: 'User defined error code 3',
257     1027: 'User defined error code 4',
258     1028: 'User defined error code 5',
259     1029: 'User defined error code 6',
260     1030: 'User defined error code 7',
261     1031: 'User defined error code 8',
262     1032: 'User defined error code 9',
263     1033: 'User defined error code 10',
264     1034: 'User defined error code 11',
265     1035: 'User defined error code 12',
266     1036: 'User defined error code 13',
267     1037: 'User defined error code 14',
268     1038: 'User defined error code 15',
269     1039: 'User defined error code 16',
270     16381: 'System error w/o errno',
271     16382: 'Unknown system error',
272     16383: 'End of file',
273     }
274 UNKNOWN = 'Unknown error code'
275
276 CODE = dict((message,code) for code,message in MESSAGE.items())
277
278 # TODO: system errors (GPG_ERR_E2BIG = GPG_ERR_SYSTEM_ERROR | 0, etc.)
279
280 class AssuanError (Exception):
281     r"""
282
283     >>> e = AssuanError(1)
284     >>> print(e)
285     1 General error
286     >>> e = AssuanError(1024, 'testing!')
287     >>> print(e)
288     1024 testing!
289     >>> e = AssuanError(message='Unknown packet')
290     >>> print(e)
291     2 Unknown packet
292     """
293     def __init__(self, code=None, message=None):
294         if code is None and message is None:
295             raise ValueError('missing both `code` and `message`')
296         if message is None:
297             message = MESSAGE[code]
298         if code is None:
299             code = CODE.get(message, UNKNOWN)
300         self.code = code
301         self.message = message
302         super(AssuanError, self).__init__('{} {}'.format(code, message))