+def FormatError(err):
+ """Return a formatted error message for a given error.
+
+ This function takes an exception instance and returns a tuple
+ consisting of two values: first, the recommended exit code, and
+ second, a string describing the error message (not
+ newline-terminated).
+
+ """
+ retcode = 1
+ obuf = StringIO()
+ if isinstance(err, errors.ConfigurationError):
+ msg = "Corrupt configuration file: %s" % err
+ logger.Error(msg)
+ obuf.write(msg + "\n")
+ obuf.write("Aborting.")
+ retcode = 2
+ elif isinstance(err, errors.HooksAbort):
+ obuf.write("Failure: hooks execution failed:\n")
+ for node, script, out in err.args[0]:
+ if out:
+ obuf.write(" node: %s, script: %s, output: %s\n" %
+ (node, script, out))
+ else:
+ obuf.write(" node: %s, script: %s (no output)\n" %
+ (node, script))
+ elif isinstance(err, errors.HooksFailure):
+ obuf.write("Failure: hooks general failure: %s" % str(err))
+ elif isinstance(err, errors.ResolverError):
+ this_host = utils.HostInfo.SysName()
+ if err.args[0] == this_host:
+ msg = "Failure: can't resolve my own hostname ('%s')"
+ else:
+ msg = "Failure: can't resolve hostname '%s'"
+ obuf.write(msg % err.args[0])
+ elif isinstance(err, errors.OpPrereqError):
+ obuf.write("Failure: prerequisites not met for this"
+ " operation:\n%s" % str(err))
+ elif isinstance(err, errors.OpExecError):
+ obuf.write("Failure: command execution error:\n%s" % str(err))
+ elif isinstance(err, errors.TagError):
+ obuf.write("Failure: invalid tag(s) given:\n%s" % str(err))
+ elif isinstance(err, errors.GenericError):
+ obuf.write("Unhandled Ganeti error: %s" % str(err))
+ else:
+ obuf.write("Unhandled exception: %s" % str(err))
+ return retcode, obuf.getvalue().rstrip('\n')
+
+