Statistics
| Branch: | Tag: | Revision:

root / lib / errors.py @ 9c8c69bc

History | View | Annotate | Download (10.8 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 b8028dcf Michael Hanselmann
"""Ganeti exception handling.
23 b8028dcf Michael Hanselmann

24 b8028dcf Michael Hanselmann
"""
25 b8028dcf Michael Hanselmann
26 b8028dcf Michael Hanselmann
from ganeti import compat
27 a8083063 Iustin Pop
28 a8083063 Iustin Pop
29 5c983ee5 Iustin Pop
# OpPrereqError failure types
30 5c983ee5 Iustin Pop
31 402ff455 Michael Hanselmann
#: Resolver errors
32 104f4ca1 Iustin Pop
ECODE_RESOLVER = "resolver_error"
33 402ff455 Michael Hanselmann
34 402ff455 Michael Hanselmann
#: Not enough resources (iallocator failure, disk space, memory, etc.)
35 5c983ee5 Iustin Pop
ECODE_NORES = "insufficient_resources"
36 402ff455 Michael Hanselmann
37 518a45e5 Michael Hanselmann
#: Temporarily out of resources; operation can be tried again
38 e2dd6ece Michael Hanselmann
ECODE_TEMP_NORES = "temp_insufficient_resources"
39 518a45e5 Michael Hanselmann
40 402ff455 Michael Hanselmann
#: Wrong arguments (at syntax level)
41 5c983ee5 Iustin Pop
ECODE_INVAL = "wrong_input"
42 402ff455 Michael Hanselmann
43 402ff455 Michael Hanselmann
#: Wrong entity state
44 5c983ee5 Iustin Pop
ECODE_STATE = "wrong_state"
45 402ff455 Michael Hanselmann
46 402ff455 Michael Hanselmann
#: Entity not found
47 5c983ee5 Iustin Pop
ECODE_NOENT = "unknown_entity"
48 402ff455 Michael Hanselmann
49 402ff455 Michael Hanselmann
#: Entity already exists
50 5c983ee5 Iustin Pop
ECODE_EXISTS = "already_exists"
51 402ff455 Michael Hanselmann
52 402ff455 Michael Hanselmann
#: Resource not unique (e.g. MAC or IP duplication)
53 5c983ee5 Iustin Pop
ECODE_NOTUNIQUE = "resource_not_unique"
54 402ff455 Michael Hanselmann
55 402ff455 Michael Hanselmann
#: Internal cluster error
56 5c983ee5 Iustin Pop
ECODE_FAULT = "internal_error"
57 402ff455 Michael Hanselmann
58 402ff455 Michael Hanselmann
#: Environment error (e.g. node disk error)
59 5c983ee5 Iustin Pop
ECODE_ENVIRON = "environment_error"
60 5c983ee5 Iustin Pop
61 df156277 Michael Hanselmann
#: List of all failure types
62 b8028dcf Michael Hanselmann
ECODE_ALL = compat.UniqueFrozenset([
63 df156277 Michael Hanselmann
  ECODE_RESOLVER,
64 df156277 Michael Hanselmann
  ECODE_NORES,
65 518a45e5 Michael Hanselmann
  ECODE_TEMP_NORES,
66 df156277 Michael Hanselmann
  ECODE_INVAL,
67 df156277 Michael Hanselmann
  ECODE_STATE,
68 df156277 Michael Hanselmann
  ECODE_NOENT,
69 df156277 Michael Hanselmann
  ECODE_EXISTS,
70 df156277 Michael Hanselmann
  ECODE_NOTUNIQUE,
71 df156277 Michael Hanselmann
  ECODE_FAULT,
72 df156277 Michael Hanselmann
  ECODE_ENVIRON,
73 df156277 Michael Hanselmann
  ])
74 df156277 Michael Hanselmann
75 5c983ee5 Iustin Pop
76 a8083063 Iustin Pop
class GenericError(Exception):
77 a8083063 Iustin Pop
  """Base exception for Ganeti.
78 a8083063 Iustin Pop

79 a8083063 Iustin Pop
  """
80 a8083063 Iustin Pop
81 a8083063 Iustin Pop
82 a8083063 Iustin Pop
class LockError(GenericError):
83 a8083063 Iustin Pop
  """Lock error exception.
84 a8083063 Iustin Pop

85 a8083063 Iustin Pop
  This signifies problems in the locking subsystem.
86 a8083063 Iustin Pop

87 a8083063 Iustin Pop
  """
88 a8083063 Iustin Pop
89 a8083063 Iustin Pop
90 b6522276 Michael Hanselmann
class PidFileLockError(LockError):
91 b6522276 Michael Hanselmann
  """PID file is already locked by another process.
92 b6522276 Michael Hanselmann

93 b6522276 Michael Hanselmann
  """
94 b6522276 Michael Hanselmann
95 b6522276 Michael Hanselmann
96 a8083063 Iustin Pop
class HypervisorError(GenericError):
97 a8083063 Iustin Pop
  """Hypervisor-related exception.
98 a8083063 Iustin Pop

99 a8083063 Iustin Pop
  This is raised in case we can't communicate with the hypervisor
100 a8083063 Iustin Pop
  properly.
101 a8083063 Iustin Pop

102 a8083063 Iustin Pop
  """
103 a8083063 Iustin Pop
104 a8083063 Iustin Pop
105 a8083063 Iustin Pop
class ProgrammerError(GenericError):
106 a8083063 Iustin Pop
  """Programming-related error.
107 a8083063 Iustin Pop

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

112 a8083063 Iustin Pop
  """
113 a8083063 Iustin Pop
114 a8083063 Iustin Pop
115 a8083063 Iustin Pop
class BlockDeviceError(GenericError):
116 a8083063 Iustin Pop
  """Block-device related exception.
117 a8083063 Iustin Pop

118 a8083063 Iustin Pop
  This is raised in case we can't setup the instance's block devices
119 a8083063 Iustin Pop
  properly.
120 a8083063 Iustin Pop

121 a8083063 Iustin Pop
  """
122 a8083063 Iustin Pop
123 a8083063 Iustin Pop
124 a8083063 Iustin Pop
class ConfigurationError(GenericError):
125 a8083063 Iustin Pop
  """Configuration related exception.
126 a8083063 Iustin Pop

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

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

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

139 4b63dc7a Iustin Pop
  """
140 4b63dc7a Iustin Pop
141 4b63dc7a Iustin Pop
142 1de1cf25 Apollon Oikonomopoulos
class AddressPoolError(GenericError):
143 1de1cf25 Apollon Oikonomopoulos
  """Errors related to IP address pools.
144 1de1cf25 Apollon Oikonomopoulos

145 1de1cf25 Apollon Oikonomopoulos
  """
146 1de1cf25 Apollon Oikonomopoulos
147 1de1cf25 Apollon Oikonomopoulos
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
162 a8083063 Iustin Pop
163 f4a2f532 Guido Trotter
class SignatureError(GenericError):
164 f4a2f532 Guido Trotter
  """Error authenticating a remote message.
165 f4a2f532 Guido Trotter

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

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

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

180 a8083063 Iustin Pop
  The argument to this exception should be the parameter name.
181 a8083063 Iustin Pop

182 a8083063 Iustin Pop
  """
183 a8083063 Iustin Pop
184 a8083063 Iustin Pop
185 776b6291 René Nussbaumer
class ResultValidationError(GenericError):
186 776b6291 René Nussbaumer
  """The iallocation results fails validation.
187 776b6291 René Nussbaumer

188 776b6291 René Nussbaumer
  """
189 776b6291 René Nussbaumer
190 776b6291 René Nussbaumer
191 a8083063 Iustin Pop
class OpPrereqError(GenericError):
192 a8083063 Iustin Pop
  """Prerequisites for the OpCode are not fulfilled.
193 a8083063 Iustin Pop

194 86a24969 Dato Simó
  This exception has two arguments: an error message, and one of the
195 5c983ee5 Iustin Pop
  ECODE_* codes.
196 5c983ee5 Iustin Pop

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

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

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

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

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

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

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

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

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

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

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

252 a8083063 Iustin Pop
  This signifies usually a setup misconfiguration.
253 a8083063 Iustin Pop

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

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

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

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

277 31155d60 Balazs Lecz
  Raised when unable to parse user input.
278 31155d60 Balazs Lecz

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

285 a5728081 Guido Trotter
  """
286 a8083063 Iustin Pop
287 ac2d0fe4 Michael Hanselmann
288 b6267745 Andrea Spadaccini
class X509CertError(GenericError):
289 b6267745 Andrea Spadaccini
  """Invalid X509 certificate.
290 b6267745 Andrea Spadaccini

291 b6267745 Andrea Spadaccini
  This error has two arguments: the certificate filename and the error cause.
292 b6267745 Andrea Spadaccini

293 b6267745 Andrea Spadaccini
  """
294 b6267745 Andrea Spadaccini
295 b6267745 Andrea Spadaccini
296 5c947f38 Iustin Pop
class TagError(GenericError):
297 5c947f38 Iustin Pop
  """Generic tag error.
298 5c947f38 Iustin Pop

299 5c947f38 Iustin Pop
  The argument to this exception will show the exact error.
300 5c947f38 Iustin Pop

301 5c947f38 Iustin Pop
  """
302 7bca53e4 Michael Hanselmann
303 7bca53e4 Michael Hanselmann
304 7bca53e4 Michael Hanselmann
class CommandError(GenericError):
305 7bca53e4 Michael Hanselmann
  """External command error.
306 7bca53e4 Michael Hanselmann

307 7bca53e4 Michael Hanselmann
  """
308 e50bdd68 Guido Trotter
309 e50bdd68 Guido Trotter
310 ac2d0fe4 Michael Hanselmann
class StorageError(GenericError):
311 ac2d0fe4 Michael Hanselmann
  """Storage-related exception.
312 ac2d0fe4 Michael Hanselmann

313 ac2d0fe4 Michael Hanselmann
  """
314 ac2d0fe4 Michael Hanselmann
315 589dee9a Guido Trotter
316 589dee9a Guido Trotter
class InotifyError(GenericError):
317 589dee9a Guido Trotter
  """Error raised when there is a failure setting up an inotify watcher.
318 589dee9a Guido Trotter

319 589dee9a Guido Trotter
  """
320 589dee9a Guido Trotter
321 ac2d0fe4 Michael Hanselmann
322 e50bdd68 Guido Trotter
class QuitGanetiException(Exception):
323 7e975535 Stephen Shirley
  """Signal Ganeti that it must quit.
324 e50bdd68 Guido Trotter

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

334 c41eea6e Iustin Pop
  Examples::
335 c41eea6e Iustin Pop

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

341 e50bdd68 Guido Trotter
  """
342 e50bdd68 Guido Trotter
343 f1da30e6 Michael Hanselmann
344 d11bda8d Iustin Pop
class JobQueueError(GenericError):
345 f1da30e6 Michael Hanselmann
  """Job queue error.
346 f1da30e6 Michael Hanselmann

347 f1da30e6 Michael Hanselmann
  """
348 6797ec29 Iustin Pop
349 6797ec29 Iustin Pop
350 686d7433 Iustin Pop
class JobQueueDrainError(JobQueueError):
351 686d7433 Iustin Pop
  """Job queue is marked for drain error.
352 686d7433 Iustin Pop

353 686d7433 Iustin Pop
  This is raised when a job submission attempt is made but the queue
354 686d7433 Iustin Pop
  is marked for drain.
355 686d7433 Iustin Pop

356 686d7433 Iustin Pop
  """
357 686d7433 Iustin Pop
358 686d7433 Iustin Pop
359 f87b405e Michael Hanselmann
class JobQueueFull(JobQueueError):
360 f87b405e Michael Hanselmann
  """Job queue full error.
361 f87b405e Michael Hanselmann

362 f87b405e Michael Hanselmann
  Raised when job queue size reached its hard limit.
363 f87b405e Michael Hanselmann

364 f87b405e Michael Hanselmann
  """
365 f87b405e Michael Hanselmann
366 f87b405e Michael Hanselmann
367 9748ab35 Guido Trotter
class ConfdMagicError(GenericError):
368 9748ab35 Guido Trotter
  """A magic fourcc error in Ganeti confd.
369 9748ab35 Guido Trotter

370 9748ab35 Guido Trotter
  Errors processing the fourcc in ganeti confd datagrams.
371 9748ab35 Guido Trotter

372 9748ab35 Guido Trotter
  """
373 9748ab35 Guido Trotter
374 9748ab35 Guido Trotter
375 e4ccf6cd Guido Trotter
class ConfdClientError(GenericError):
376 e4ccf6cd Guido Trotter
  """A magic fourcc error in Ganeti confd.
377 e4ccf6cd Guido Trotter

378 e4ccf6cd Guido Trotter
  Errors in the confd client library.
379 e4ccf6cd Guido Trotter

380 e4ccf6cd Guido Trotter
  """
381 e4ccf6cd Guido Trotter
382 e4ccf6cd Guido Trotter
383 c8eded0b Guido Trotter
class UdpDataSizeError(GenericError):
384 c8eded0b Guido Trotter
  """UDP payload too big.
385 c8eded0b Guido Trotter

386 c8eded0b Guido Trotter
  """
387 c8eded0b Guido Trotter
388 c8eded0b Guido Trotter
389 4c32a8bd Luca Bigliardi
class NoCtypesError(GenericError):
390 4c32a8bd Luca Bigliardi
  """python ctypes module is not found in the system.
391 4c32a8bd Luca Bigliardi

392 4c32a8bd Luca Bigliardi
  """
393 4c32a8bd Luca Bigliardi
394 4c32a8bd Luca Bigliardi
395 8b312c1d Manuel Franceschini
class IPAddressError(GenericError):
396 8b312c1d Manuel Franceschini
  """Generic IP address error.
397 8b312c1d Manuel Franceschini

398 8b312c1d Manuel Franceschini
  """
399 8b312c1d Manuel Franceschini
400 8b312c1d Manuel Franceschini
401 7a8bda3f Michael Hanselmann
class LuxiError(GenericError):
402 7a8bda3f Michael Hanselmann
  """LUXI error.
403 7a8bda3f Michael Hanselmann

404 7a8bda3f Michael Hanselmann
  """
405 7a8bda3f Michael Hanselmann
406 7a8bda3f Michael Hanselmann
407 7578ab0a Michael Hanselmann
class QueryFilterParseError(ParseError):
408 7578ab0a Michael Hanselmann
  """Error while parsing query filter.
409 7578ab0a Michael Hanselmann

410 300e5450 Michael Hanselmann
  This exception must be instantiated with two values. The first one is a
411 300e5450 Michael Hanselmann
  string with an error description, the second one is an instance of a subclass
412 300e5450 Michael Hanselmann
  of C{pyparsing.ParseBaseException} (used to display the exact error
413 300e5450 Michael Hanselmann
  location).
414 300e5450 Michael Hanselmann

415 7578ab0a Michael Hanselmann
  """
416 7578ab0a Michael Hanselmann
  def GetDetails(self):
417 7578ab0a Michael Hanselmann
    """Returns a list of strings with details about the error.
418 7578ab0a Michael Hanselmann

419 7578ab0a Michael Hanselmann
    """
420 7578ab0a Michael Hanselmann
    try:
421 7578ab0a Michael Hanselmann
      (_, inner) = self.args
422 7578ab0a Michael Hanselmann
    except IndexError:
423 7578ab0a Michael Hanselmann
      return None
424 7578ab0a Michael Hanselmann
425 7578ab0a Michael Hanselmann
    return [str(inner.line),
426 7578ab0a Michael Hanselmann
            (" " * (inner.column - 1)) + "^",
427 7578ab0a Michael Hanselmann
            str(inner)]
428 7578ab0a Michael Hanselmann
429 7578ab0a Michael Hanselmann
430 352e1a26 Michael Hanselmann
class RapiTestResult(GenericError):
431 352e1a26 Michael Hanselmann
  """Exception containing results from RAPI test utilities.
432 352e1a26 Michael Hanselmann

433 352e1a26 Michael Hanselmann
  """
434 352e1a26 Michael Hanselmann
435 352e1a26 Michael Hanselmann
436 fbdac0d9 Michael Hanselmann
class FileStoragePathError(GenericError):
437 fbdac0d9 Michael Hanselmann
  """Error from file storage path validation.
438 fbdac0d9 Michael Hanselmann

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

448 6797ec29 Iustin Pop
  Given the class name, return the class itself.
449 6797ec29 Iustin Pop

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

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

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

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

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

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

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

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

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