Statistics
| Branch: | Tag: | Revision:

root / lib / errors.py @ 86a24969

History | View | Annotate | Download (10.5 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
65 a8083063 Iustin Pop
66 a8083063 Iustin Pop
class LockError(GenericError):
67 a8083063 Iustin Pop
  """Lock error exception.
68 a8083063 Iustin Pop

69 a8083063 Iustin Pop
  This signifies problems in the locking subsystem.
70 a8083063 Iustin Pop

71 a8083063 Iustin Pop
  """
72 a8083063 Iustin Pop
73 a8083063 Iustin Pop
74 b6522276 Michael Hanselmann
class PidFileLockError(LockError):
75 b6522276 Michael Hanselmann
  """PID file is already locked by another process.
76 b6522276 Michael Hanselmann

77 b6522276 Michael Hanselmann
  """
78 b6522276 Michael Hanselmann
79 b6522276 Michael Hanselmann
80 a8083063 Iustin Pop
class HypervisorError(GenericError):
81 a8083063 Iustin Pop
  """Hypervisor-related exception.
82 a8083063 Iustin Pop

83 a8083063 Iustin Pop
  This is raised in case we can't communicate with the hypervisor
84 a8083063 Iustin Pop
  properly.
85 a8083063 Iustin Pop

86 a8083063 Iustin Pop
  """
87 a8083063 Iustin Pop
88 a8083063 Iustin Pop
89 a8083063 Iustin Pop
class ProgrammerError(GenericError):
90 a8083063 Iustin Pop
  """Programming-related error.
91 a8083063 Iustin Pop

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

96 a8083063 Iustin Pop
  """
97 a8083063 Iustin Pop
98 a8083063 Iustin Pop
99 a8083063 Iustin Pop
class BlockDeviceError(GenericError):
100 a8083063 Iustin Pop
  """Block-device related exception.
101 a8083063 Iustin Pop

102 a8083063 Iustin Pop
  This is raised in case we can't setup the instance's block devices
103 a8083063 Iustin Pop
  properly.
104 a8083063 Iustin Pop

105 a8083063 Iustin Pop
  """
106 a8083063 Iustin Pop
107 a8083063 Iustin Pop
108 a8083063 Iustin Pop
class ConfigurationError(GenericError):
109 a8083063 Iustin Pop
  """Configuration related exception.
110 a8083063 Iustin Pop

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

114 a8083063 Iustin Pop
  """
115 a8083063 Iustin Pop
116 a8083063 Iustin Pop
117 4b63dc7a Iustin Pop
class ConfigVersionMismatch(ConfigurationError):
118 4b63dc7a Iustin Pop
  """Version mismatch in the configuration file.
119 4b63dc7a Iustin Pop

120 4b63dc7a Iustin Pop
  The error has two arguments: the expected and the actual found
121 4b63dc7a Iustin Pop
  version.
122 4b63dc7a Iustin Pop

123 4b63dc7a Iustin Pop
  """
124 4b63dc7a Iustin Pop
125 4b63dc7a Iustin Pop
126 2c8a5690 Guido Trotter
class ReservationError(GenericError):
127 2c8a5690 Guido Trotter
  """Errors reserving a resource.
128 2c8a5690 Guido Trotter

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

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

138 a8083063 Iustin Pop
  """
139 a8083063 Iustin Pop
140 a8083063 Iustin Pop
141 f4a2f532 Guido Trotter
class SignatureError(GenericError):
142 f4a2f532 Guido Trotter
  """Error authenticating a remote message.
143 f4a2f532 Guido Trotter

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

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

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

158 a8083063 Iustin Pop
  The argument to this exception should be the parameter name.
159 a8083063 Iustin Pop

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

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

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

175 a8083063 Iustin Pop
  """
176 a8083063 Iustin Pop
177 098c0958 Michael Hanselmann
178 a8083063 Iustin Pop
class OpExecError(GenericError):
179 a8083063 Iustin Pop
  """Error during OpCode execution.
180 a8083063 Iustin Pop

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

187 1ce03fb1 Michael Hanselmann
  """
188 1ce03fb1 Michael Hanselmann
189 1ce03fb1 Michael Hanselmann
190 a8083063 Iustin Pop
class OpCodeUnknown(GenericError):
191 a8083063 Iustin Pop
  """Unknown opcode submitted.
192 a8083063 Iustin Pop

193 a8083063 Iustin Pop
  This signifies a mismatch between the definitions on the client and
194 a8083063 Iustin Pop
  server side.
195 a8083063 Iustin Pop

196 a8083063 Iustin Pop
  """
197 a8083063 Iustin Pop
198 098c0958 Michael Hanselmann
199 685ee993 Iustin Pop
class JobLost(GenericError):
200 685ee993 Iustin Pop
  """Submitted job lost.
201 685ee993 Iustin Pop

202 685ee993 Iustin Pop
  The job was submitted but it cannot be found in the current job
203 685ee993 Iustin Pop
  list.
204 685ee993 Iustin Pop

205 685ee993 Iustin Pop
  """
206 685ee993 Iustin Pop
207 685ee993 Iustin Pop
208 3d6c5566 Guido Trotter
class JobFileCorrupted(GenericError):
209 3d6c5566 Guido Trotter
  """Job file could not be properly decoded/restored.
210 3d6c5566 Guido Trotter

211 3d6c5566 Guido Trotter
  """
212 3d6c5566 Guido Trotter
213 3d6c5566 Guido Trotter
214 89e1fc26 Iustin Pop
class ResolverError(GenericError):
215 89e1fc26 Iustin Pop
  """Host name cannot be resolved.
216 89e1fc26 Iustin Pop

217 89e1fc26 Iustin Pop
  This is not a normal situation for Ganeti, as we rely on having a
218 89e1fc26 Iustin Pop
  working resolver.
219 89e1fc26 Iustin Pop

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

224 89e1fc26 Iustin Pop
  """
225 89e1fc26 Iustin Pop
226 89e1fc26 Iustin Pop
227 a8083063 Iustin Pop
class HooksFailure(GenericError):
228 a8083063 Iustin Pop
  """A generic hook failure.
229 a8083063 Iustin Pop

230 a8083063 Iustin Pop
  This signifies usually a setup misconfiguration.
231 a8083063 Iustin Pop

232 a8083063 Iustin Pop
  """
233 a8083063 Iustin Pop
234 098c0958 Michael Hanselmann
235 a8083063 Iustin Pop
class HooksAbort(HooksFailure):
236 a8083063 Iustin Pop
  """A required hook has failed.
237 a8083063 Iustin Pop

238 a8083063 Iustin Pop
  This caused an abort of the operation in the initial phase. This
239 a8083063 Iustin Pop
  exception always has an attribute args which is a list of tuples of:
240 a8083063 Iustin Pop
    - node: the source node on which this hooks has failed
241 a8083063 Iustin Pop
    - script: the name of the script which aborted the run
242 a8083063 Iustin Pop

243 a8083063 Iustin Pop
  """
244 a8083063 Iustin Pop
245 098c0958 Michael Hanselmann
246 a8083063 Iustin Pop
class UnitParseError(GenericError):
247 a8083063 Iustin Pop
  """Unable to parse size unit.
248 a8083063 Iustin Pop

249 a8083063 Iustin Pop
  """
250 a8083063 Iustin Pop
251 ac2d0fe4 Michael Hanselmann
252 31155d60 Balazs Lecz
class ParseError(GenericError):
253 31155d60 Balazs Lecz
  """Generic parse error.
254 31155d60 Balazs Lecz

255 31155d60 Balazs Lecz
  Raised when unable to parse user input.
256 31155d60 Balazs Lecz

257 31155d60 Balazs Lecz
  """
258 31155d60 Balazs Lecz
259 31155d60 Balazs Lecz
260 a5728081 Guido Trotter
class TypeEnforcementError(GenericError):
261 a5728081 Guido Trotter
  """Unable to enforce data type.
262 a5728081 Guido Trotter

263 a5728081 Guido Trotter
  """
264 a8083063 Iustin Pop
265 ac2d0fe4 Michael Hanselmann
266 b6267745 Andrea Spadaccini
class X509CertError(GenericError):
267 b6267745 Andrea Spadaccini
  """Invalid X509 certificate.
268 b6267745 Andrea Spadaccini

269 b6267745 Andrea Spadaccini
  This error has two arguments: the certificate filename and the error cause.
270 b6267745 Andrea Spadaccini

271 b6267745 Andrea Spadaccini
  """
272 b6267745 Andrea Spadaccini
273 b6267745 Andrea Spadaccini
274 5c947f38 Iustin Pop
class TagError(GenericError):
275 5c947f38 Iustin Pop
  """Generic tag error.
276 5c947f38 Iustin Pop

277 5c947f38 Iustin Pop
  The argument to this exception will show the exact error.
278 5c947f38 Iustin Pop

279 5c947f38 Iustin Pop
  """
280 7bca53e4 Michael Hanselmann
281 7bca53e4 Michael Hanselmann
282 7bca53e4 Michael Hanselmann
class CommandError(GenericError):
283 7bca53e4 Michael Hanselmann
  """External command error.
284 7bca53e4 Michael Hanselmann

285 7bca53e4 Michael Hanselmann
  """
286 e50bdd68 Guido Trotter
287 e50bdd68 Guido Trotter
288 ac2d0fe4 Michael Hanselmann
class StorageError(GenericError):
289 ac2d0fe4 Michael Hanselmann
  """Storage-related exception.
290 ac2d0fe4 Michael Hanselmann

291 ac2d0fe4 Michael Hanselmann
  """
292 ac2d0fe4 Michael Hanselmann
293 589dee9a Guido Trotter
294 589dee9a Guido Trotter
class InotifyError(GenericError):
295 589dee9a Guido Trotter
  """Error raised when there is a failure setting up an inotify watcher.
296 589dee9a Guido Trotter

297 589dee9a Guido Trotter
  """
298 589dee9a Guido Trotter
299 ac2d0fe4 Michael Hanselmann
300 e50bdd68 Guido Trotter
class QuitGanetiException(Exception):
301 7e975535 Stephen Shirley
  """Signal Ganeti that it must quit.
302 e50bdd68 Guido Trotter

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

312 c41eea6e Iustin Pop
  Examples::
313 c41eea6e Iustin Pop

314 e50bdd68 Guido Trotter
    # Return a result of "True" to the caller, but quit ganeti afterwards
315 0623d351 Iustin Pop
    raise QuitGanetiException(True, None)
316 e50bdd68 Guido Trotter
    # Send an error to the caller, and quit ganeti
317 0623d351 Iustin Pop
    raise QuitGanetiException(False, "Fatal safety violation, shutting down")
318 e50bdd68 Guido Trotter

319 e50bdd68 Guido Trotter
  """
320 e50bdd68 Guido Trotter
321 f1da30e6 Michael Hanselmann
322 d11bda8d Iustin Pop
class JobQueueError(GenericError):
323 f1da30e6 Michael Hanselmann
  """Job queue error.
324 f1da30e6 Michael Hanselmann

325 f1da30e6 Michael Hanselmann
  """
326 6797ec29 Iustin Pop
327 6797ec29 Iustin Pop
328 686d7433 Iustin Pop
class JobQueueDrainError(JobQueueError):
329 686d7433 Iustin Pop
  """Job queue is marked for drain error.
330 686d7433 Iustin Pop

331 686d7433 Iustin Pop
  This is raised when a job submission attempt is made but the queue
332 686d7433 Iustin Pop
  is marked for drain.
333 686d7433 Iustin Pop

334 686d7433 Iustin Pop
  """
335 686d7433 Iustin Pop
336 686d7433 Iustin Pop
337 f87b405e Michael Hanselmann
class JobQueueFull(JobQueueError):
338 f87b405e Michael Hanselmann
  """Job queue full error.
339 f87b405e Michael Hanselmann

340 f87b405e Michael Hanselmann
  Raised when job queue size reached its hard limit.
341 f87b405e Michael Hanselmann

342 f87b405e Michael Hanselmann
  """
343 f87b405e Michael Hanselmann
344 f87b405e Michael Hanselmann
345 9748ab35 Guido Trotter
class ConfdMagicError(GenericError):
346 9748ab35 Guido Trotter
  """A magic fourcc error in Ganeti confd.
347 9748ab35 Guido Trotter

348 9748ab35 Guido Trotter
  Errors processing the fourcc in ganeti confd datagrams.
349 9748ab35 Guido Trotter

350 9748ab35 Guido Trotter
  """
351 9748ab35 Guido Trotter
352 9748ab35 Guido Trotter
353 e4ccf6cd Guido Trotter
class ConfdClientError(GenericError):
354 e4ccf6cd Guido Trotter
  """A magic fourcc error in Ganeti confd.
355 e4ccf6cd Guido Trotter

356 e4ccf6cd Guido Trotter
  Errors in the confd client library.
357 e4ccf6cd Guido Trotter

358 e4ccf6cd Guido Trotter
  """
359 e4ccf6cd Guido Trotter
360 e4ccf6cd Guido Trotter
361 c8eded0b Guido Trotter
class UdpDataSizeError(GenericError):
362 c8eded0b Guido Trotter
  """UDP payload too big.
363 c8eded0b Guido Trotter

364 c8eded0b Guido Trotter
  """
365 c8eded0b Guido Trotter
366 c8eded0b Guido Trotter
367 4c32a8bd Luca Bigliardi
class NoCtypesError(GenericError):
368 4c32a8bd Luca Bigliardi
  """python ctypes module is not found in the system.
369 4c32a8bd Luca Bigliardi

370 4c32a8bd Luca Bigliardi
  """
371 4c32a8bd Luca Bigliardi
372 4c32a8bd Luca Bigliardi
373 8b312c1d Manuel Franceschini
class IPAddressError(GenericError):
374 8b312c1d Manuel Franceschini
  """Generic IP address error.
375 8b312c1d Manuel Franceschini

376 8b312c1d Manuel Franceschini
  """
377 8b312c1d Manuel Franceschini
378 8b312c1d Manuel Franceschini
379 7a8bda3f Michael Hanselmann
class LuxiError(GenericError):
380 7a8bda3f Michael Hanselmann
  """LUXI error.
381 7a8bda3f Michael Hanselmann

382 7a8bda3f Michael Hanselmann
  """
383 7a8bda3f Michael Hanselmann
384 7a8bda3f Michael Hanselmann
385 7578ab0a Michael Hanselmann
class QueryFilterParseError(ParseError):
386 7578ab0a Michael Hanselmann
  """Error while parsing query filter.
387 7578ab0a Michael Hanselmann

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

393 7578ab0a Michael Hanselmann
  """
394 7578ab0a Michael Hanselmann
  def GetDetails(self):
395 7578ab0a Michael Hanselmann
    """Returns a list of strings with details about the error.
396 7578ab0a Michael Hanselmann

397 7578ab0a Michael Hanselmann
    """
398 7578ab0a Michael Hanselmann
    try:
399 7578ab0a Michael Hanselmann
      (_, inner) = self.args
400 7578ab0a Michael Hanselmann
    except IndexError:
401 7578ab0a Michael Hanselmann
      return None
402 7578ab0a Michael Hanselmann
403 7578ab0a Michael Hanselmann
    return [str(inner.line),
404 7578ab0a Michael Hanselmann
            (" " * (inner.column - 1)) + "^",
405 7578ab0a Michael Hanselmann
            str(inner)]
406 7578ab0a Michael Hanselmann
407 7578ab0a Michael Hanselmann
408 352e1a26 Michael Hanselmann
class RapiTestResult(GenericError):
409 352e1a26 Michael Hanselmann
  """Exception containing results from RAPI test utilities.
410 352e1a26 Michael Hanselmann

411 352e1a26 Michael Hanselmann
  """
412 352e1a26 Michael Hanselmann
413 352e1a26 Michael Hanselmann
414 fbdac0d9 Michael Hanselmann
class FileStoragePathError(GenericError):
415 fbdac0d9 Michael Hanselmann
  """Error from file storage path validation.
416 fbdac0d9 Michael Hanselmann

417 fbdac0d9 Michael Hanselmann
  """
418 fbdac0d9 Michael Hanselmann
419 fbdac0d9 Michael Hanselmann
420 6797ec29 Iustin Pop
# errors should be added above
421 6797ec29 Iustin Pop
422 6797ec29 Iustin Pop
423 6797ec29 Iustin Pop
def GetErrorClass(name):
424 6797ec29 Iustin Pop
  """Return the class of an exception.
425 6797ec29 Iustin Pop

426 6797ec29 Iustin Pop
  Given the class name, return the class itself.
427 6797ec29 Iustin Pop

428 6797ec29 Iustin Pop
  @type name: str
429 6797ec29 Iustin Pop
  @param name: the exception name
430 6797ec29 Iustin Pop
  @rtype: class
431 6797ec29 Iustin Pop
  @return: the actual class, or None if not found
432 6797ec29 Iustin Pop

433 6797ec29 Iustin Pop
  """
434 6797ec29 Iustin Pop
  item = globals().get(name, None)
435 6797ec29 Iustin Pop
  if item is not None:
436 6797ec29 Iustin Pop
    if not (isinstance(item, type(Exception)) and
437 6797ec29 Iustin Pop
            issubclass(item, GenericError)):
438 6797ec29 Iustin Pop
      item = None
439 6797ec29 Iustin Pop
  return item
440 6956e9cd Iustin Pop
441 6956e9cd Iustin Pop
442 6956e9cd Iustin Pop
def EncodeException(err):
443 6956e9cd Iustin Pop
  """Encodes an exception into a format that L{MaybeRaise} will recognise.
444 6956e9cd Iustin Pop

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

448 6956e9cd Iustin Pop
  @type err: GenericError child
449 6956e9cd Iustin Pop
  @param err: usually a child of GenericError (but any exception
450 6956e9cd Iustin Pop
      will be accepted)
451 6956e9cd Iustin Pop
  @rtype: tuple
452 6956e9cd Iustin Pop
  @return: tuple of (exception name, exception arguments)
453 6956e9cd Iustin Pop

454 6956e9cd Iustin Pop
  """
455 6956e9cd Iustin Pop
  return (err.__class__.__name__, err.args)
456 6956e9cd Iustin Pop
457 6956e9cd Iustin Pop
458 84f790e6 Michael Hanselmann
def GetEncodedError(result):
459 84f790e6 Michael Hanselmann
  """If this looks like an encoded Ganeti exception, return it.
460 6956e9cd Iustin Pop

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

465 6956e9cd Iustin Pop
  """
466 6956e9cd Iustin Pop
  tlt = (tuple, list)
467 6956e9cd Iustin Pop
  if (isinstance(result, tlt) and len(result) == 2 and
468 6956e9cd Iustin Pop
      isinstance(result[1], tlt)):
469 6956e9cd Iustin Pop
    # custom ganeti errors
470 84f790e6 Michael Hanselmann
    errcls = GetErrorClass(result[0])
471 84f790e6 Michael Hanselmann
    if errcls:
472 84f790e6 Michael Hanselmann
      return (errcls, tuple(result[1]))
473 84f790e6 Michael Hanselmann
474 84f790e6 Michael Hanselmann
  return None
475 84f790e6 Michael Hanselmann
476 84f790e6 Michael Hanselmann
477 84f790e6 Michael Hanselmann
def MaybeRaise(result):
478 84f790e6 Michael Hanselmann
  """If this looks like an encoded Ganeti exception, raise it.
479 84f790e6 Michael Hanselmann

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

483 84f790e6 Michael Hanselmann
  """
484 84f790e6 Michael Hanselmann
  error = GetEncodedError(result)
485 84f790e6 Michael Hanselmann
  if error:
486 84f790e6 Michael Hanselmann
    (errcls, args) = error
487 98dfcaff Iustin Pop
    # pylint: disable=W0142
488 98dfcaff Iustin Pop
    raise errcls(*args)