Statistics
| Branch: | Tag: | Revision:

root / lib / errors.py @ bc57fa8d

History | View | Annotate | Download (10.9 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 88ac4075 Jose A. Lopes
from ganeti import constants
27 88ac4075 Jose A. Lopes
28 88ac4075 Jose A. Lopes
29 88ac4075 Jose A. Lopes
ECODE_RESOLVER = constants.ERRORS_ECODE_RESOLVER
30 88ac4075 Jose A. Lopes
ECODE_NORES = constants.ERRORS_ECODE_NORES
31 88ac4075 Jose A. Lopes
ECODE_TEMP_NORES = constants.ERRORS_ECODE_TEMP_NORES
32 88ac4075 Jose A. Lopes
ECODE_INVAL = constants.ERRORS_ECODE_INVAL
33 88ac4075 Jose A. Lopes
ECODE_STATE = constants.ERRORS_ECODE_STATE
34 88ac4075 Jose A. Lopes
ECODE_NOENT = constants.ERRORS_ECODE_NOENT
35 88ac4075 Jose A. Lopes
ECODE_EXISTS = constants.ERRORS_ECODE_EXISTS
36 88ac4075 Jose A. Lopes
ECODE_NOTUNIQUE = constants.ERRORS_ECODE_NOTUNIQUE
37 88ac4075 Jose A. Lopes
ECODE_FAULT = constants.ERRORS_ECODE_FAULT
38 88ac4075 Jose A. Lopes
ECODE_ENVIRON = constants.ERRORS_ECODE_ENVIRON
39 88ac4075 Jose A. Lopes
ECODE_ALL = constants.ERRORS_ECODE_ALL
40 df156277 Michael Hanselmann
41 5c983ee5 Iustin Pop
42 a8083063 Iustin Pop
class GenericError(Exception):
43 a8083063 Iustin Pop
  """Base exception for Ganeti.
44 a8083063 Iustin Pop

45 a8083063 Iustin Pop
  """
46 a8083063 Iustin Pop
47 a8083063 Iustin Pop
48 a8083063 Iustin Pop
class LockError(GenericError):
49 a8083063 Iustin Pop
  """Lock error exception.
50 a8083063 Iustin Pop

51 a8083063 Iustin Pop
  This signifies problems in the locking subsystem.
52 a8083063 Iustin Pop

53 a8083063 Iustin Pop
  """
54 a8083063 Iustin Pop
55 a8083063 Iustin Pop
56 b6522276 Michael Hanselmann
class PidFileLockError(LockError):
57 b6522276 Michael Hanselmann
  """PID file is already locked by another process.
58 b6522276 Michael Hanselmann

59 b6522276 Michael Hanselmann
  """
60 b6522276 Michael Hanselmann
61 b6522276 Michael Hanselmann
62 a8083063 Iustin Pop
class HypervisorError(GenericError):
63 a8083063 Iustin Pop
  """Hypervisor-related exception.
64 a8083063 Iustin Pop

65 a8083063 Iustin Pop
  This is raised in case we can't communicate with the hypervisor
66 a8083063 Iustin Pop
  properly.
67 a8083063 Iustin Pop

68 a8083063 Iustin Pop
  """
69 a8083063 Iustin Pop
70 a8083063 Iustin Pop
71 0fe22ad2 Dimitris Aragiorgis
class HotplugError(HypervisorError):
72 0fe22ad2 Dimitris Aragiorgis
  """Hotplug-related exception.
73 0fe22ad2 Dimitris Aragiorgis

74 0fe22ad2 Dimitris Aragiorgis
  This is raised in case a hotplug action fails or is not supported.
75 0fe22ad2 Dimitris Aragiorgis
  It is currently used only by KVM hypervisor.
76 0fe22ad2 Dimitris Aragiorgis

77 0fe22ad2 Dimitris Aragiorgis
  """
78 0fe22ad2 Dimitris Aragiorgis
79 0fe22ad2 Dimitris Aragiorgis
80 a8083063 Iustin Pop
class ProgrammerError(GenericError):
81 a8083063 Iustin Pop
  """Programming-related error.
82 a8083063 Iustin Pop

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

87 a8083063 Iustin Pop
  """
88 a8083063 Iustin Pop
89 a8083063 Iustin Pop
90 a8083063 Iustin Pop
class BlockDeviceError(GenericError):
91 a8083063 Iustin Pop
  """Block-device related exception.
92 a8083063 Iustin Pop

93 a8083063 Iustin Pop
  This is raised in case we can't setup the instance's block devices
94 a8083063 Iustin Pop
  properly.
95 a8083063 Iustin Pop

96 a8083063 Iustin Pop
  """
97 a8083063 Iustin Pop
98 a8083063 Iustin Pop
99 a8083063 Iustin Pop
class ConfigurationError(GenericError):
100 a8083063 Iustin Pop
  """Configuration related exception.
101 a8083063 Iustin Pop

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

105 a8083063 Iustin Pop
  """
106 a8083063 Iustin Pop
107 a8083063 Iustin Pop
108 4b63dc7a Iustin Pop
class ConfigVersionMismatch(ConfigurationError):
109 4b63dc7a Iustin Pop
  """Version mismatch in the configuration file.
110 4b63dc7a Iustin Pop

111 4b63dc7a Iustin Pop
  The error has two arguments: the expected and the actual found
112 4b63dc7a Iustin Pop
  version.
113 4b63dc7a Iustin Pop

114 4b63dc7a Iustin Pop
  """
115 4b63dc7a Iustin Pop
116 4b63dc7a Iustin Pop
117 1de1cf25 Apollon Oikonomopoulos
class AddressPoolError(GenericError):
118 1de1cf25 Apollon Oikonomopoulos
  """Errors related to IP address pools.
119 1de1cf25 Apollon Oikonomopoulos

120 1de1cf25 Apollon Oikonomopoulos
  """
121 1de1cf25 Apollon Oikonomopoulos
122 1de1cf25 Apollon Oikonomopoulos
123 2c8a5690 Guido Trotter
class ReservationError(GenericError):
124 2c8a5690 Guido Trotter
  """Errors reserving a resource.
125 2c8a5690 Guido Trotter

126 2c8a5690 Guido Trotter
  """
127 2c8a5690 Guido Trotter
128 2c8a5690 Guido Trotter
129 a8083063 Iustin Pop
class RemoteError(GenericError):
130 a8083063 Iustin Pop
  """Programming-related error on remote call.
131 a8083063 Iustin Pop

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

135 a8083063 Iustin Pop
  """
136 a8083063 Iustin Pop
137 a8083063 Iustin Pop
138 f4a2f532 Guido Trotter
class SignatureError(GenericError):
139 f4a2f532 Guido Trotter
  """Error authenticating a remote message.
140 f4a2f532 Guido Trotter

141 f4a2f532 Guido Trotter
  This is raised when the hmac signature on a message doesn't verify correctly
142 f4a2f532 Guido Trotter
  to the message itself. It can happen because of network unreliability or
143 f4a2f532 Guido Trotter
  because of spurious traffic.
144 f4a2f532 Guido Trotter

145 f4a2f532 Guido Trotter
  """
146 f4a2f532 Guido Trotter
147 f4a2f532 Guido Trotter
148 a8083063 Iustin Pop
class ParameterError(GenericError):
149 a8083063 Iustin Pop
  """A passed parameter to a command is invalid.
150 a8083063 Iustin Pop

151 a8083063 Iustin Pop
  This is raised when the parameter passed to a request function is
152 a8083063 Iustin Pop
  invalid. Correct code should have verified this before passing the
153 a8083063 Iustin Pop
  request structure.
154 a8083063 Iustin Pop

155 a8083063 Iustin Pop
  The argument to this exception should be the parameter name.
156 a8083063 Iustin Pop

157 a8083063 Iustin Pop
  """
158 a8083063 Iustin Pop
159 a8083063 Iustin Pop
160 776b6291 Renรฉ Nussbaumer
class ResultValidationError(GenericError):
161 776b6291 Renรฉ Nussbaumer
  """The iallocation results fails validation.
162 776b6291 Renรฉ Nussbaumer

163 776b6291 Renรฉ Nussbaumer
  """
164 776b6291 Renรฉ Nussbaumer
165 776b6291 Renรฉ Nussbaumer
166 a8083063 Iustin Pop
class OpPrereqError(GenericError):
167 a8083063 Iustin Pop
  """Prerequisites for the OpCode are not fulfilled.
168 a8083063 Iustin Pop

169 86a24969 Dato Simรณ
  This exception has two arguments: an error message, and one of the
170 5c983ee5 Iustin Pop
  ECODE_* codes.
171 5c983ee5 Iustin Pop

172 a8083063 Iustin Pop
  """
173 a8083063 Iustin Pop
174 098c0958 Michael Hanselmann
175 a8083063 Iustin Pop
class OpExecError(GenericError):
176 a8083063 Iustin Pop
  """Error during OpCode execution.
177 a8083063 Iustin Pop

178 a8083063 Iustin Pop
  """
179 a8083063 Iustin Pop
180 098c0958 Michael Hanselmann
181 1ce03fb1 Michael Hanselmann
class OpResultError(GenericError):
182 1ce03fb1 Michael Hanselmann
  """Issue with OpCode result.
183 1ce03fb1 Michael Hanselmann

184 1ce03fb1 Michael Hanselmann
  """
185 1ce03fb1 Michael Hanselmann
186 1ce03fb1 Michael Hanselmann
187 9b221ea4 Michele Tartara
class DeviceCreationError(GenericError):
188 9b221ea4 Michele Tartara
  """Error during the creation of a device.
189 9b221ea4 Michele Tartara

190 9b221ea4 Michele Tartara
  This exception should contain the list of the devices actually created
191 9b221ea4 Michele Tartara
  up to now, in the form of pairs (node, device)
192 9b221ea4 Michele Tartara

193 9b221ea4 Michele Tartara
  """
194 9b221ea4 Michele Tartara
  def __init__(self, message, created_devices):
195 9b221ea4 Michele Tartara
    GenericError.__init__(self)
196 9b221ea4 Michele Tartara
    self.message = message
197 9b221ea4 Michele Tartara
    self.created_devices = created_devices
198 9b221ea4 Michele Tartara
199 9b221ea4 Michele Tartara
  def __str__(self):
200 9b221ea4 Michele Tartara
    return self.message
201 9b221ea4 Michele Tartara
202 9b221ea4 Michele Tartara
203 a8083063 Iustin Pop
class OpCodeUnknown(GenericError):
204 a8083063 Iustin Pop
  """Unknown opcode submitted.
205 a8083063 Iustin Pop

206 a8083063 Iustin Pop
  This signifies a mismatch between the definitions on the client and
207 a8083063 Iustin Pop
  server side.
208 a8083063 Iustin Pop

209 a8083063 Iustin Pop
  """
210 a8083063 Iustin Pop
211 098c0958 Michael Hanselmann
212 685ee993 Iustin Pop
class JobLost(GenericError):
213 685ee993 Iustin Pop
  """Submitted job lost.
214 685ee993 Iustin Pop

215 685ee993 Iustin Pop
  The job was submitted but it cannot be found in the current job
216 685ee993 Iustin Pop
  list.
217 685ee993 Iustin Pop

218 685ee993 Iustin Pop
  """
219 685ee993 Iustin Pop
220 685ee993 Iustin Pop
221 3d6c5566 Guido Trotter
class JobFileCorrupted(GenericError):
222 3d6c5566 Guido Trotter
  """Job file could not be properly decoded/restored.
223 3d6c5566 Guido Trotter

224 3d6c5566 Guido Trotter
  """
225 3d6c5566 Guido Trotter
226 3d6c5566 Guido Trotter
227 89e1fc26 Iustin Pop
class ResolverError(GenericError):
228 89e1fc26 Iustin Pop
  """Host name cannot be resolved.
229 89e1fc26 Iustin Pop

230 89e1fc26 Iustin Pop
  This is not a normal situation for Ganeti, as we rely on having a
231 89e1fc26 Iustin Pop
  working resolver.
232 89e1fc26 Iustin Pop

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

237 89e1fc26 Iustin Pop
  """
238 89e1fc26 Iustin Pop
239 89e1fc26 Iustin Pop
240 a8083063 Iustin Pop
class HooksFailure(GenericError):
241 a8083063 Iustin Pop
  """A generic hook failure.
242 a8083063 Iustin Pop

243 a8083063 Iustin Pop
  This signifies usually a setup misconfiguration.
244 a8083063 Iustin Pop

245 a8083063 Iustin Pop
  """
246 a8083063 Iustin Pop
247 098c0958 Michael Hanselmann
248 a8083063 Iustin Pop
class HooksAbort(HooksFailure):
249 a8083063 Iustin Pop
  """A required hook has failed.
250 a8083063 Iustin Pop

251 a8083063 Iustin Pop
  This caused an abort of the operation in the initial phase. This
252 a8083063 Iustin Pop
  exception always has an attribute args which is a list of tuples of:
253 a8083063 Iustin Pop
    - node: the source node on which this hooks has failed
254 a8083063 Iustin Pop
    - script: the name of the script which aborted the run
255 a8083063 Iustin Pop

256 a8083063 Iustin Pop
  """
257 a8083063 Iustin Pop
258 098c0958 Michael Hanselmann
259 a8083063 Iustin Pop
class UnitParseError(GenericError):
260 a8083063 Iustin Pop
  """Unable to parse size unit.
261 a8083063 Iustin Pop

262 a8083063 Iustin Pop
  """
263 a8083063 Iustin Pop
264 ac2d0fe4 Michael Hanselmann
265 31155d60 Balazs Lecz
class ParseError(GenericError):
266 31155d60 Balazs Lecz
  """Generic parse error.
267 31155d60 Balazs Lecz

268 31155d60 Balazs Lecz
  Raised when unable to parse user input.
269 31155d60 Balazs Lecz

270 31155d60 Balazs Lecz
  """
271 31155d60 Balazs Lecz
272 31155d60 Balazs Lecz
273 a5728081 Guido Trotter
class TypeEnforcementError(GenericError):
274 a5728081 Guido Trotter
  """Unable to enforce data type.
275 a5728081 Guido Trotter

276 a5728081 Guido Trotter
  """
277 a8083063 Iustin Pop
278 ac2d0fe4 Michael Hanselmann
279 b6267745 Andrea Spadaccini
class X509CertError(GenericError):
280 b6267745 Andrea Spadaccini
  """Invalid X509 certificate.
281 b6267745 Andrea Spadaccini

282 b6267745 Andrea Spadaccini
  This error has two arguments: the certificate filename and the error cause.
283 b6267745 Andrea Spadaccini

284 b6267745 Andrea Spadaccini
  """
285 b6267745 Andrea Spadaccini
286 b6267745 Andrea Spadaccini
287 5c947f38 Iustin Pop
class TagError(GenericError):
288 5c947f38 Iustin Pop
  """Generic tag error.
289 5c947f38 Iustin Pop

290 5c947f38 Iustin Pop
  The argument to this exception will show the exact error.
291 5c947f38 Iustin Pop

292 5c947f38 Iustin Pop
  """
293 7bca53e4 Michael Hanselmann
294 7bca53e4 Michael Hanselmann
295 7bca53e4 Michael Hanselmann
class CommandError(GenericError):
296 7bca53e4 Michael Hanselmann
  """External command error.
297 7bca53e4 Michael Hanselmann

298 7bca53e4 Michael Hanselmann
  """
299 e50bdd68 Guido Trotter
300 e50bdd68 Guido Trotter
301 ac2d0fe4 Michael Hanselmann
class StorageError(GenericError):
302 ac2d0fe4 Michael Hanselmann
  """Storage-related exception.
303 ac2d0fe4 Michael Hanselmann

304 ac2d0fe4 Michael Hanselmann
  """
305 ac2d0fe4 Michael Hanselmann
306 589dee9a Guido Trotter
307 589dee9a Guido Trotter
class InotifyError(GenericError):
308 589dee9a Guido Trotter
  """Error raised when there is a failure setting up an inotify watcher.
309 589dee9a Guido Trotter

310 589dee9a Guido Trotter
  """
311 589dee9a Guido Trotter
312 ac2d0fe4 Michael Hanselmann
313 e50bdd68 Guido Trotter
class QuitGanetiException(Exception):
314 7e975535 Stephen Shirley
  """Signal Ganeti that it must quit.
315 e50bdd68 Guido Trotter

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

325 c41eea6e Iustin Pop
  Examples::
326 c41eea6e Iustin Pop

327 e50bdd68 Guido Trotter
    # Return a result of "True" to the caller, but quit ganeti afterwards
328 0623d351 Iustin Pop
    raise QuitGanetiException(True, None)
329 e50bdd68 Guido Trotter
    # Send an error to the caller, and quit ganeti
330 0623d351 Iustin Pop
    raise QuitGanetiException(False, "Fatal safety violation, shutting down")
331 e50bdd68 Guido Trotter

332 e50bdd68 Guido Trotter
  """
333 e50bdd68 Guido Trotter
334 f1da30e6 Michael Hanselmann
335 d11bda8d Iustin Pop
class JobQueueError(GenericError):
336 f1da30e6 Michael Hanselmann
  """Job queue error.
337 f1da30e6 Michael Hanselmann

338 f1da30e6 Michael Hanselmann
  """
339 6797ec29 Iustin Pop
340 6797ec29 Iustin Pop
341 686d7433 Iustin Pop
class JobQueueDrainError(JobQueueError):
342 686d7433 Iustin Pop
  """Job queue is marked for drain error.
343 686d7433 Iustin Pop

344 686d7433 Iustin Pop
  This is raised when a job submission attempt is made but the queue
345 686d7433 Iustin Pop
  is marked for drain.
346 686d7433 Iustin Pop

347 686d7433 Iustin Pop
  """
348 686d7433 Iustin Pop
349 686d7433 Iustin Pop
350 f87b405e Michael Hanselmann
class JobQueueFull(JobQueueError):
351 f87b405e Michael Hanselmann
  """Job queue full error.
352 f87b405e Michael Hanselmann

353 f87b405e Michael Hanselmann
  Raised when job queue size reached its hard limit.
354 f87b405e Michael Hanselmann

355 f87b405e Michael Hanselmann
  """
356 f87b405e Michael Hanselmann
357 f87b405e Michael Hanselmann
358 9748ab35 Guido Trotter
class ConfdMagicError(GenericError):
359 9748ab35 Guido Trotter
  """A magic fourcc error in Ganeti confd.
360 9748ab35 Guido Trotter

361 9748ab35 Guido Trotter
  Errors processing the fourcc in ganeti confd datagrams.
362 9748ab35 Guido Trotter

363 9748ab35 Guido Trotter
  """
364 9748ab35 Guido Trotter
365 9748ab35 Guido Trotter
366 e4ccf6cd Guido Trotter
class ConfdClientError(GenericError):
367 e4ccf6cd Guido Trotter
  """A magic fourcc error in Ganeti confd.
368 e4ccf6cd Guido Trotter

369 e4ccf6cd Guido Trotter
  Errors in the confd client library.
370 e4ccf6cd Guido Trotter

371 e4ccf6cd Guido Trotter
  """
372 e4ccf6cd Guido Trotter
373 e4ccf6cd Guido Trotter
374 c8eded0b Guido Trotter
class UdpDataSizeError(GenericError):
375 c8eded0b Guido Trotter
  """UDP payload too big.
376 c8eded0b Guido Trotter

377 c8eded0b Guido Trotter
  """
378 c8eded0b Guido Trotter
379 c8eded0b Guido Trotter
380 4c32a8bd Luca Bigliardi
class NoCtypesError(GenericError):
381 4c32a8bd Luca Bigliardi
  """python ctypes module is not found in the system.
382 4c32a8bd Luca Bigliardi

383 4c32a8bd Luca Bigliardi
  """
384 4c32a8bd Luca Bigliardi
385 4c32a8bd Luca Bigliardi
386 8b312c1d Manuel Franceschini
class IPAddressError(GenericError):
387 8b312c1d Manuel Franceschini
  """Generic IP address error.
388 8b312c1d Manuel Franceschini

389 8b312c1d Manuel Franceschini
  """
390 8b312c1d Manuel Franceschini
391 8b312c1d Manuel Franceschini
392 7a8bda3f Michael Hanselmann
class LuxiError(GenericError):
393 7a8bda3f Michael Hanselmann
  """LUXI error.
394 7a8bda3f Michael Hanselmann

395 7a8bda3f Michael Hanselmann
  """
396 7a8bda3f Michael Hanselmann
397 7a8bda3f Michael Hanselmann
398 7578ab0a Michael Hanselmann
class QueryFilterParseError(ParseError):
399 7578ab0a Michael Hanselmann
  """Error while parsing query filter.
400 7578ab0a Michael Hanselmann

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

406 7578ab0a Michael Hanselmann
  """
407 7578ab0a Michael Hanselmann
  def GetDetails(self):
408 7578ab0a Michael Hanselmann
    """Returns a list of strings with details about the error.
409 7578ab0a Michael Hanselmann

410 7578ab0a Michael Hanselmann
    """
411 7578ab0a Michael Hanselmann
    try:
412 7578ab0a Michael Hanselmann
      (_, inner) = self.args
413 7578ab0a Michael Hanselmann
    except IndexError:
414 7578ab0a Michael Hanselmann
      return None
415 7578ab0a Michael Hanselmann
416 7578ab0a Michael Hanselmann
    return [str(inner.line),
417 7578ab0a Michael Hanselmann
            (" " * (inner.column - 1)) + "^",
418 7578ab0a Michael Hanselmann
            str(inner)]
419 7578ab0a Michael Hanselmann
420 7578ab0a Michael Hanselmann
421 352e1a26 Michael Hanselmann
class RapiTestResult(GenericError):
422 352e1a26 Michael Hanselmann
  """Exception containing results from RAPI test utilities.
423 352e1a26 Michael Hanselmann

424 352e1a26 Michael Hanselmann
  """
425 352e1a26 Michael Hanselmann
426 352e1a26 Michael Hanselmann
427 fbdac0d9 Michael Hanselmann
class FileStoragePathError(GenericError):
428 fbdac0d9 Michael Hanselmann
  """Error from file storage path validation.
429 fbdac0d9 Michael Hanselmann

430 fbdac0d9 Michael Hanselmann
  """
431 fbdac0d9 Michael Hanselmann
432 fbdac0d9 Michael Hanselmann
433 6797ec29 Iustin Pop
# errors should be added above
434 6797ec29 Iustin Pop
435 6797ec29 Iustin Pop
436 6797ec29 Iustin Pop
def GetErrorClass(name):
437 6797ec29 Iustin Pop
  """Return the class of an exception.
438 6797ec29 Iustin Pop

439 6797ec29 Iustin Pop
  Given the class name, return the class itself.
440 6797ec29 Iustin Pop

441 6797ec29 Iustin Pop
  @type name: str
442 6797ec29 Iustin Pop
  @param name: the exception name
443 6797ec29 Iustin Pop
  @rtype: class
444 6797ec29 Iustin Pop
  @return: the actual class, or None if not found
445 6797ec29 Iustin Pop

446 6797ec29 Iustin Pop
  """
447 6797ec29 Iustin Pop
  item = globals().get(name, None)
448 6797ec29 Iustin Pop
  if item is not None:
449 6797ec29 Iustin Pop
    if not (isinstance(item, type(Exception)) and
450 6797ec29 Iustin Pop
            issubclass(item, GenericError)):
451 6797ec29 Iustin Pop
      item = None
452 6797ec29 Iustin Pop
  return item
453 6956e9cd Iustin Pop
454 6956e9cd Iustin Pop
455 6956e9cd Iustin Pop
def EncodeException(err):
456 6956e9cd Iustin Pop
  """Encodes an exception into a format that L{MaybeRaise} will recognise.
457 6956e9cd Iustin Pop

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

461 6956e9cd Iustin Pop
  @type err: GenericError child
462 6956e9cd Iustin Pop
  @param err: usually a child of GenericError (but any exception
463 6956e9cd Iustin Pop
      will be accepted)
464 6956e9cd Iustin Pop
  @rtype: tuple
465 6956e9cd Iustin Pop
  @return: tuple of (exception name, exception arguments)
466 6956e9cd Iustin Pop

467 6956e9cd Iustin Pop
  """
468 6956e9cd Iustin Pop
  return (err.__class__.__name__, err.args)
469 6956e9cd Iustin Pop
470 6956e9cd Iustin Pop
471 84f790e6 Michael Hanselmann
def GetEncodedError(result):
472 84f790e6 Michael Hanselmann
  """If this looks like an encoded Ganeti exception, return it.
473 6956e9cd Iustin Pop

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

478 6956e9cd Iustin Pop
  """
479 6956e9cd Iustin Pop
  tlt = (tuple, list)
480 6956e9cd Iustin Pop
  if (isinstance(result, tlt) and len(result) == 2 and
481 6956e9cd Iustin Pop
      isinstance(result[1], tlt)):
482 6956e9cd Iustin Pop
    # custom ganeti errors
483 84f790e6 Michael Hanselmann
    errcls = GetErrorClass(result[0])
484 84f790e6 Michael Hanselmann
    if errcls:
485 84f790e6 Michael Hanselmann
      return (errcls, tuple(result[1]))
486 84f790e6 Michael Hanselmann
487 84f790e6 Michael Hanselmann
  return None
488 84f790e6 Michael Hanselmann
489 84f790e6 Michael Hanselmann
490 84f790e6 Michael Hanselmann
def MaybeRaise(result):
491 84f790e6 Michael Hanselmann
  """If this looks like an encoded Ganeti exception, raise it.
492 84f790e6 Michael Hanselmann

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

496 84f790e6 Michael Hanselmann
  """
497 84f790e6 Michael Hanselmann
  error = GetEncodedError(result)
498 84f790e6 Michael Hanselmann
  if error:
499 84f790e6 Michael Hanselmann
    (errcls, args) = error
500 98dfcaff Iustin Pop
    # pylint: disable=W0142
501 98dfcaff Iustin Pop
    raise errcls(*args)