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
, ...)
36 err
= g_malloc0(sizeof(*err
));
39 err
->obj
= qobject_to_qdict(qobject_from_jsonv(fmt
, &ap
));
46 Error
*error_copy(const Error
*err
)
50 err_new
= g_malloc0(sizeof(*err
));
51 err_new
->msg
= g_strdup(err
->msg
);
52 err_new
->fmt
= err
->fmt
;
53 err_new
->obj
= err
->obj
;
54 QINCREF(err_new
->obj
);
59 bool error_is_set(Error
**errp
)
61 return (errp
&& *errp
);
64 const char *error_get_pretty(Error
*err
)
66 if (err
->msg
== NULL
) {
68 str
= qerror_format(err
->fmt
, err
->obj
);
69 err
->msg
= g_strdup(qstring_get_str(str
));
76 const char *error_get_field(Error
*err
, const char *field
)
78 if (strcmp(field
, "class") == 0) {
79 return qdict_get_str(err
->obj
, field
);
81 QDict
*dict
= qdict_get_qdict(err
->obj
, "data");
82 return qdict_get_str(dict
, field
);
86 QDict
*error_get_data(Error
*err
)
88 QDict
*data
= qdict_get_qdict(err
->obj
, "data");
93 void error_set_field(Error
*err
, const char *field
, const char *value
)
95 QDict
*dict
= qdict_get_qdict(err
->obj
, "data");
96 qdict_put(dict
, field
, qstring_from_str(value
));
99 void error_free(Error
*err
)
108 bool error_is_type(Error
*err
, const char *fmt
)
110 const char *error_class
;
118 ptr
= strstr(fmt
, "'class': '");
120 ptr
+= strlen("'class': '");
122 end
= strchr(ptr
, '\'');
125 error_class
= error_get_field(err
, "class");
126 if (strlen(error_class
) != end
- ptr
) {
130 return strncmp(ptr
, error_class
, end
- ptr
) == 0;
133 void error_propagate(Error
**dst_err
, Error
*local_err
)
136 *dst_err
= local_err
;
137 } else if (local_err
) {
138 error_free(local_err
);
142 QObject
*error_get_qobject(Error
*err
)
145 return QOBJECT(err
->obj
);
148 void error_set_qobject(Error
**errp
, QObject
*obj
)
154 err
= g_malloc0(sizeof(*err
));
155 err
->obj
= qobject_to_qdict(obj
);