4 * Copyright IBM, Corp. 2011
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU LGPL, version 2. See
10 * the COPYING.LIB file in the top-level directory.
13 #include "qemu-common.h"
17 #include "error_int.h"
27 void error_set(Error
**errp
, const char *fmt
, ...)
35 assert(*errp
== NULL
);
37 err
= g_malloc0(sizeof(*err
));
40 err
->obj
= qobject_to_qdict(qobject_from_jsonv(fmt
, &ap
));
47 Error
*error_copy(const Error
*err
)
51 err_new
= g_malloc0(sizeof(*err
));
52 err_new
->msg
= g_strdup(err
->msg
);
53 err_new
->fmt
= err
->fmt
;
54 err_new
->obj
= err
->obj
;
55 QINCREF(err_new
->obj
);
60 bool error_is_set(Error
**errp
)
62 return (errp
&& *errp
);
65 const char *error_get_pretty(Error
*err
)
67 if (err
->msg
== NULL
) {
69 str
= qerror_format(err
->fmt
, err
->obj
);
70 err
->msg
= g_strdup(qstring_get_str(str
));
77 const char *error_get_field(Error
*err
, const char *field
)
79 if (strcmp(field
, "class") == 0) {
80 return qdict_get_str(err
->obj
, field
);
82 QDict
*dict
= qdict_get_qdict(err
->obj
, "data");
83 return qdict_get_str(dict
, field
);
87 QDict
*error_get_data(Error
*err
)
89 QDict
*data
= qdict_get_qdict(err
->obj
, "data");
94 void error_set_field(Error
*err
, const char *field
, const char *value
)
96 QDict
*dict
= qdict_get_qdict(err
->obj
, "data");
97 qdict_put(dict
, field
, qstring_from_str(value
));
100 void error_free(Error
*err
)
109 bool error_is_type(Error
*err
, const char *fmt
)
111 const char *error_class
;
119 ptr
= strstr(fmt
, "'class': '");
121 ptr
+= strlen("'class': '");
123 end
= strchr(ptr
, '\'');
126 error_class
= error_get_field(err
, "class");
127 if (strlen(error_class
) != end
- ptr
) {
131 return strncmp(ptr
, error_class
, end
- ptr
) == 0;
134 void error_propagate(Error
**dst_err
, Error
*local_err
)
136 if (dst_err
&& !*dst_err
) {
137 *dst_err
= local_err
;
138 } else if (local_err
) {
139 error_free(local_err
);
143 QObject
*error_get_qobject(Error
*err
)
146 return QOBJECT(err
->obj
);
149 void error_set_qobject(Error
**errp
, QObject
*obj
)
155 err
= g_malloc0(sizeof(*err
));
156 err
->obj
= qobject_to_qdict(obj
);