[libvirt] [PATCH 3/3] Add sentinel for virErrorNumber enum

Daniel P. Berrange berrange at redhat.com
Tue May 15 11:30:13 UTC 2012


From: "Daniel P. Berrange" <berrange at redhat.com>

Add a VIR_ERR_NUMBER_LAST sentinel for virErrorNumber and replace
the virErrorStr function by a VIR_ENUM_IMPL. Once this is done
all callers of virRaiseErrorFull are simply passing in a constant
string message with "%s" as the format arg. Thus virRaiseErrorFull
can have its var-args removed.

* include/libvirt/virterror.h: Add VIR_ERR_NUMBER_LAST sentinel
* src/rpc/virnetclientprogram.c, src/rpc/virnetclientstream.c:
  Remove format string passed to virRaiseErrorFull
* src/util/virterror.c: Remove var-args from virRaiseErrorFull
  and replace virErrorStr with an VIR_ENUM
* src/util/virterror_internal.h: Remove var-args from
  virRaiseErrorFull
* tests/cpuset, tests/undefine, tests/virsh-optparse: Adapt
  for changes in error message
---
 include/libvirt/virterror.h   |   37 +++
 src/rpc/virnetclientprogram.c |    4 +-
 src/rpc/virnetclientstream.c  |    2 +-
 src/util/virterror.c          |  702 +++++++++--------------------------------
 src/util/virterror_internal.h |    3 +-
 tests/cpuset                  |    2 +-
 tests/undefine                |    2 +-
 tests/virsh-optparse          |    2 +-
 8 files changed, 193 insertions(+), 561 deletions(-)

diff --git a/include/libvirt/virterror.h b/include/libvirt/virterror.h
index 0e0bc9c..6ae1f6d 100644
--- a/include/libvirt/virterror.h
+++ b/include/libvirt/virterror.h
@@ -172,27 +172,37 @@ typedef enum {
     VIR_ERR_NO_MEMORY = 2,		/* memory allocation failure */
     VIR_ERR_NO_SUPPORT = 3,		/* no support for this function */
     VIR_ERR_UNKNOWN_HOST = 4,		/* could not resolve hostname */
+
+
     VIR_ERR_NO_CONNECT = 5,		/* can't connect to hypervisor */
     VIR_ERR_INVALID_CONN = 6,		/* invalid connection object */
     VIR_ERR_INVALID_DOMAIN = 7,		/* invalid domain object */
     VIR_ERR_INVALID_ARG = 8,		/* invalid function argument */
     VIR_ERR_OPERATION_FAILED = 9,	/* a command to hypervisor failed */
+
+
     VIR_ERR_GET_FAILED = 10,		/* a HTTP GET command to failed */
     VIR_ERR_POST_FAILED = 11,		/* a HTTP POST command to failed */
     VIR_ERR_HTTP_ERROR = 12,		/* unexpected HTTP error code */
     VIR_ERR_SEXPR_SERIAL = 13,		/* failure to serialize an S-Expr */
     VIR_ERR_NO_XEN = 14,		/* could not open Xen hypervisor
                                            control */
+
+
     VIR_ERR_XEN_CALL = 15,		/* failure doing an hypervisor call */
     VIR_ERR_OS_TYPE = 16,		/* unknown OS type */
     VIR_ERR_NO_KERNEL = 17,		/* missing kernel information */
     VIR_ERR_NO_ROOT = 18,		/* missing root device information */
     VIR_ERR_NO_SOURCE = 19,		/* missing source device information */
+
+
     VIR_ERR_NO_TARGET = 20,		/* missing target device information */
     VIR_ERR_NO_NAME = 21,		/* missing domain name information */
     VIR_ERR_NO_OS = 22,			/* missing domain OS information */
     VIR_ERR_NO_DEVICE = 23,		/* missing domain devices information */
     VIR_ERR_NO_XENSTORE = 24,		/* could not open Xen Store control */
+
+
     VIR_ERR_DRIVER_FULL = 25,		/* too many drivers registered */
     VIR_ERR_CALL_FAILED = 26,		/* not supported by the drivers
                                            (DEPRECATED) */
@@ -201,33 +211,45 @@ typedef enum {
     VIR_ERR_DOM_EXIST = 28,		/* the domain already exist */
     VIR_ERR_OPERATION_DENIED = 29,	/* operation forbidden on read-only
                                            connections */
+
+
     VIR_ERR_OPEN_FAILED = 30,		/* failed to open a conf file */
     VIR_ERR_READ_FAILED = 31,		/* failed to read a conf file */
     VIR_ERR_PARSE_FAILED = 32,		/* failed to parse a conf file */
     VIR_ERR_CONF_SYNTAX = 33,		/* failed to parse the syntax of a
                                            conf file */
     VIR_ERR_WRITE_FAILED = 34,		/* failed to write a conf file */
+
+
     VIR_ERR_XML_DETAIL = 35,		/* detail of an XML error */
     VIR_ERR_INVALID_NETWORK = 36,	/* invalid network object */
     VIR_ERR_NETWORK_EXIST = 37,		/* the network already exist */
     VIR_ERR_SYSTEM_ERROR = 38,		/* general system call failure */
     VIR_ERR_RPC = 39,			/* some sort of RPC error */
+
+
     VIR_ERR_GNUTLS_ERROR = 40,		/* error from a GNUTLS call */
     VIR_WAR_NO_NETWORK = 41,		/* failed to start network */
     VIR_ERR_NO_DOMAIN = 42,		/* domain not found or unexpectedly
                                            disappeared */
     VIR_ERR_NO_NETWORK = 43,		/* network not found */
     VIR_ERR_INVALID_MAC = 44,		/* invalid MAC address */
+
+
     VIR_ERR_AUTH_FAILED = 45,		/* authentication failed */
     VIR_ERR_INVALID_STORAGE_POOL = 46,	/* invalid storage pool object */
     VIR_ERR_INVALID_STORAGE_VOL = 47,	/* invalid storage vol object */
     VIR_WAR_NO_STORAGE = 48,		/* failed to start storage */
     VIR_ERR_NO_STORAGE_POOL = 49,	/* storage pool not found */
+
+
     VIR_ERR_NO_STORAGE_VOL = 50,	/* storage volume not found */
     VIR_WAR_NO_NODE = 51,		/* failed to start node driver */
     VIR_ERR_INVALID_NODE_DEVICE = 52,	/* invalid node device object */
     VIR_ERR_NO_NODE_DEVICE = 53,	/* node device not found */
     VIR_ERR_NO_SECURITY_MODEL = 54,	/* security model not found */
+
+
     VIR_ERR_OPERATION_INVALID = 55,	/* operation is not applicable at this
                                            time */
     VIR_WAR_NO_INTERFACE = 56,		/* failed to start interface driver */
@@ -235,11 +257,15 @@ typedef enum {
     VIR_ERR_INVALID_INTERFACE = 58,	/* invalid interface object */
     VIR_ERR_MULTIPLE_INTERFACES = 59,	/* more than one matching interface
                                            found */
+
+
     VIR_WAR_NO_NWFILTER = 60,		/* failed to start nwfilter driver */
     VIR_ERR_INVALID_NWFILTER = 61,	/* invalid nwfilter object */
     VIR_ERR_NO_NWFILTER = 62,		/* nw filter pool not found */
     VIR_ERR_BUILD_FIREWALL = 63,	/* nw filter pool not found */
     VIR_WAR_NO_SECRET = 64,		/* failed to start secret storage */
+
+
     VIR_ERR_INVALID_SECRET = 65,	/* invalid secret */
     VIR_ERR_NO_SECRET = 66,		/* secret not found */
     VIR_ERR_CONFIG_UNSUPPORTED = 67,	/* unsupported configuration
@@ -247,12 +273,16 @@ typedef enum {
     VIR_ERR_OPERATION_TIMEOUT = 68,	/* timeout occurred during operation */
     VIR_ERR_MIGRATE_PERSIST_FAILED = 69,/* a migration worked, but making the
                                            VM persist on the dest host failed */
+
+
     VIR_ERR_HOOK_SCRIPT_FAILED = 70,	/* a synchronous hook script failed */
     VIR_ERR_INVALID_DOMAIN_SNAPSHOT = 71,/* invalid domain snapshot */
     VIR_ERR_NO_DOMAIN_SNAPSHOT = 72,	/* domain snapshot not found */
     VIR_ERR_INVALID_STREAM = 73,	/* stream pointer not valid */
     VIR_ERR_ARGUMENT_UNSUPPORTED = 74,	/* valid API use but unsupported by
                                            the given driver */
+
+
     VIR_ERR_STORAGE_PROBE_FAILED = 75,	/* storage pool probe failed */
     VIR_ERR_STORAGE_POOL_BUILT = 76,	/* storage pool already built */
     VIR_ERR_SNAPSHOT_REVERT_RISKY = 77,	/* force was not requested for a
@@ -260,10 +290,17 @@ typedef enum {
     VIR_ERR_OPERATION_ABORTED = 78,     /* operation on a domain was
                                            canceled/aborted by user */
     VIR_ERR_AUTH_CANCELLED = 79,        /* authentication cancelled */
+
+
     VIR_ERR_NO_DOMAIN_METADATA = 80,    /* The metadata is not present */
     VIR_ERR_MIGRATE_UNSAFE = 81,        /* Migration is not safe */
     VIR_ERR_OVERFLOW = 82,              /* integer overflow */
     VIR_ERR_BLOCK_COPY_ACTIVE = 83,     /* action prevented by block copy job */
+
+
+# ifdef VIR_ENUM_SENTINELS
+    VIR_ERR_NUMBER_LAST
+# endif
 } virErrorNumber;
 
 /**
diff --git a/src/rpc/virnetclientprogram.c b/src/rpc/virnetclientprogram.c
index e1e8846..383c0f5 100644
--- a/src/rpc/virnetclientprogram.c
+++ b/src/rpc/virnetclientprogram.c
@@ -169,7 +169,7 @@ virNetClientProgramDispatchError(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                           err.str3 ? *err.str3 : NULL,
                           err.int1,
                           err.int2,
-                          "%s", *err.message);
+                          *err.message);
     } else {
         virRaiseErrorFull(__FILE__, __FUNCTION__, __LINE__,
                           err.domain,
@@ -180,7 +180,7 @@ virNetClientProgramDispatchError(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                           err.str3 ? *err.str3 : NULL,
                           err.int1,
                           err.int2,
-                          "%s", err.message ? *err.message : _("Unknown error"));
+                          err.message ? *err.message : NULL);
     }
 
     ret = 0;
diff --git a/src/rpc/virnetclientstream.c b/src/rpc/virnetclientstream.c
index be06c66..84ed5d7 100644
--- a/src/rpc/virnetclientstream.c
+++ b/src/rpc/virnetclientstream.c
@@ -215,7 +215,7 @@ bool virNetClientStreamRaiseError(virNetClientStreamPtr st)
                       st->err.str3,
                       st->err.int1,
                       st->err.int2,
-                      "%s", st->err.message ? st->err.message : _("Unknown error"));
+                      st->err.message);
     virMutexUnlock(&st->lock);
     return true;
 }
diff --git a/src/util/virterror.c b/src/util/virterror.c
index fa57833..1449863 100644
--- a/src/util/virterror.c
+++ b/src/util/virterror.c
@@ -21,6 +21,7 @@
 #include "memory.h"
 #include "threads.h"
 #include "util.h"
+#include "ignore-value.h"
 
 virThreadLocal virLastErr;
 
@@ -103,6 +104,110 @@ VIR_ENUM_IMPL(virErrorDomain, VIR_ERR_DOMAIN_LAST,
     )
 
 
+VIR_ENUM_DECL(virErrorNumber)
+VIR_ENUM_IMPL(virErrorNumber, VIR_ERR_NUMBER_LAST,
+              N_(""), /* 0 */
+              N_("internal error"),
+              N_("out of memory"),
+              N_("this function is not supported by the current driver"),
+              N_("unknown hostname"),
+
+              N_("no connection driver available"), /* 5 */
+              N_("invalid connection pointer"),
+              N_("invalid domain pointer"),
+              N_("invalid argument"),
+              N_("operation failed"),
+
+              N_("HTTP GET request failed"), /* 10 */
+              N_("HTTP POST request failed"),
+              N_("unknown HTTP error code"),
+              N_("failed to serialize S-expression"),
+              N_("unable to use Xen hypervisor"),
+
+              N_("failed Xen hypercall"), /* 15 */
+              N_("unknown OS type"),
+              N_("missing kernel path"),
+              N_("missing root device name"),
+              N_("missing source information for device"),
+
+              N_("missing target information for device"), /* 20 */
+              N_("missing name information"),
+              N_("missing operating system information"),
+              N_("missing device list"),
+              N_("unable to use XenStore daemon"),
+
+              N_("too many drivers registered"), /* 25 */
+              N_("library call failed, possibly not supported"),
+              N_("XML description is invalid or not well formed"),
+              N_("domain exists already"),
+              N_("operation forbidden for read only access"),
+
+              N_("failed to open configuration file"), /* 30 */
+              N_("failed to read configuration file"),
+              N_("failed to parse configuration file"),
+              N_("configuration file syntax error"),
+              N_("failed to write configuration file"),
+
+              N_("XML parser error"), /* 35 */
+              N_("invalid network pointer"),
+              N_("network exists already"),
+              N_("system call error"),
+              N_("RPC error"),
+
+              N_("GNUTLS call error"), /* 40 */
+              N_("failed to find a network driver"),
+              N_("domain not found"),
+              N_("network not found"),
+              N_("invalid MAC address"),
+
+              N_("authentication failed"), /* 45 */
+              N_("invalid storage pool pointer"),
+              N_("invalid storage volume pointer"),
+              N_("failed to find a storage driver"),
+              N_("storage pool not found"),
+
+              N_("storage volume not found"), /* 50 */
+              N_("failed to find a node device driver"),
+              N_("invalid node device pointer"),
+              N_("node device not found"),
+              N_("security model not found"),
+
+              N_("requested operation is not valid"), /* 55 */
+              N_("failed to find a network interface driver"),
+              N_("interface not found"),
+              N_("invalid interface pointer"),
+              N_("multiple matching interfaces found"),
+
+              N_("failed to find a network filter driver"), /* 60 */
+              N_("invalid network filter"),
+              N_("network filter not found"),
+              N_("error while building firewall"),
+              N_("failed to find a secret driver"),
+
+              N_("invalid secret pointer"), /* 65 */
+              N_("secret not found"),
+              N_("unsupported configuration"),
+              N_("timed out during operation"),
+              N_("failed to make domain persistent after migration"),
+
+              N_("hook script execution failed"), /* 70 */
+              N_("invalid snapshot pointer"),
+              N_("domain snapshot not found"),
+              N_("invalid stream pointer"),
+              N_("argument unsupported"),
+
+              N_("storage pool probe failed"), /* 75 */
+              N_("storage pool already built"),
+              N_("revert requires force"),
+              N_("operation aborted"),
+              N_("authentication cancelled"),
+
+              N_("metadata not found"), /* 80 */
+              N_("unsafe migration"),
+              N_("numerical overflow"),
+              N_("block copy still active"),
+    )
+
 /*
  * Internal helper that is called when a thread exits, to
  * release the error object stored in the thread local
@@ -576,8 +681,7 @@ virDispatchError(virConnectPtr conn)
  * @str3: extra string info
  * @int1: extra int info
  * @int2: extra int info
- * @fmt:  the message to display/transmit
- * @...:  extra parameters for the message display
+ * @msg:  the pre-formatted message to display/transmit
  *
  * Internal routine called when an error is detected. It will raise it
  * immediately if a callback is found and store it for later handling.
@@ -594,11 +698,10 @@ virRaiseErrorFull(const char *filename ATTRIBUTE_UNUSED,
                   const char *str3,
                   int int1,
                   int int2,
-                  const char *fmt, ...)
+                  const char *msg)
 {
     int save_errno = errno;
     virErrorPtr to;
-    char *str;
     int priority;
 
     /*
@@ -620,18 +723,6 @@ virRaiseErrorFull(const char *filename ATTRIBUTE_UNUSED,
     }
 
     /*
-     * formats the message; drop message on OOM situations
-     */
-    if (fmt == NULL) {
-        str = strdup(_("No error message provided"));
-    } else {
-        va_list ap;
-        va_start(ap, fmt);
-        virVasprintf(&str, fmt, ap);
-        va_end(ap);
-    }
-
-    /*
      * Save the information about the error
      */
     /*
@@ -640,7 +731,7 @@ virRaiseErrorFull(const char *filename ATTRIBUTE_UNUSED,
      */
     to->domain = domain;
     to->code = code;
-    to->message = str;
+    to->message = strdup(msg);
     to->level = level;
     if (str1 != NULL)
         to->str1 = strdup(str1);
@@ -661,518 +752,11 @@ virRaiseErrorFull(const char *filename ATTRIBUTE_UNUSED,
     virLogMessage(filename, priority,
                   funcname, linenr,
                   virErrorLogPriorityFilter ? 0 : 1,
-                  "%s", str);
+                  "%s", msg);
 
     errno = save_errno;
 }
 
-/**
- * virErrorMsg:
- * @error: the virErrorNumber
- * @info: usually the first parameter string
- *
- * Internal routine to get the message associated to an error raised
- * from the library
- *
- * Returns the constant string associated to @error
- */
-static const char *
-virErrorMsg(virErrorNumber error, const char *info)
-{
-    const char *errmsg = NULL;
-
-    switch (error) {
-        case VIR_ERR_OK:
-            return NULL;
-        case VIR_ERR_INTERNAL_ERROR:
-            if (info != NULL)
-              errmsg = _("internal error %s");
-            else
-              errmsg = _("internal error");
-            break;
-        case VIR_ERR_NO_MEMORY:
-            errmsg = _("out of memory");
-            break;
-        case VIR_ERR_NO_SUPPORT:
-            if (info == NULL)
-                errmsg = _("this function is not supported by the connection driver");
-            else
-                errmsg = _("this function is not supported by the connection driver: %s");
-            break;
-        case VIR_ERR_NO_CONNECT:
-            if (info == NULL)
-                errmsg = _("no connection driver available");
-            else
-                errmsg = _("no connection driver available for %s");
-            break;
-        case VIR_ERR_INVALID_CONN:
-            if (info == NULL)
-                errmsg = _("invalid connection pointer in");
-            else
-                errmsg = _("invalid connection pointer in %s");
-            break;
-        case VIR_ERR_INVALID_DOMAIN:
-            if (info == NULL)
-                errmsg = _("invalid domain pointer in");
-            else
-                errmsg = _("invalid domain pointer in %s");
-            break;
-        case VIR_ERR_INVALID_ARG:
-            if (info == NULL)
-                errmsg = _("invalid argument");
-            else
-                errmsg = _("invalid argument: %s");
-            break;
-        case VIR_ERR_OPERATION_FAILED:
-            if (info != NULL)
-                errmsg = _("operation failed: %s");
-            else
-                errmsg = _("operation failed");
-            break;
-        case VIR_ERR_GET_FAILED:
-            if (info != NULL)
-                errmsg = _("GET operation failed: %s");
-            else
-                errmsg = _("GET operation failed");
-            break;
-        case VIR_ERR_POST_FAILED:
-            if (info != NULL)
-                errmsg = _("POST operation failed: %s");
-            else
-                errmsg = _("POST operation failed");
-            break;
-        case VIR_ERR_HTTP_ERROR:
-            errmsg = _("got unknown HTTP error code %d");
-            break;
-        case VIR_ERR_UNKNOWN_HOST:
-            if (info != NULL)
-                errmsg = _("unknown host %s");
-            else
-                errmsg = _("unknown host");
-            break;
-        case VIR_ERR_SEXPR_SERIAL:
-            if (info != NULL)
-                errmsg = _("failed to serialize S-Expr: %s");
-            else
-                errmsg = _("failed to serialize S-Expr");
-            break;
-        case VIR_ERR_NO_XEN:
-            if (info == NULL)
-                errmsg = _("could not use Xen hypervisor entry");
-            else
-                errmsg = _("could not use Xen hypervisor entry %s");
-            break;
-        case VIR_ERR_NO_XENSTORE:
-            if (info == NULL)
-                errmsg = _("could not connect to Xen Store");
-            else
-                errmsg = _("could not connect to Xen Store %s");
-            break;
-        case VIR_ERR_XEN_CALL:
-            errmsg = _("failed Xen syscall %s");
-            break;
-        case VIR_ERR_OS_TYPE:
-            if (info == NULL)
-                errmsg = _("unknown OS type");
-            else
-                errmsg = _("unknown OS type %s");
-            break;
-        case VIR_ERR_NO_KERNEL:
-            errmsg = _("missing kernel information");
-            break;
-        case VIR_ERR_NO_ROOT:
-            if (info == NULL)
-                errmsg = _("missing root device information");
-            else
-                errmsg = _("missing root device information in %s");
-            break;
-        case VIR_ERR_NO_SOURCE:
-            if (info == NULL)
-                errmsg = _("missing source information for device");
-            else
-                errmsg = _("missing source information for device %s");
-            break;
-        case VIR_ERR_NO_TARGET:
-            if (info == NULL)
-                errmsg = _("missing target information for device");
-            else
-                errmsg = _("missing target information for device %s");
-            break;
-        case VIR_ERR_NO_NAME:
-            if (info == NULL)
-                errmsg = _("missing name information");
-            else
-                errmsg = _("missing name information in %s");
-            break;
-        case VIR_ERR_NO_OS:
-            if (info == NULL)
-                errmsg = _("missing operating system information");
-            else
-                errmsg = _("missing operating system information for %s");
-            break;
-        case VIR_ERR_NO_DEVICE:
-            if (info == NULL)
-                errmsg = _("missing devices information");
-            else
-                errmsg = _("missing devices information for %s");
-            break;
-        case VIR_ERR_DRIVER_FULL:
-            if (info == NULL)
-                errmsg = _("too many drivers registered");
-            else
-                errmsg = _("too many drivers registered in %s");
-            break;
-        case VIR_ERR_CALL_FAILED: /* DEPRECATED, use VIR_ERR_NO_SUPPORT */
-            if (info == NULL)
-                errmsg = _("library call failed, possibly not supported");
-            else
-                errmsg = _("library call %s failed, possibly not supported");
-            break;
-        case VIR_ERR_XML_ERROR:
-            if (info == NULL)
-                errmsg = _("XML description is invalid or not well formed");
-            else
-                errmsg = _("XML error: %s");
-            break;
-        case VIR_ERR_DOM_EXIST:
-            if (info == NULL)
-                errmsg = _("this domain exists already");
-            else
-                errmsg = _("domain %s exists already");
-            break;
-        case VIR_ERR_OPERATION_DENIED:
-            if (info == NULL)
-                errmsg = _("operation forbidden for read only access");
-            else
-                errmsg = _("operation %s forbidden for read only access");
-            break;
-        case VIR_ERR_OPEN_FAILED:
-            if (info == NULL)
-                errmsg = _("failed to open configuration file for reading");
-            else
-                errmsg = _("failed to open %s for reading");
-            break;
-        case VIR_ERR_READ_FAILED:
-            if (info == NULL)
-                errmsg = _("failed to read configuration file");
-            else
-                errmsg = _("failed to read configuration file %s");
-            break;
-        case VIR_ERR_PARSE_FAILED:
-            if (info == NULL)
-                errmsg = _("failed to parse configuration file");
-            else
-                errmsg = _("failed to parse configuration file %s");
-            break;
-        case VIR_ERR_CONF_SYNTAX:
-            if (info == NULL)
-                errmsg = _("configuration file syntax error");
-            else
-                errmsg = _("configuration file syntax error: %s");
-            break;
-        case VIR_ERR_WRITE_FAILED:
-            if (info == NULL)
-                errmsg = _("failed to write configuration file");
-            else
-                errmsg = _("failed to write configuration file: %s");
-            break;
-        case VIR_ERR_XML_DETAIL:
-            if (info == NULL)
-                errmsg = _("parser error");
-            else
-                errmsg = "%s";
-            break;
-        case VIR_ERR_INVALID_NETWORK:
-            if (info == NULL)
-                errmsg = _("invalid network pointer in");
-            else
-                errmsg = _("invalid network pointer in %s");
-            break;
-        case VIR_ERR_NETWORK_EXIST:
-            if (info == NULL)
-                errmsg = _("this network exists already");
-            else
-                errmsg = _("network %s exists already");
-            break;
-        case VIR_ERR_SYSTEM_ERROR:
-            if (info == NULL)
-                errmsg = _("system call error");
-            else
-                errmsg = "%s";
-            break;
-        case VIR_ERR_RPC:
-            if (info == NULL)
-                errmsg = _("RPC error");
-            else
-                errmsg = "%s";
-            break;
-        case VIR_ERR_GNUTLS_ERROR:
-            if (info == NULL)
-                errmsg = _("GNUTLS call error");
-            else
-                errmsg = "%s";
-            break;
-        case VIR_WAR_NO_NETWORK:
-            if (info == NULL)
-                errmsg = _("Failed to find the network");
-            else
-                errmsg = _("Failed to find the network: %s");
-            break;
-        case VIR_ERR_NO_DOMAIN:
-            if (info == NULL)
-                errmsg = _("Domain not found");
-            else
-                errmsg = _("Domain not found: %s");
-            break;
-        case VIR_ERR_NO_NETWORK:
-            if (info == NULL)
-                errmsg = _("Network not found");
-            else
-                errmsg = _("Network not found: %s");
-            break;
-        case VIR_ERR_INVALID_MAC:
-            if (info == NULL)
-                errmsg = _("invalid MAC address");
-            else
-                errmsg = _("invalid MAC address: %s");
-            break;
-        case VIR_ERR_AUTH_FAILED:
-            if (info == NULL)
-                errmsg = _("authentication failed");
-            else
-                errmsg = _("authentication failed: %s");
-            break;
-        case VIR_ERR_AUTH_CANCELLED:
-            if (info == NULL)
-                errmsg = _("authentication cancelled");
-            else
-                errmsg = _("authentication cancelled: %s");
-            break;
-        case VIR_ERR_NO_STORAGE_POOL:
-            if (info == NULL)
-                errmsg = _("Storage pool not found");
-            else
-                errmsg = _("Storage pool not found: %s");
-            break;
-        case VIR_ERR_NO_STORAGE_VOL:
-            if (info == NULL)
-                errmsg = _("Storage volume not found");
-            else
-                errmsg = _("Storage volume not found: %s");
-            break;
-        case VIR_ERR_STORAGE_PROBE_FAILED:
-            if (info == NULL)
-                errmsg = _("Storage pool probe failed");
-            else
-                errmsg = _("Storage pool probe failed: %s");
-            break;
-        case VIR_ERR_STORAGE_POOL_BUILT:
-            if (info == NULL)
-                errmsg = _("Storage pool already built");
-            else
-                errmsg = _("Storage pool already built: %s");
-            break;
-        case VIR_ERR_INVALID_STORAGE_POOL:
-            if (info == NULL)
-                errmsg = _("invalid storage pool pointer in");
-            else
-                errmsg = _("invalid storage pool pointer in %s");
-            break;
-        case VIR_ERR_INVALID_STORAGE_VOL:
-            if (info == NULL)
-                errmsg = _("invalid storage volume pointer in");
-            else
-                errmsg = _("invalid storage volume pointer in %s");
-            break;
-        case VIR_WAR_NO_STORAGE:
-            if (info == NULL)
-                errmsg = _("Failed to find a storage driver");
-            else
-                errmsg = _("Failed to find a storage driver: %s");
-            break;
-        case VIR_WAR_NO_NODE:
-            if (info == NULL)
-                errmsg = _("Failed to find a node driver");
-            else
-                errmsg = _("Failed to find a node driver: %s");
-            break;
-        case VIR_ERR_INVALID_NODE_DEVICE:
-            if (info == NULL)
-                errmsg = _("invalid node device pointer");
-            else
-                errmsg = _("invalid node device pointer in %s");
-            break;
-        case VIR_ERR_NO_NODE_DEVICE:
-            if (info == NULL)
-                errmsg = _("Node device not found");
-            else
-                errmsg = _("Node device not found: %s");
-            break;
-        case VIR_ERR_NO_SECURITY_MODEL:
-            if (info == NULL)
-                errmsg = _("Security model not found");
-            else
-                errmsg = _("Security model not found: %s");
-            break;
-        case VIR_ERR_OPERATION_INVALID:
-            if (info == NULL)
-                errmsg = _("Requested operation is not valid");
-            else
-                errmsg = _("Requested operation is not valid: %s");
-            break;
-        case VIR_WAR_NO_INTERFACE:
-            if (info == NULL)
-                errmsg = _("Failed to find the interface");
-            else
-                errmsg = _("Failed to find the interface: %s");
-            break;
-        case VIR_ERR_NO_INTERFACE:
-            if (info == NULL)
-                errmsg = _("Interface not found");
-            else
-                errmsg = _("Interface not found: %s");
-            break;
-        case VIR_ERR_INVALID_INTERFACE:
-            if (info == NULL)
-                errmsg = _("invalid interface pointer in");
-            else
-                errmsg = _("invalid interface pointer in %s");
-            break;
-        case VIR_ERR_MULTIPLE_INTERFACES:
-            if (info == NULL)
-                errmsg = _("multiple matching interfaces found");
-            else
-                errmsg = _("multiple matching interfaces found: %s");
-            break;
-        case VIR_WAR_NO_SECRET:
-            if (info == NULL)
-                errmsg = _("Failed to find a secret storage driver");
-            else
-                errmsg = _("Failed to find a secret storage driver: %s");
-            break;
-        case VIR_ERR_INVALID_SECRET:
-            if (info == NULL)
-                errmsg = _("Invalid secret");
-            else
-                errmsg = _("Invalid secret: %s");
-            break;
-        case VIR_ERR_NO_SECRET:
-            if (info == NULL)
-                errmsg = _("Secret not found");
-            else
-                errmsg = _("Secret not found: %s");
-            break;
-        case VIR_WAR_NO_NWFILTER:
-            if (info == NULL)
-                errmsg = _("Failed to start the nwfilter driver");
-            else
-                errmsg = _("Failed to start the nwfilter driver: %s");
-            break;
-        case VIR_ERR_INVALID_NWFILTER:
-            if (info == NULL)
-                errmsg = _("Invalid network filter");
-            else
-                errmsg = _("Invalid network filter: %s");
-            break;
-        case VIR_ERR_NO_NWFILTER:
-            if (info == NULL)
-                errmsg = _("Network filter not found");
-            else
-                errmsg = _("Network filter not found: %s");
-            break;
-        case VIR_ERR_BUILD_FIREWALL:
-            if (info == NULL)
-                errmsg = _("Error while building firewall");
-            else
-                errmsg = _("Error while building firewall: %s");
-            break;
-        case VIR_ERR_CONFIG_UNSUPPORTED:
-            if (info == NULL)
-                errmsg = _("unsupported configuration");
-            else
-                errmsg = _("unsupported configuration: %s");
-            break;
-        case VIR_ERR_OPERATION_TIMEOUT:
-            if (info == NULL)
-                errmsg = _("Timed out during operation");
-            else
-                errmsg = _("Timed out during operation: %s");
-            break;
-        case VIR_ERR_MIGRATE_PERSIST_FAILED:
-            if (info == NULL)
-                errmsg = _("Failed to make domain persistent after migration");
-            else
-                errmsg = _("Failed to make domain persistent after migration: %s");
-            break;
-        case VIR_ERR_HOOK_SCRIPT_FAILED:
-            if (info == NULL)
-                errmsg = _("Hook script execution failed");
-            else
-                errmsg = _("Hook script execution failed: %s");
-            break;
-        case VIR_ERR_INVALID_DOMAIN_SNAPSHOT:
-            if (info == NULL)
-                errmsg = _("Invalid snapshot");
-            else
-                errmsg = _("Invalid snapshot: %s");
-            break;
-        case VIR_ERR_NO_DOMAIN_SNAPSHOT:
-            if (info == NULL)
-                errmsg = _("Domain snapshot not found");
-            else
-                errmsg = _("Domain snapshot not found: %s");
-            break;
-        case VIR_ERR_INVALID_STREAM:
-            if (info == NULL)
-                errmsg = _("invalid stream pointer");
-            else
-                errmsg = _("invalid stream pointer in %s");
-            break;
-        case VIR_ERR_ARGUMENT_UNSUPPORTED:
-            if (info == NULL)
-                errmsg = _("argument unsupported");
-            else
-                errmsg = _("argument unsupported: %s");
-            break;
-        case VIR_ERR_SNAPSHOT_REVERT_RISKY:
-            if (info == NULL)
-                errmsg = _("revert requires force");
-            else
-                errmsg = _("revert requires force: %s");
-            break;
-        case VIR_ERR_OPERATION_ABORTED:
-            if (info == NULL)
-                errmsg = _("operation aborted");
-            else
-                errmsg = _("operation aborted: %s");
-            break;
-        case VIR_ERR_NO_DOMAIN_METADATA:
-            if (info == NULL)
-                errmsg = _("metadata not found");
-            else
-                errmsg = _("metadata not found: %s");
-            break;
-        case VIR_ERR_MIGRATE_UNSAFE:
-            if (!info)
-                errmsg = _("Unsafe migration");
-            else
-                errmsg = _("Unsafe migration: %s");
-            break;
-        case VIR_ERR_OVERFLOW:
-            if (!info)
-                errmsg = _("numerical overflow");
-            else
-                errmsg = _("numerical overflow: %s");
-            break;
-        case VIR_ERR_BLOCK_COPY_ACTIVE:
-            if (!info)
-                errmsg = _("block copy still active");
-            else
-                errmsg = _("block copy still active: %s");
-            break;
-    }
-    return errmsg;
-}
 
 /**
  * virReportErrorHelper:
@@ -1196,23 +780,41 @@ void virReportErrorHelper(int domcode,
                           const char *fmt, ...)
 {
     int save_errno = errno;
-    va_list args;
-    char errorMessage[1024];
-    const char *virerr;
+    char *msg = NULL;
+    char *fmtmsg = NULL;
+    const char *virerr = gettext(virErrorNumberTypeToString(errorcode));
+
+    /* Special case a couple of codes where we don't want to
+     * prepend some worthless generic message, to a detailed
+     * piece of info */
+    if ((errorcode == VIR_ERR_GNUTLS_ERROR ||
+         errorcode == VIR_ERR_RPC ||
+         errorcode == VIR_ERR_SYSTEM_ERROR)
+        && fmt)
+        virerr = NULL;
 
     if (fmt) {
+        va_list args;
+
         va_start(args, fmt);
-        vsnprintf(errorMessage, sizeof(errorMessage)-1, fmt, args);
+        ignore_value(virVasprintf(&fmtmsg, fmt, args));
         va_end(args);
-    } else {
-        errorMessage[0] = '\0';
+
+        if (fmtmsg && virerr) {
+            ignore_value(virAsprintf(&msg, "%s: %s", virerr, fmtmsg));
+        } else if (fmtmsg) {
+            msg = fmtmsg;
+            fmtmsg = NULL;
+        }
     }
 
-    virerr = virErrorMsg(errorcode, (errorMessage[0] ? errorMessage : NULL));
     virRaiseErrorFull(filename, funcname, linenr,
                       domcode, errorcode, VIR_ERR_ERROR,
-                      virerr, errorMessage, NULL,
-                      -1, -1, virerr, errorMessage);
+                      virerr, fmtmsg, NULL,
+                      -1, -1,
+                      msg ? msg : virerr);
+    VIR_FREE(msg);
+    VIR_FREE(fmtmsg);
     errno = save_errno;
 }
 
@@ -1259,35 +861,30 @@ void virReportSystemErrorFull(int domcode,
 {
     int save_errno = errno;
     char strerror_buf[1024];
-    char msgDetailBuf[1024];
-
+    const char *virerr = gettext(virErrorNumberTypeToString(VIR_ERR_SYSTEM_ERROR));
     const char *errnoDetail = virStrerror(theerrno, strerror_buf,
                                           sizeof(strerror_buf));
-    const char *msg = virErrorMsg(VIR_ERR_SYSTEM_ERROR, fmt);
-    const char *msgDetail = NULL;
+    char *msg = NULL;
+    char *fmtmsg;
 
     if (fmt) {
         va_list args;
-        int n;
 
         va_start(args, fmt);
-        n = vsnprintf(msgDetailBuf, sizeof(msgDetailBuf), fmt, args);
+        ignore_value(virVasprintf(&fmtmsg, fmt, args));
         va_end(args);
 
-        size_t len = strlen(errnoDetail);
-        if (0 <= n && n + 2 + len < sizeof(msgDetailBuf)) {
-          char *p = msgDetailBuf + n;
-          stpcpy (stpcpy (p, ": "), errnoDetail);
-          msgDetail = msgDetailBuf;
-        }
+        if (fmtmsg)
+            ignore_value(virAsprintf(&msg, "%s: %s", fmtmsg, errnoDetail));
     }
 
-    if (!msgDetail)
-        msgDetail = errnoDetail;
-
     virRaiseErrorFull(filename, funcname, linenr,
                       domcode, VIR_ERR_SYSTEM_ERROR, VIR_ERR_ERROR,
-                      msg, msgDetail, NULL, -1, -1, msg, msgDetail);
+                      virerr, fmtmsg, errnoDetail,
+                      -1, -1,
+                      msg ? msg : errnoDetail);
+    VIR_FREE(fmtmsg);
+    VIR_FREE(msg);
     errno = save_errno;
 }
 
@@ -1307,11 +904,10 @@ void virReportOOMErrorFull(int domcode,
                            size_t linenr)
 {
     const char *virerr;
-
-    virerr = virErrorMsg(VIR_ERR_NO_MEMORY, NULL);
+    virerr = gettext(virErrorNumberTypeToString(VIR_ERR_NO_MEMORY));
     virRaiseErrorFull(filename, funcname, linenr,
                       domcode, VIR_ERR_NO_MEMORY, VIR_ERR_ERROR,
-                      virerr, NULL, NULL, -1, -1, virerr, NULL);
+                      virerr, NULL, NULL, -1, -1, virerr);
 }
 
 /**
diff --git a/src/util/virterror_internal.h b/src/util/virterror_internal.h
index b8cb279..ffa2d02 100644
--- a/src/util/virterror_internal.h
+++ b/src/util/virterror_internal.h
@@ -44,8 +44,7 @@ void virRaiseErrorFull(const char *filename,
                        const char *str3,
                        int int1,
                        int int2,
-                       const char *fmt, ...)
-    ATTRIBUTE_FMT_PRINTF(12, 13);
+                       const char *msg);
 
 void virReportErrorHelper(int domcode, int errcode,
                           const char *filename,
diff --git a/tests/cpuset b/tests/cpuset
index d638ad6..26f97f0 100755
--- a/tests/cpuset
+++ b/tests/cpuset
@@ -41,7 +41,7 @@ sed "s/vcpu placement='static'>/vcpu cpuset='aaa'>/" xml > xml-invalid || fail=1
 $abs_top_builddir/tools/virsh --connect test:///default define xml-invalid > out 2>&1 && fail=1
 cat <<\EOF > exp || fail=1
 error: Failed to define domain from xml-invalid
-error: internal error topology cpuset syntax error
+error: internal error: topology cpuset syntax error
 
 EOF
 compare exp out || fail=1
diff --git a/tests/undefine b/tests/undefine
index 22d6c14..59771b6 100755
--- a/tests/undefine
+++ b/tests/undefine
@@ -64,7 +64,7 @@ cat <<\EOF > expout || fail=1
 Domain test is being shutdown
 Domain test has been undefined
 error: failed to get domain 'test'
-error: Domain not found
+error: domain not found
 EOF
 compare expout out || fail=1
 
diff --git a/tests/virsh-optparse b/tests/virsh-optparse
index fc2279f..ce8dd87 100755
--- a/tests/virsh-optparse
+++ b/tests/virsh-optparse
@@ -129,7 +129,7 @@ test -s err && fail=1
 
 # Test a required argv
 cat <<\EOF > exp-err || framework_failure
-error: this function is not supported by the connection driver: virDomainQemuMonitorCommand
+error: this function is not supported by the current driver: virDomainQemuMonitorCommand
 EOF
 virsh -q -c $test_url qemu-monitor-command test a >out 2>err && fail=1
 test -s out && fail=1
-- 
1.7.10.1




More information about the libvir-list mailing list