1 # Copyright (C) 2012 W. Trevor King <wking@tremily.us>
3 # This file is part of pyassuan.
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
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.
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/>.
17 """Assuan errors as defined in `libgpg-error`_.
19 The Assuan_ docs_ suggest these error codes.
21 .. _libgpg-error: http://www.gnupg.org/related_software/libgpg-error/
23 http://www.gnupg.org/documentation/manuals/assuan/Server-responses.html
24 .. _docs: http://www.gnupg.org/documentation/manuals/assuan/Error-codes.html
27 MESSAGE = { # extracted from libgpg-error-1.10/src/err-codes.h and gpg-error.h
31 3: 'Unknown version in packet',
32 4: 'Invalid public key algorithm',
33 5: 'Invalid digest algorithm',
40 12: 'Invalid cipher algorithm',
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',
56 28: 'Value not found',
59 31: 'Invalid passphrase',
60 32: 'Invalid signature class',
61 33: 'Resources exhausted',
62 34: 'Invalid keyring',
64 36: 'Bad certificate',
65 37: 'Invalid user ID',
66 38: 'Unexpected error',
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',
78 50: 'Selftest failed',
79 51: 'Data not encrypted',
80 52: 'Data not processed',
81 53: 'Unusable public key',
82 54: 'Unusable secret key',
84 56: 'Bad certificate chain',
85 57: 'Missing certificate',
89 61: 'Invalid operation code',
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',
101 73: 'Invalid handle',
102 74: 'Result truncated',
103 75: 'Incomplete line',
104 76: 'Invalid response',
105 77: 'No agent running',
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',
114 86: 'pinentry error',
118 90: 'Invalid parameter',
122 94: 'Certificate revoked',
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',
137 109: 'Card reset required',
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',
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',
159 131: 'Conditions of use not satisfied',
160 132: 'PINs are not synced',
164 136: 'Element not found',
165 137: 'Identifier not found',
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',
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',
190 162: 'Invalid CRL object',
191 163: 'Unsupported CRL version',
192 164: 'Invalid certificate object',
194 166: 'A locale function failed',
196 168: 'Protocol violation',
198 170: 'Invalid request',
199 171: 'Unknown extension',
200 172: 'Unknown critical extension',
202 174: 'Unknown option',
203 175: 'Unknown command',
204 176: 'Not operational',
205 177: 'No passphrase given',
208 180: 'No crypto engine',
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',
274 UNKNOWN = 'Unknown error code'
276 CODE = dict((message,code) for code,message in MESSAGE.items())
278 # TODO: system errors (GPG_ERR_E2BIG = GPG_ERR_SYSTEM_ERROR | 0, etc.)
280 class AssuanError (Exception):
283 >>> e = AssuanError(1)
286 >>> e = AssuanError(1024, 'testing!')
289 >>> e = AssuanError(message='Unknown packet')
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`')
297 message = MESSAGE[code]
299 code = CODE.get(message, UNKNOWN)
301 self.message = message
302 super(AssuanError, self).__init__('{} {}'.format(code, message))