Statistics
| Branch: | Tag: | Revision:

root / lib / errors.py @ 8e5f43b1

History | View | Annotate | Download (10.7 kB)

1 2f31098c Iustin Pop
#
2 a8083063 Iustin Pop
#
3 a8083063 Iustin Pop
4 98dfcaff Iustin Pop
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012 Google Inc.
5 a8083063 Iustin Pop
#
6 a8083063 Iustin Pop
# This program is free software; you can redistribute it and/or modify
7 a8083063 Iustin Pop
# it under the terms of the GNU General Public License as published by
8 a8083063 Iustin Pop
# the Free Software Foundation; either version 2 of the License, or
9 a8083063 Iustin Pop
# (at your option) any later version.
10 a8083063 Iustin Pop
#
11 a8083063 Iustin Pop
# This program is distributed in the hope that it will be useful, but
12 a8083063 Iustin Pop
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 a8083063 Iustin Pop
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 a8083063 Iustin Pop
# General Public License for more details.
15 a8083063 Iustin Pop
#
16 a8083063 Iustin Pop
# You should have received a copy of the GNU General Public License
17 a8083063 Iustin Pop
# along with this program; if not, write to the Free Software
18 a8083063 Iustin Pop
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 a8083063 Iustin Pop
# 02110-1301, USA.
20 a8083063 Iustin Pop
21 a8083063 Iustin Pop
22 a8083063 Iustin Pop
"""Ganeti exception handling"""
23 a8083063 Iustin Pop
24 a8083063 Iustin Pop
25 5c983ee5 Iustin Pop
# OpPrereqError failure types
26 5c983ee5 Iustin Pop
27 104f4ca1 Iustin Pop
# resolver errors
28 104f4ca1 Iustin Pop
ECODE_RESOLVER = "resolver_error"
29 5c983ee5 Iustin Pop
# not enough resources (iallocator failure, disk space, memory, etc.)
30 5c983ee5 Iustin Pop
ECODE_NORES = "insufficient_resources"
31 5c983ee5 Iustin Pop
# wrong arguments (at syntax level)
32 5c983ee5 Iustin Pop
ECODE_INVAL = "wrong_input"
33 5c983ee5 Iustin Pop
# wrong entity state
34 5c983ee5 Iustin Pop
ECODE_STATE = "wrong_state"
35 5c983ee5 Iustin Pop
# entity not found
36 5c983ee5 Iustin Pop
ECODE_NOENT = "unknown_entity"
37 5c983ee5 Iustin Pop
# entity already exists
38 5c983ee5 Iustin Pop
ECODE_EXISTS = "already_exists"
39 5c983ee5 Iustin Pop
# resource not unique (e.g. MAC or IP duplication)
40 5c983ee5 Iustin Pop
ECODE_NOTUNIQUE = "resource_not_unique"
41 5c983ee5 Iustin Pop
# internal cluster error
42 5c983ee5 Iustin Pop
ECODE_FAULT = "internal_error"
43 5c983ee5 Iustin Pop
# environment error (e.g. node disk error)
44 5c983ee5 Iustin Pop
ECODE_ENVIRON = "environment_error"
45 5c983ee5 Iustin Pop
46 df156277 Michael Hanselmann
#: List of all failure types
47 df156277 Michael Hanselmann
ECODE_ALL = frozenset([
48 df156277 Michael Hanselmann
  ECODE_RESOLVER,
49 df156277 Michael Hanselmann
  ECODE_NORES,
50 df156277 Michael Hanselmann
  ECODE_INVAL,
51 df156277 Michael Hanselmann
  ECODE_STATE,
52 df156277 Michael Hanselmann
  ECODE_NOENT,
53 df156277 Michael Hanselmann
  ECODE_EXISTS,
54 df156277 Michael Hanselmann
  ECODE_NOTUNIQUE,
55 df156277 Michael Hanselmann
  ECODE_FAULT,
56 df156277 Michael Hanselmann
  ECODE_ENVIRON,
57 df156277 Michael Hanselmann
  ])
58 df156277 Michael Hanselmann
59 5c983ee5 Iustin Pop
60 a8083063 Iustin Pop
class GenericError(Exception):
61 a8083063 Iustin Pop
  """Base exception for Ganeti.
62 a8083063 Iustin Pop

63 a8083063 Iustin Pop
  """
64 a8083063 Iustin Pop
  pass
65 a8083063 Iustin Pop
66 a8083063 Iustin Pop
67 a8083063 Iustin Pop
class LVMError(GenericError):
68 a8083063 Iustin Pop
  """LVM-related exception.
69 a8083063 Iustin Pop

70 a8083063 Iustin Pop
  This exception codifies problems with LVM setup.
71 a8083063 Iustin Pop

72 a8083063 Iustin Pop
  """
73 a8083063 Iustin Pop
  pass
74 a8083063 Iustin Pop
75 a8083063 Iustin Pop
76 a8083063 Iustin Pop
class LockError(GenericError):
77 a8083063 Iustin Pop
  """Lock error exception.
78 a8083063 Iustin Pop

79 a8083063 Iustin Pop
  This signifies problems in the locking subsystem.
80 a8083063 Iustin Pop

81 a8083063 Iustin Pop
  """
82 a8083063 Iustin Pop
  pass
83 a8083063 Iustin Pop
84 a8083063 Iustin Pop
85 b6522276 Michael Hanselmann
class PidFileLockError(LockError):
86 b6522276 Michael Hanselmann
  """PID file is already locked by another process.
87 b6522276 Michael Hanselmann

88 b6522276 Michael Hanselmann
  """
89 b6522276 Michael Hanselmann
90 b6522276 Michael Hanselmann
91 a8083063 Iustin Pop
class HypervisorError(GenericError):
92 a8083063 Iustin Pop
  """Hypervisor-related exception.
93 a8083063 Iustin Pop

94 a8083063 Iustin Pop
  This is raised in case we can't communicate with the hypervisor
95 a8083063 Iustin Pop
  properly.
96 a8083063 Iustin Pop

97 a8083063 Iustin Pop
  """
98 a8083063 Iustin Pop
  pass
99 a8083063 Iustin Pop
100 a8083063 Iustin Pop
101 a8083063 Iustin Pop
class ProgrammerError(GenericError):
102 a8083063 Iustin Pop
  """Programming-related error.
103 a8083063 Iustin Pop

104 a8083063 Iustin Pop
  This is raised in cases we determine that the calling conventions
105 a8083063 Iustin Pop
  have been violated, meaning we got some desynchronisation between
106 a8083063 Iustin Pop
  parts of our code. It signifies a real programming bug.
107 a8083063 Iustin Pop

108 a8083063 Iustin Pop
  """
109 a8083063 Iustin Pop
  pass
110 a8083063 Iustin Pop
111 a8083063 Iustin Pop
112 a8083063 Iustin Pop
class BlockDeviceError(GenericError):
113 a8083063 Iustin Pop
  """Block-device related exception.
114 a8083063 Iustin Pop

115 a8083063 Iustin Pop
  This is raised in case we can't setup the instance's block devices
116 a8083063 Iustin Pop
  properly.
117 a8083063 Iustin Pop

118 a8083063 Iustin Pop
  """
119 a8083063 Iustin Pop
  pass
120 a8083063 Iustin Pop
121 a8083063 Iustin Pop
122 a8083063 Iustin Pop
class ConfigurationError(GenericError):
123 a8083063 Iustin Pop
  """Configuration related exception.
124 a8083063 Iustin Pop

125 a8083063 Iustin Pop
  Things like having an instance with a primary node that doesn't
126 a8083063 Iustin Pop
  exist in the config or such raise this exception.
127 a8083063 Iustin Pop

128 a8083063 Iustin Pop
  """
129 a8083063 Iustin Pop
  pass
130 a8083063 Iustin Pop
131 a8083063 Iustin Pop
132 4b63dc7a Iustin Pop
class ConfigVersionMismatch(ConfigurationError):
133 4b63dc7a Iustin Pop
  """Version mismatch in the configuration file.
134 4b63dc7a Iustin Pop

135 4b63dc7a Iustin Pop
  The error has two arguments: the expected and the actual found
136 4b63dc7a Iustin Pop
  version.
137 4b63dc7a Iustin Pop

138 4b63dc7a Iustin Pop
  """
139 4b63dc7a Iustin Pop
  pass
140 4b63dc7a Iustin Pop
141 4b63dc7a Iustin Pop
142 8e5f43b1 Dimitris Aragiorgis
class AddressPoolError(GenericError):
143 8e5f43b1 Dimitris Aragiorgis
  """Errors related to IP address pools.
144 8e5f43b1 Dimitris Aragiorgis

145 8e5f43b1 Dimitris Aragiorgis
  """
146 8e5f43b1 Dimitris Aragiorgis
147 8e5f43b1 Dimitris Aragiorgis
148 2c8a5690 Guido Trotter
class ReservationError(GenericError):
149 2c8a5690 Guido Trotter
  """Errors reserving a resource.
150 2c8a5690 Guido Trotter

151 2c8a5690 Guido Trotter
  """
152 2c8a5690 Guido Trotter
153 2c8a5690 Guido Trotter
154 a8083063 Iustin Pop
class RemoteError(GenericError):
155 a8083063 Iustin Pop
  """Programming-related error on remote call.
156 a8083063 Iustin Pop

157 a8083063 Iustin Pop
  This is raised when an unhandled error occurs in a call to a
158 a8083063 Iustin Pop
  remote node.  It usually signifies a real programming bug.
159 a8083063 Iustin Pop

160 a8083063 Iustin Pop
  """
161 a8083063 Iustin Pop
  pass
162 a8083063 Iustin Pop
163 a8083063 Iustin Pop
164 f4a2f532 Guido Trotter
class SignatureError(GenericError):
165 f4a2f532 Guido Trotter
  """Error authenticating a remote message.
166 f4a2f532 Guido Trotter

167 f4a2f532 Guido Trotter
  This is raised when the hmac signature on a message doesn't verify correctly
168 f4a2f532 Guido Trotter
  to the message itself. It can happen because of network unreliability or
169 f4a2f532 Guido Trotter
  because of spurious traffic.
170 f4a2f532 Guido Trotter

171 f4a2f532 Guido Trotter
  """
172 f4a2f532 Guido Trotter
  pass
173 f4a2f532 Guido Trotter
174 f4a2f532 Guido Trotter
175 a8083063 Iustin Pop
class ParameterError(GenericError):
176 a8083063 Iustin Pop
  """A passed parameter to a command is invalid.
177 a8083063 Iustin Pop

178 a8083063 Iustin Pop
  This is raised when the parameter passed to a request function is
179 a8083063 Iustin Pop
  invalid. Correct code should have verified this before passing the
180 a8083063 Iustin Pop
  request structure.
181 a8083063 Iustin Pop

182 a8083063 Iustin Pop
  The argument to this exception should be the parameter name.
183 a8083063 Iustin Pop

184 a8083063 Iustin Pop
  """
185 a8083063 Iustin Pop
  pass
186 a8083063 Iustin Pop
187 a8083063 Iustin Pop
188 a8083063 Iustin Pop
class OpPrereqError(GenericError):
189 a8083063 Iustin Pop
  """Prerequisites for the OpCode are not fulfilled.
190 a8083063 Iustin Pop

191 5c983ee5 Iustin Pop
  This exception will have either one or two arguments. For the
192 5c983ee5 Iustin Pop
  two-argument construction, the second argument should be one of the
193 5c983ee5 Iustin Pop
  ECODE_* codes.
194 5c983ee5 Iustin Pop

195 a8083063 Iustin Pop
  """
196 a8083063 Iustin Pop
197 098c0958 Michael Hanselmann
198 a8083063 Iustin Pop
class OpExecError(GenericError):
199 a8083063 Iustin Pop
  """Error during OpCode execution.
200 a8083063 Iustin Pop

201 a8083063 Iustin Pop
  """
202 a8083063 Iustin Pop
203 098c0958 Michael Hanselmann
204 1ce03fb1 Michael Hanselmann
class OpResultError(GenericError):
205 1ce03fb1 Michael Hanselmann
  """Issue with OpCode result.
206 1ce03fb1 Michael Hanselmann

207 1ce03fb1 Michael Hanselmann
  """
208 1ce03fb1 Michael Hanselmann
209 1ce03fb1 Michael Hanselmann
210 a8083063 Iustin Pop
class OpCodeUnknown(GenericError):
211 a8083063 Iustin Pop
  """Unknown opcode submitted.
212 a8083063 Iustin Pop

213 a8083063 Iustin Pop
  This signifies a mismatch between the definitions on the client and
214 a8083063 Iustin Pop
  server side.
215 a8083063 Iustin Pop

216 a8083063 Iustin Pop
  """
217 a8083063 Iustin Pop
218 098c0958 Michael Hanselmann
219 685ee993 Iustin Pop
class JobLost(GenericError):
220 685ee993 Iustin Pop
  """Submitted job lost.
221 685ee993 Iustin Pop

222 685ee993 Iustin Pop
  The job was submitted but it cannot be found in the current job
223 685ee993 Iustin Pop
  list.
224 685ee993 Iustin Pop

225 685ee993 Iustin Pop
  """
226 685ee993 Iustin Pop
227 685ee993 Iustin Pop
228 3d6c5566 Guido Trotter
class JobFileCorrupted(GenericError):
229 3d6c5566 Guido Trotter
  """Job file could not be properly decoded/restored.
230 3d6c5566 Guido Trotter

231 3d6c5566 Guido Trotter
  """
232 3d6c5566 Guido Trotter
233 3d6c5566 Guido Trotter
234 89e1fc26 Iustin Pop
class ResolverError(GenericError):
235 89e1fc26 Iustin Pop
  """Host name cannot be resolved.
236 89e1fc26 Iustin Pop

237 89e1fc26 Iustin Pop
  This is not a normal situation for Ganeti, as we rely on having a
238 89e1fc26 Iustin Pop
  working resolver.
239 89e1fc26 Iustin Pop

240 89e1fc26 Iustin Pop
  The non-resolvable hostname is available as the first element of the
241 89e1fc26 Iustin Pop
  args tuple; the other two elements of the tuple are the first two
242 89e1fc26 Iustin Pop
  args of the socket.gaierror exception (error code and description).
243 89e1fc26 Iustin Pop

244 89e1fc26 Iustin Pop
  """
245 89e1fc26 Iustin Pop
246 89e1fc26 Iustin Pop
247 a8083063 Iustin Pop
class HooksFailure(GenericError):
248 a8083063 Iustin Pop
  """A generic hook failure.
249 a8083063 Iustin Pop

250 a8083063 Iustin Pop
  This signifies usually a setup misconfiguration.
251 a8083063 Iustin Pop

252 a8083063 Iustin Pop
  """
253 a8083063 Iustin Pop
254 098c0958 Michael Hanselmann
255 a8083063 Iustin Pop
class HooksAbort(HooksFailure):
256 a8083063 Iustin Pop
  """A required hook has failed.
257 a8083063 Iustin Pop

258 a8083063 Iustin Pop
  This caused an abort of the operation in the initial phase. This
259 a8083063 Iustin Pop
  exception always has an attribute args which is a list of tuples of:
260 a8083063 Iustin Pop
    - node: the source node on which this hooks has failed
261 a8083063 Iustin Pop
    - script: the name of the script which aborted the run
262 a8083063 Iustin Pop

263 a8083063 Iustin Pop
  """
264 a8083063 Iustin Pop
265 098c0958 Michael Hanselmann
266 a8083063 Iustin Pop
class UnitParseError(GenericError):
267 a8083063 Iustin Pop
  """Unable to parse size unit.
268 a8083063 Iustin Pop

269 a8083063 Iustin Pop
  """
270 a8083063 Iustin Pop
271 ac2d0fe4 Michael Hanselmann
272 31155d60 Balazs Lecz
class ParseError(GenericError):
273 31155d60 Balazs Lecz
  """Generic parse error.
274 31155d60 Balazs Lecz

275 31155d60 Balazs Lecz
  Raised when unable to parse user input.
276 31155d60 Balazs Lecz

277 31155d60 Balazs Lecz
  """
278 31155d60 Balazs Lecz
279 31155d60 Balazs Lecz
280 a5728081 Guido Trotter
class TypeEnforcementError(GenericError):
281 a5728081 Guido Trotter
  """Unable to enforce data type.
282 a5728081 Guido Trotter

283 a5728081 Guido Trotter
  """
284 a8083063 Iustin Pop
285 ac2d0fe4 Michael Hanselmann
286 a8083063 Iustin Pop
class SshKeyError(GenericError):
287 a8083063 Iustin Pop
  """Invalid SSH key.
288 b0059682 Guido Trotter

289 a8083063 Iustin Pop
  """
290 5c947f38 Iustin Pop
291 5c947f38 Iustin Pop
292 b6267745 Andrea Spadaccini
class X509CertError(GenericError):
293 b6267745 Andrea Spadaccini
  """Invalid X509 certificate.
294 b6267745 Andrea Spadaccini

295 b6267745 Andrea Spadaccini
  This error has two arguments: the certificate filename and the error cause.
296 b6267745 Andrea Spadaccini

297 b6267745 Andrea Spadaccini
  """
298 b6267745 Andrea Spadaccini
299 b6267745 Andrea Spadaccini
300 5c947f38 Iustin Pop
class TagError(GenericError):
301 5c947f38 Iustin Pop
  """Generic tag error.
302 5c947f38 Iustin Pop

303 5c947f38 Iustin Pop
  The argument to this exception will show the exact error.
304 5c947f38 Iustin Pop

305 5c947f38 Iustin Pop
  """
306 7bca53e4 Michael Hanselmann
307 7bca53e4 Michael Hanselmann
308 7bca53e4 Michael Hanselmann
class CommandError(GenericError):
309 7bca53e4 Michael Hanselmann
  """External command error.
310 7bca53e4 Michael Hanselmann

311 7bca53e4 Michael Hanselmann
  """
312 e50bdd68 Guido Trotter
313 e50bdd68 Guido Trotter
314 ac2d0fe4 Michael Hanselmann
class StorageError(GenericError):
315 ac2d0fe4 Michael Hanselmann
  """Storage-related exception.
316 ac2d0fe4 Michael Hanselmann

317 ac2d0fe4 Michael Hanselmann
  """
318 ac2d0fe4 Michael Hanselmann
319 589dee9a Guido Trotter
320 589dee9a Guido Trotter
class InotifyError(GenericError):
321 589dee9a Guido Trotter
  """Error raised when there is a failure setting up an inotify watcher.
322 589dee9a Guido Trotter

323 589dee9a Guido Trotter
  """
324 589dee9a Guido Trotter
325 ac2d0fe4 Michael Hanselmann
326 e50bdd68 Guido Trotter
class QuitGanetiException(Exception):
327 7e975535 Stephen Shirley
  """Signal Ganeti that it must quit.
328 e50bdd68 Guido Trotter

329 0623d351 Iustin Pop
  This is not necessarily an error (and thus not a subclass of
330 0623d351 Iustin Pop
  GenericError), but it's an exceptional circumstance and it is thus
331 0623d351 Iustin Pop
  treated. This instance should be instantiated with two values. The
332 0623d351 Iustin Pop
  first one will specify the return code to the caller, and the second
333 0623d351 Iustin Pop
  one will be the returned result (either as an error or as a normal
334 0623d351 Iustin Pop
  result). Usually only the leave cluster rpc call should return
335 0623d351 Iustin Pop
  status True (as there it's expected we quit), every other call will
336 0623d351 Iustin Pop
  return status False (as a critical error was encountered).
337 e50bdd68 Guido Trotter

338 c41eea6e Iustin Pop
  Examples::
339 c41eea6e Iustin Pop

340 e50bdd68 Guido Trotter
    # Return a result of "True" to the caller, but quit ganeti afterwards
341 0623d351 Iustin Pop
    raise QuitGanetiException(True, None)
342 e50bdd68 Guido Trotter
    # Send an error to the caller, and quit ganeti
343 0623d351 Iustin Pop
    raise QuitGanetiException(False, "Fatal safety violation, shutting down")
344 e50bdd68 Guido Trotter

345 e50bdd68 Guido Trotter
  """
346 e50bdd68 Guido Trotter
347 f1da30e6 Michael Hanselmann
348 d11bda8d Iustin Pop
class JobQueueError(GenericError):
349 f1da30e6 Michael Hanselmann
  """Job queue error.
350 f1da30e6 Michael Hanselmann

351 f1da30e6 Michael Hanselmann
  """
352 6797ec29 Iustin Pop
353 6797ec29 Iustin Pop
354 686d7433 Iustin Pop
class JobQueueDrainError(JobQueueError):
355 686d7433 Iustin Pop
  """Job queue is marked for drain error.
356 686d7433 Iustin Pop

357 686d7433 Iustin Pop
  This is raised when a job submission attempt is made but the queue
358 686d7433 Iustin Pop
  is marked for drain.
359 686d7433 Iustin Pop

360 686d7433 Iustin Pop
  """
361 686d7433 Iustin Pop
362 686d7433 Iustin Pop
363 f87b405e Michael Hanselmann
class JobQueueFull(JobQueueError):
364 f87b405e Michael Hanselmann
  """Job queue full error.
365 f87b405e Michael Hanselmann

366 f87b405e Michael Hanselmann
  Raised when job queue size reached its hard limit.
367 f87b405e Michael Hanselmann

368 f87b405e Michael Hanselmann
  """
369 f87b405e Michael Hanselmann
370 f87b405e Michael Hanselmann
371 b125e3b3 Guido Trotter
class ConfdRequestError(GenericError):
372 b125e3b3 Guido Trotter
  """A request error in Ganeti confd.
373 b125e3b3 Guido Trotter

374 b125e3b3 Guido Trotter
  Events that should make confd abort the current request and proceed serving
375 b125e3b3 Guido Trotter
  different ones.
376 b125e3b3 Guido Trotter

377 b125e3b3 Guido Trotter
  """
378 b125e3b3 Guido Trotter
379 b125e3b3 Guido Trotter
380 9748ab35 Guido Trotter
class ConfdMagicError(GenericError):
381 9748ab35 Guido Trotter
  """A magic fourcc error in Ganeti confd.
382 9748ab35 Guido Trotter

383 9748ab35 Guido Trotter
  Errors processing the fourcc in ganeti confd datagrams.
384 9748ab35 Guido Trotter

385 9748ab35 Guido Trotter
  """
386 9748ab35 Guido Trotter
387 9748ab35 Guido Trotter
388 e4ccf6cd Guido Trotter
class ConfdClientError(GenericError):
389 e4ccf6cd Guido Trotter
  """A magic fourcc error in Ganeti confd.
390 e4ccf6cd Guido Trotter

391 e4ccf6cd Guido Trotter
  Errors in the confd client library.
392 e4ccf6cd Guido Trotter

393 e4ccf6cd Guido Trotter
  """
394 e4ccf6cd Guido Trotter
395 e4ccf6cd Guido Trotter
396 c8eded0b Guido Trotter
class UdpDataSizeError(GenericError):
397 c8eded0b Guido Trotter
  """UDP payload too big.
398 c8eded0b Guido Trotter

399 c8eded0b Guido Trotter
  """
400 c8eded0b Guido Trotter
401 c8eded0b Guido Trotter
402 4c32a8bd Luca Bigliardi
class NoCtypesError(GenericError):
403 4c32a8bd Luca Bigliardi
  """python ctypes module is not found in the system.
404 4c32a8bd Luca Bigliardi

405 4c32a8bd Luca Bigliardi
  """
406 4c32a8bd Luca Bigliardi
407 4c32a8bd Luca Bigliardi
408 8b312c1d Manuel Franceschini
class IPAddressError(GenericError):
409 8b312c1d Manuel Franceschini
  """Generic IP address error.
410 8b312c1d Manuel Franceschini

411 8b312c1d Manuel Franceschini
  """
412 8b312c1d Manuel Franceschini
413 8b312c1d Manuel Franceschini
414 7a8bda3f Michael Hanselmann
class LuxiError(GenericError):
415 7a8bda3f Michael Hanselmann
  """LUXI error.
416 7a8bda3f Michael Hanselmann

417 7a8bda3f Michael Hanselmann
  """
418 7a8bda3f Michael Hanselmann
419 7a8bda3f Michael Hanselmann
420 7578ab0a Michael Hanselmann
class QueryFilterParseError(ParseError):
421 7578ab0a Michael Hanselmann
  """Error while parsing query filter.
422 7578ab0a Michael Hanselmann

423 7578ab0a Michael Hanselmann
  """
424 7578ab0a Michael Hanselmann
  def GetDetails(self):
425 7578ab0a Michael Hanselmann
    """Returns a list of strings with details about the error.
426 7578ab0a Michael Hanselmann

427 7578ab0a Michael Hanselmann
    """
428 7578ab0a Michael Hanselmann
    try:
429 7578ab0a Michael Hanselmann
      (_, inner) = self.args
430 7578ab0a Michael Hanselmann
    except IndexError:
431 7578ab0a Michael Hanselmann
      return None
432 7578ab0a Michael Hanselmann
433 7578ab0a Michael Hanselmann
    return [str(inner.line),
434 7578ab0a Michael Hanselmann
            (" " * (inner.column - 1)) + "^",
435 7578ab0a Michael Hanselmann
            str(inner)]
436 7578ab0a Michael Hanselmann
437 7578ab0a Michael Hanselmann
438 352e1a26 Michael Hanselmann
class RapiTestResult(GenericError):
439 352e1a26 Michael Hanselmann
  """Exception containing results from RAPI test utilities.
440 352e1a26 Michael Hanselmann

441 352e1a26 Michael Hanselmann
  """
442 352e1a26 Michael Hanselmann
443 352e1a26 Michael Hanselmann
444 6797ec29 Iustin Pop
# errors should be added above
445 6797ec29 Iustin Pop
446 6797ec29 Iustin Pop
447 6797ec29 Iustin Pop
def GetErrorClass(name):
448 6797ec29 Iustin Pop
  """Return the class of an exception.
449 6797ec29 Iustin Pop

450 6797ec29 Iustin Pop
  Given the class name, return the class itself.
451 6797ec29 Iustin Pop

452 6797ec29 Iustin Pop
  @type name: str
453 6797ec29 Iustin Pop
  @param name: the exception name
454 6797ec29 Iustin Pop
  @rtype: class
455 6797ec29 Iustin Pop
  @return: the actual class, or None if not found
456 6797ec29 Iustin Pop

457 6797ec29 Iustin Pop
  """
458 6797ec29 Iustin Pop
  item = globals().get(name, None)
459 6797ec29 Iustin Pop
  if item is not None:
460 6797ec29 Iustin Pop
    if not (isinstance(item, type(Exception)) and
461 6797ec29 Iustin Pop
            issubclass(item, GenericError)):
462 6797ec29 Iustin Pop
      item = None
463 6797ec29 Iustin Pop
  return item
464 6956e9cd Iustin Pop
465 6956e9cd Iustin Pop
466 6956e9cd Iustin Pop
def EncodeException(err):
467 6956e9cd Iustin Pop
  """Encodes an exception into a format that L{MaybeRaise} will recognise.
468 6956e9cd Iustin Pop

469 6956e9cd Iustin Pop
  The passed L{err} argument will be formatted as a tuple (exception
470 6956e9cd Iustin Pop
  name, arguments) that the MaybeRaise function will recognise.
471 6956e9cd Iustin Pop

472 6956e9cd Iustin Pop
  @type err: GenericError child
473 6956e9cd Iustin Pop
  @param err: usually a child of GenericError (but any exception
474 6956e9cd Iustin Pop
      will be accepted)
475 6956e9cd Iustin Pop
  @rtype: tuple
476 6956e9cd Iustin Pop
  @return: tuple of (exception name, exception arguments)
477 6956e9cd Iustin Pop

478 6956e9cd Iustin Pop
  """
479 6956e9cd Iustin Pop
  return (err.__class__.__name__, err.args)
480 6956e9cd Iustin Pop
481 6956e9cd Iustin Pop
482 84f790e6 Michael Hanselmann
def GetEncodedError(result):
483 84f790e6 Michael Hanselmann
  """If this looks like an encoded Ganeti exception, return it.
484 6956e9cd Iustin Pop

485 6956e9cd Iustin Pop
  This function tries to parse the passed argument and if it looks
486 84f790e6 Michael Hanselmann
  like an encoding done by EncodeException, it will return the class
487 84f790e6 Michael Hanselmann
  object and arguments.
488 6956e9cd Iustin Pop

489 6956e9cd Iustin Pop
  """
490 6956e9cd Iustin Pop
  tlt = (tuple, list)
491 6956e9cd Iustin Pop
  if (isinstance(result, tlt) and len(result) == 2 and
492 6956e9cd Iustin Pop
      isinstance(result[1], tlt)):
493 6956e9cd Iustin Pop
    # custom ganeti errors
494 84f790e6 Michael Hanselmann
    errcls = GetErrorClass(result[0])
495 84f790e6 Michael Hanselmann
    if errcls:
496 84f790e6 Michael Hanselmann
      return (errcls, tuple(result[1]))
497 84f790e6 Michael Hanselmann
498 84f790e6 Michael Hanselmann
  return None
499 84f790e6 Michael Hanselmann
500 84f790e6 Michael Hanselmann
501 84f790e6 Michael Hanselmann
def MaybeRaise(result):
502 84f790e6 Michael Hanselmann
  """If this looks like an encoded Ganeti exception, raise it.
503 84f790e6 Michael Hanselmann

504 84f790e6 Michael Hanselmann
  This function tries to parse the passed argument and if it looks
505 84f790e6 Michael Hanselmann
  like an encoding done by EncodeException, it will re-raise it.
506 84f790e6 Michael Hanselmann

507 84f790e6 Michael Hanselmann
  """
508 84f790e6 Michael Hanselmann
  error = GetEncodedError(result)
509 84f790e6 Michael Hanselmann
  if error:
510 84f790e6 Michael Hanselmann
    (errcls, args) = error
511 98dfcaff Iustin Pop
    # pylint: disable=W0142
512 98dfcaff Iustin Pop
    raise errcls(*args)