2011-06-02 01:14:49 +08:00
|
|
|
/*
|
|
|
|
* QEMU Error Objects
|
|
|
|
*
|
|
|
|
* Copyright IBM, Corp. 2011
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Anthony Liguori <aliguori@us.ibm.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU LGPL, version 2. See
|
|
|
|
* the COPYING.LIB file in the top-level directory.
|
|
|
|
*/
|
2011-06-14 05:01:53 +08:00
|
|
|
|
|
|
|
#include "qemu-common.h"
|
2011-06-02 01:14:49 +08:00
|
|
|
#include "error.h"
|
2011-10-22 02:05:43 +08:00
|
|
|
#include "qjson.h"
|
|
|
|
#include "qdict.h"
|
2012-07-28 01:09:29 +08:00
|
|
|
#include "qapi-types.h"
|
2011-06-02 01:14:49 +08:00
|
|
|
#include "qerror.h"
|
|
|
|
|
|
|
|
struct Error
|
|
|
|
{
|
|
|
|
QDict *obj;
|
|
|
|
char *msg;
|
2012-07-28 01:09:29 +08:00
|
|
|
ErrorClass err_class;
|
2011-06-02 01:14:49 +08:00
|
|
|
};
|
|
|
|
|
2012-07-28 01:09:29 +08:00
|
|
|
void error_set(Error **errp, ErrorClass err_class, const char *fmt, ...)
|
2011-06-02 01:14:49 +08:00
|
|
|
{
|
|
|
|
Error *err;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
if (errp == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2012-07-17 22:17:04 +08:00
|
|
|
assert(*errp == NULL);
|
2011-06-02 01:14:49 +08:00
|
|
|
|
2011-08-21 11:09:37 +08:00
|
|
|
err = g_malloc0(sizeof(*err));
|
2011-06-02 01:14:49 +08:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
err->obj = qobject_to_qdict(qobject_from_jsonv(fmt, &ap));
|
|
|
|
va_end(ap);
|
2012-07-21 00:43:37 +08:00
|
|
|
err->msg = qerror_format(fmt, err->obj);
|
2012-07-28 01:09:29 +08:00
|
|
|
err->err_class = err_class;
|
2011-06-02 01:14:49 +08:00
|
|
|
|
|
|
|
*errp = err;
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:04:05 +08:00
|
|
|
Error *error_copy(const Error *err)
|
|
|
|
{
|
|
|
|
Error *err_new;
|
|
|
|
|
|
|
|
err_new = g_malloc0(sizeof(*err));
|
|
|
|
err_new->msg = g_strdup(err->msg);
|
2012-07-28 01:09:29 +08:00
|
|
|
err_new->err_class = err->err_class;
|
2011-12-06 02:04:05 +08:00
|
|
|
err_new->obj = err->obj;
|
|
|
|
QINCREF(err_new->obj);
|
|
|
|
|
|
|
|
return err_new;
|
|
|
|
}
|
|
|
|
|
2011-06-02 01:14:49 +08:00
|
|
|
bool error_is_set(Error **errp)
|
|
|
|
{
|
|
|
|
return (errp && *errp);
|
|
|
|
}
|
|
|
|
|
2012-08-02 03:29:38 +08:00
|
|
|
ErrorClass error_get_class(const Error *err)
|
|
|
|
{
|
|
|
|
return err->err_class;
|
|
|
|
}
|
|
|
|
|
2011-06-02 01:14:49 +08:00
|
|
|
const char *error_get_pretty(Error *err)
|
|
|
|
{
|
|
|
|
return err->msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
void error_free(Error *err)
|
|
|
|
{
|
|
|
|
if (err) {
|
|
|
|
QDECREF(err->obj);
|
2011-08-21 11:09:37 +08:00
|
|
|
g_free(err->msg);
|
|
|
|
g_free(err);
|
2011-06-02 01:14:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void error_propagate(Error **dst_err, Error *local_err)
|
|
|
|
{
|
2012-07-17 22:17:04 +08:00
|
|
|
if (dst_err && !*dst_err) {
|
2011-06-02 01:14:49 +08:00
|
|
|
*dst_err = local_err;
|
|
|
|
} else if (local_err) {
|
|
|
|
error_free(local_err);
|
|
|
|
}
|
|
|
|
}
|