Added two function which will try replace the error if it is already set, so only last error is reported.
Signed-off-by: Wenchao Xia <xiaw...@linux.vnet.ibm.com> --- error.c | 23 +++++++++++++++++++++++ error.h | 9 +++++++++ 2 files changed, 32 insertions(+), 0 deletions(-) diff --git a/error.c b/error.c index 128d88c..5a82f8e 100644 --- a/error.c +++ b/error.c @@ -43,6 +43,29 @@ void error_set(Error **errp, ErrorClass err_class, const char *fmt, ...) *errp = err; } +void error_set_replace(Error **errp, ErrorClass err_class, const char *fmt, ...) +{ + Error *err; + va_list ap; + + if (errp == NULL) { + return; + } + if (*errp != NULL) { + error_free(*errp); + *errp = NULL; + } + + err = g_malloc0(sizeof(*err)); + + va_start(ap, fmt); + err->msg = g_strdup_vprintf(fmt, ap); + va_end(ap); + err->err_class = err_class; + + *errp = err; +} + void error_set_errno(Error **errp, int os_errno, ErrorClass err_class, const char *fmt, ...) { diff --git a/error.h b/error.h index 4d52e73..8039408 100644 --- a/error.h +++ b/error.h @@ -29,6 +29,9 @@ typedef struct Error Error; */ void error_set(Error **err, ErrorClass err_class, const char *fmt, ...) GCC_FMT_ATTR(3, 4); +void error_set_replace(Error **err, ErrorClass err_class, const char *fmt, ...) +GCC_FMT_ATTR(3, 4); + /** * Set an indirect pointer to an error given a ErrorClass value and a * printf-style human message, followed by a strerror() string if @@ -43,6 +46,12 @@ void error_set_errno(Error **err, int os_error, ErrorClass err_class, const char error_set(err, ERROR_CLASS_GENERIC_ERROR, fmt, ## __VA_ARGS__) #define error_setg_errno(err, os_error, fmt, ...) \ error_set_errno(err, os_error, ERROR_CLASS_GENERIC_ERROR, fmt, ## __VA_ARGS__) +#define error_setg_replace(err, fmt, ...) do { \ + if (*err != NULL) { \ + error_free(*err); \ + } \ + error_set(err, ERROR_CLASS_GENERIC_ERROR, fmt, ## __VA_ARGS__); \ +} while (/*CONSTCOND*/0) /** * Returns true if an indirect pointer to an error is pointing to a valid -- 1.7.1