Statistics
| Branch: | Tag: | Revision:

root / lib / errors.py @ 1d4a4b26

History | View | Annotate | Download (11.2 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 9b221ea4 Michele Tartara
class DeviceCreationError(GenericError):
213 9b221ea4 Michele Tartara
  """Error during the creation of a device.
214 9b221ea4 Michele Tartara

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

218 9b221ea4 Michele Tartara
  """
219 9b221ea4 Michele Tartara
  def __init__(self, message, created_devices):
220 9b221ea4 Michele Tartara
    GenericError.__init__(self)
221 9b221ea4 Michele Tartara
    self.message = message
222 9b221ea4 Michele Tartara
    self.created_devices = created_devices
223 9b221ea4 Michele Tartara
224 9b221ea4 Michele Tartara
  def __str__(self):
225 9b221ea4 Michele Tartara
    return self.message
226 9b221ea4 Michele Tartara
227 9b221ea4 Michele Tartara
228 a8083063 Iustin Pop
class OpCodeUnknown(GenericError):
229 a8083063 Iustin Pop
  """Unknown opcode submitted.
230 a8083063 Iustin Pop

231 a8083063 Iustin Pop
  This signifies a mismatch between the definitions on the client and
232 a8083063 Iustin Pop
  server side.
233 a8083063 Iustin Pop

234 a8083063 Iustin Pop
  """
235 a8083063 Iustin Pop
236 098c0958 Michael Hanselmann
237 685ee993 Iustin Pop
class JobLost(GenericError):
238 685ee993 Iustin Pop
  """Submitted job lost.
239 685ee993 Iustin Pop

240 685ee993 Iustin Pop
  The job was submitted but it cannot be found in the current job
241 685ee993 Iustin Pop
  list.
242 685ee993 Iustin Pop

243 685ee993 Iustin Pop
  """
244 685ee993 Iustin Pop
245 685ee993 Iustin Pop
246 3d6c5566 Guido Trotter
class JobFileCorrupted(GenericError):
247 3d6c5566 Guido Trotter
  """Job file could not be properly decoded/restored.
248 3d6c5566 Guido Trotter

249 3d6c5566 Guido Trotter
  """
250 3d6c5566 Guido Trotter
251 3d6c5566 Guido Trotter
252 89e1fc26 Iustin Pop
class ResolverError(GenericError):
253 89e1fc26 Iustin Pop
  """Host name cannot be resolved.
254 89e1fc26 Iustin Pop

255 89e1fc26 Iustin Pop
  This is not a normal situation for Ganeti, as we rely on having a
256 89e1fc26 Iustin Pop
  working resolver.
257 89e1fc26 Iustin Pop

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

262 89e1fc26 Iustin Pop
  """
263 89e1fc26 Iustin Pop
264 89e1fc26 Iustin Pop
265 a8083063 Iustin Pop
class HooksFailure(GenericError):
266 a8083063 Iustin Pop
  """A generic hook failure.
267 a8083063 Iustin Pop

268 a8083063 Iustin Pop
  This signifies usually a setup misconfiguration.
269 a8083063 Iustin Pop

270 a8083063 Iustin Pop
  """
271 a8083063 Iustin Pop
272 098c0958 Michael Hanselmann
273 a8083063 Iustin Pop
class HooksAbort(HooksFailure):
274 a8083063 Iustin Pop
  """A required hook has failed.
275 a8083063 Iustin Pop

276 a8083063 Iustin Pop
  This caused an abort of the operation in the initial phase. This
277 a8083063 Iustin Pop
  exception always has an attribute args which is a list of tuples of:
278 a8083063 Iustin Pop
    - node: the source node on which this hooks has failed
279 a8083063 Iustin Pop
    - script: the name of the script which aborted the run
280 a8083063 Iustin Pop

281 a8083063 Iustin Pop
  """
282 a8083063 Iustin Pop
283 098c0958 Michael Hanselmann
284 a8083063 Iustin Pop
class UnitParseError(GenericError):
285 a8083063 Iustin Pop
  """Unable to parse size unit.
286 a8083063 Iustin Pop

287 a8083063 Iustin Pop
  """
288 a8083063 Iustin Pop
289 ac2d0fe4 Michael Hanselmann
290 31155d60 Balazs Lecz
class ParseError(GenericError):
291 31155d60 Balazs Lecz
  """Generic parse error.
292 31155d60 Balazs Lecz

293 31155d60 Balazs Lecz
  Raised when unable to parse user input.
294 31155d60 Balazs Lecz

295 31155d60 Balazs Lecz
  """
296 31155d60 Balazs Lecz
297 31155d60 Balazs Lecz
298 a5728081 Guido Trotter
class TypeEnforcementError(GenericError):
299 a5728081 Guido Trotter
  """Unable to enforce data type.
300 a5728081 Guido Trotter

301 a5728081 Guido Trotter
  """
302 a8083063 Iustin Pop
303 ac2d0fe4 Michael Hanselmann
304 b6267745 Andrea Spadaccini
class X509CertError(GenericError):
305 b6267745 Andrea Spadaccini
  """Invalid X509 certificate.
306 b6267745 Andrea Spadaccini

307 b6267745 Andrea Spadaccini
  This error has two arguments: the certificate filename and the error cause.
308 b6267745 Andrea Spadaccini

309 b6267745 Andrea Spadaccini
  """
310 b6267745 Andrea Spadaccini
311 b6267745 Andrea Spadaccini
312 5c947f38 Iustin Pop
class TagError(GenericError):
313 5c947f38 Iustin Pop
  """Generic tag error.
314 5c947f38 Iustin Pop

315 5c947f38 Iustin Pop
  The argument to this exception will show the exact error.
316 5c947f38 Iustin Pop

317 5c947f38 Iustin Pop
  """
318 7bca53e4 Michael Hanselmann
319 7bca53e4 Michael Hanselmann
320 7bca53e4 Michael Hanselmann
class CommandError(GenericError):
321 7bca53e4 Michael Hanselmann
  """External command error.
322 7bca53e4 Michael Hanselmann

323 7bca53e4 Michael Hanselmann
  """
324 e50bdd68 Guido Trotter
325 e50bdd68 Guido Trotter
326 ac2d0fe4 Michael Hanselmann
class StorageError(GenericError):
327 ac2d0fe4 Michael Hanselmann
  """Storage-related exception.
328 ac2d0fe4 Michael Hanselmann

329 ac2d0fe4 Michael Hanselmann
  """
330 ac2d0fe4 Michael Hanselmann
331 589dee9a Guido Trotter
332 589dee9a Guido Trotter
class InotifyError(GenericError):
333 589dee9a Guido Trotter
  """Error raised when there is a failure setting up an inotify watcher.
334 589dee9a Guido Trotter

335 589dee9a Guido Trotter
  """
336 589dee9a Guido Trotter
337 ac2d0fe4 Michael Hanselmann
338 e50bdd68 Guido Trotter
class QuitGanetiException(Exception):
339 7e975535 Stephen Shirley
  """Signal Ganeti that it must quit.
340 e50bdd68 Guido Trotter

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

350 c41eea6e Iustin Pop
  Examples::
351 c41eea6e Iustin Pop

352 e50bdd68 Guido Trotter
    # Return a result of "True" to the caller, but quit ganeti afterwards
353 0623d351 Iustin Pop
    raise QuitGanetiException(True, None)
354 e50bdd68 Guido Trotter
    # Send an error to the caller, and quit ganeti
355 0623d351 Iustin Pop
    raise QuitGanetiException(False, "Fatal safety violation, shutting down")
356 e50bdd68 Guido Trotter

357 e50bdd68 Guido Trotter
  """
358 e50bdd68 Guido Trotter
359 f1da30e6 Michael Hanselmann
360 d11bda8d Iustin Pop
class JobQueueError(GenericError):
361 f1da30e6 Michael Hanselmann
  """Job queue error.
362 f1da30e6 Michael Hanselmann

363 f1da30e6 Michael Hanselmann
  """
364 6797ec29 Iustin Pop
365 6797ec29 Iustin Pop
366 686d7433 Iustin Pop
class JobQueueDrainError(JobQueueError):
367 686d7433 Iustin Pop
  """Job queue is marked for drain error.
368 686d7433 Iustin Pop

369 686d7433 Iustin Pop
  This is raised when a job submission attempt is made but the queue
370 686d7433 Iustin Pop
  is marked for drain.
371 686d7433 Iustin Pop

372 686d7433 Iustin Pop
  """
373 686d7433 Iustin Pop
374 686d7433 Iustin Pop
375 f87b405e Michael Hanselmann
class JobQueueFull(JobQueueError):
376 f87b405e Michael Hanselmann
  """Job queue full error.
377 f87b405e Michael Hanselmann

378 f87b405e Michael Hanselmann
  Raised when job queue size reached its hard limit.
379 f87b405e Michael Hanselmann

380 f87b405e Michael Hanselmann
  """
381 f87b405e Michael Hanselmann
382 f87b405e Michael Hanselmann
383 9748ab35 Guido Trotter
class ConfdMagicError(GenericError):
384 9748ab35 Guido Trotter
  """A magic fourcc error in Ganeti confd.
385 9748ab35 Guido Trotter

386 9748ab35 Guido Trotter
  Errors processing the fourcc in ganeti confd datagrams.
387 9748ab35 Guido Trotter

388 9748ab35 Guido Trotter
  """
389 9748ab35 Guido Trotter
390 9748ab35 Guido Trotter
391 e4ccf6cd Guido Trotter
class ConfdClientError(GenericError):
392 e4ccf6cd Guido Trotter
  """A magic fourcc error in Ganeti confd.
393 e4ccf6cd Guido Trotter

394 e4ccf6cd Guido Trotter
  Errors in the confd client library.
395 e4ccf6cd Guido Trotter

396 e4ccf6cd Guido Trotter
  """
397 e4ccf6cd Guido Trotter
398 e4ccf6cd Guido Trotter
399 c8eded0b Guido Trotter
class UdpDataSizeError(GenericError):
400 c8eded0b Guido Trotter
  """UDP payload too big.
401 c8eded0b Guido Trotter

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

408 4c32a8bd Luca Bigliardi
  """
409 4c32a8bd Luca Bigliardi
410 4c32a8bd Luca Bigliardi
411 8b312c1d Manuel Franceschini
class IPAddressError(GenericError):
412 8b312c1d Manuel Franceschini
  """Generic IP address error.
413 8b312c1d Manuel Franceschini

414 8b312c1d Manuel Franceschini
  """
415 8b312c1d Manuel Franceschini
416 8b312c1d Manuel Franceschini
417 7a8bda3f Michael Hanselmann
class LuxiError(GenericError):
418 7a8bda3f Michael Hanselmann
  """LUXI error.
419 7a8bda3f Michael Hanselmann

420 7a8bda3f Michael Hanselmann
  """
421 7a8bda3f Michael Hanselmann
422 7a8bda3f Michael Hanselmann
423 7578ab0a Michael Hanselmann
class QueryFilterParseError(ParseError):
424 7578ab0a Michael Hanselmann
  """Error while parsing query filter.
425 7578ab0a Michael Hanselmann

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

431 7578ab0a Michael Hanselmann
  """
432 7578ab0a Michael Hanselmann
  def GetDetails(self):
433 7578ab0a Michael Hanselmann
    """Returns a list of strings with details about the error.
434 7578ab0a Michael Hanselmann

435 7578ab0a Michael Hanselmann
    """
436 7578ab0a Michael Hanselmann
    try:
437 7578ab0a Michael Hanselmann
      (_, inner) = self.args
438 7578ab0a Michael Hanselmann
    except IndexError:
439 7578ab0a Michael Hanselmann
      return None
440 7578ab0a Michael Hanselmann
441 7578ab0a Michael Hanselmann
    return [str(inner.line),
442 7578ab0a Michael Hanselmann
            (" " * (inner.column - 1)) + "^",
443 7578ab0a Michael Hanselmann
            str(inner)]
444 7578ab0a Michael Hanselmann
445 7578ab0a Michael Hanselmann
446 352e1a26 Michael Hanselmann
class RapiTestResult(GenericError):
447 352e1a26 Michael Hanselmann
  """Exception containing results from RAPI test utilities.
448 352e1a26 Michael Hanselmann

449 352e1a26 Michael Hanselmann
  """
450 352e1a26 Michael Hanselmann
451 352e1a26 Michael Hanselmann
452 fbdac0d9 Michael Hanselmann
class FileStoragePathError(GenericError):
453 fbdac0d9 Michael Hanselmann
  """Error from file storage path validation.
454 fbdac0d9 Michael Hanselmann

455 fbdac0d9 Michael Hanselmann
  """
456 fbdac0d9 Michael Hanselmann
457 fbdac0d9 Michael Hanselmann
458 6797ec29 Iustin Pop
# errors should be added above
459 6797ec29 Iustin Pop
460 6797ec29 Iustin Pop
461 6797ec29 Iustin Pop
def GetErrorClass(name):
462 6797ec29 Iustin Pop
  """Return the class of an exception.
463 6797ec29 Iustin Pop

464 6797ec29 Iustin Pop
  Given the class name, return the class itself.
465 6797ec29 Iustin Pop

466 6797ec29 Iustin Pop
  @type name: str
467 6797ec29 Iustin Pop
  @param name: the exception name
468 6797ec29 Iustin Pop
  @rtype: class
469 6797ec29 Iustin Pop
  @return: the actual class, or None if not found
470 6797ec29 Iustin Pop

471 6797ec29 Iustin Pop
  """
472 6797ec29 Iustin Pop
  item = globals().get(name, None)
473 6797ec29 Iustin Pop
  if item is not None:
474 6797ec29 Iustin Pop
    if not (isinstance(item, type(Exception)) and
475 6797ec29 Iustin Pop
            issubclass(item, GenericError)):
476 6797ec29 Iustin Pop
      item = None
477 6797ec29 Iustin Pop
  return item
478 6956e9cd Iustin Pop
479 6956e9cd Iustin Pop
480 6956e9cd Iustin Pop
def EncodeException(err):
481 6956e9cd Iustin Pop
  """Encodes an exception into a format that L{MaybeRaise} will recognise.
482 6956e9cd Iustin Pop

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

486 6956e9cd Iustin Pop
  @type err: GenericError child
487 6956e9cd Iustin Pop
  @param err: usually a child of GenericError (but any exception
488 6956e9cd Iustin Pop
      will be accepted)
489 6956e9cd Iustin Pop
  @rtype: tuple
490 6956e9cd Iustin Pop
  @return: tuple of (exception name, exception arguments)
491 6956e9cd Iustin Pop

492 6956e9cd Iustin Pop
  """
493 6956e9cd Iustin Pop
  return (err.__class__.__name__, err.args)
494 6956e9cd Iustin Pop
495 6956e9cd Iustin Pop
496 84f790e6 Michael Hanselmann
def GetEncodedError(result):
497 84f790e6 Michael Hanselmann
  """If this looks like an encoded Ganeti exception, return it.
498 6956e9cd Iustin Pop

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

503 6956e9cd Iustin Pop
  """
504 6956e9cd Iustin Pop
  tlt = (tuple, list)
505 6956e9cd Iustin Pop
  if (isinstance(result, tlt) and len(result) == 2 and
506 6956e9cd Iustin Pop
      isinstance(result[1], tlt)):
507 6956e9cd Iustin Pop
    # custom ganeti errors
508 84f790e6 Michael Hanselmann
    errcls = GetErrorClass(result[0])
509 84f790e6 Michael Hanselmann
    if errcls:
510 84f790e6 Michael Hanselmann
      return (errcls, tuple(result[1]))
511 84f790e6 Michael Hanselmann
512 84f790e6 Michael Hanselmann
  return None
513 84f790e6 Michael Hanselmann
514 84f790e6 Michael Hanselmann
515 84f790e6 Michael Hanselmann
def MaybeRaise(result):
516 84f790e6 Michael Hanselmann
  """If this looks like an encoded Ganeti exception, raise it.
517 84f790e6 Michael Hanselmann

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

521 84f790e6 Michael Hanselmann
  """
522 84f790e6 Michael Hanselmann
  error = GetEncodedError(result)
523 84f790e6 Michael Hanselmann
  if error:
524 84f790e6 Michael Hanselmann
    (errcls, args) = error
525 98dfcaff Iustin Pop
    # pylint: disable=W0142
526 98dfcaff Iustin Pop
    raise errcls(*args)