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