hmp_change(): don't access DeviceEncrypted's data
[qemu/ar7.git] / error.c
blob2ade99b0808ac4eb00b27ca1fa456fe05cc248f7
1 /*
2 * QEMU Error Objects
4 * Copyright IBM, Corp. 2011
6 * Authors:
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"
14 #include "error.h"
15 #include "qjson.h"
16 #include "qdict.h"
17 #include "error_int.h"
18 #include "qerror.h"
20 struct Error
22 QDict *obj;
23 char *msg;
26 void error_set(Error **errp, const char *fmt, ...)
28 Error *err;
29 va_list ap;
31 if (errp == NULL) {
32 return;
34 assert(*errp == NULL);
36 err = g_malloc0(sizeof(*err));
38 va_start(ap, fmt);
39 err->obj = qobject_to_qdict(qobject_from_jsonv(fmt, &ap));
40 va_end(ap);
41 err->msg = qerror_format(fmt, err->obj);
43 *errp = err;
46 Error *error_copy(const Error *err)
48 Error *err_new;
50 err_new = g_malloc0(sizeof(*err));
51 err_new->msg = g_strdup(err->msg);
52 err_new->obj = err->obj;
53 QINCREF(err_new->obj);
55 return err_new;
58 bool error_is_set(Error **errp)
60 return (errp && *errp);
63 const char *error_get_pretty(Error *err)
65 return err->msg;
68 const char *error_get_field(Error *err, const char *field)
70 if (strcmp(field, "class") == 0) {
71 return qdict_get_str(err->obj, field);
72 } else {
73 QDict *dict = qdict_get_qdict(err->obj, "data");
74 return qdict_get_str(dict, field);
78 QDict *error_get_data(Error *err)
80 QDict *data = qdict_get_qdict(err->obj, "data");
81 QINCREF(data);
82 return data;
85 void error_set_field(Error *err, const char *field, const char *value)
87 QDict *dict = qdict_get_qdict(err->obj, "data");
88 qdict_put(dict, field, qstring_from_str(value));
91 void error_free(Error *err)
93 if (err) {
94 QDECREF(err->obj);
95 g_free(err->msg);
96 g_free(err);
100 bool error_is_type(Error *err, const char *fmt)
102 const char *error_class;
103 char *ptr;
104 char *end;
106 if (!err) {
107 return false;
110 ptr = strstr(fmt, "'class': '");
111 assert(ptr != NULL);
112 ptr += strlen("'class': '");
114 end = strchr(ptr, '\'');
115 assert(end != NULL);
117 error_class = error_get_field(err, "class");
118 if (strlen(error_class) != end - ptr) {
119 return false;
122 return strncmp(ptr, error_class, end - ptr) == 0;
125 void error_propagate(Error **dst_err, Error *local_err)
127 if (dst_err && !*dst_err) {
128 *dst_err = local_err;
129 } else if (local_err) {
130 error_free(local_err);
134 QObject *error_get_qobject(Error *err)
136 QINCREF(err->obj);
137 return QOBJECT(err->obj);
140 void error_set_qobject(Error **errp, QObject *obj)
142 Error *err;
143 if (errp == NULL) {
144 return;
146 err = g_malloc0(sizeof(*err));
147 err->obj = qobject_to_qdict(obj);
148 qobject_incref(obj);
150 *errp = err;