pseries: Update SLOF firmware image
[qemu.git] / error.c
blob68c0039dbaa399e3a00aa34fe7c11c4f43427236
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 "error_int.h"
16 #include "qemu-objects.h"
17 #include "qerror.h"
19 struct Error
21 QDict *obj;
22 const char *fmt;
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;
35 err = g_malloc0(sizeof(*err));
37 va_start(ap, fmt);
38 err->obj = qobject_to_qdict(qobject_from_jsonv(fmt, &ap));
39 va_end(ap);
40 err->fmt = fmt;
42 *errp = err;
45 bool error_is_set(Error **errp)
47 return (errp && *errp);
50 const char *error_get_pretty(Error *err)
52 if (err->msg == NULL) {
53 QString *str;
54 str = qerror_format(err->fmt, err->obj);
55 err->msg = g_strdup(qstring_get_str(str));
56 QDECREF(str);
59 return err->msg;
62 const char *error_get_field(Error *err, const char *field)
64 if (strcmp(field, "class") == 0) {
65 return qdict_get_str(err->obj, field);
66 } else {
67 QDict *dict = qdict_get_qdict(err->obj, "data");
68 return qdict_get_str(dict, field);
72 QDict *error_get_data(Error *err)
74 QDict *data = qdict_get_qdict(err->obj, "data");
75 QINCREF(data);
76 return data;
79 void error_set_field(Error *err, const char *field, const char *value)
81 QDict *dict = qdict_get_qdict(err->obj, "data");
82 return qdict_put(dict, field, qstring_from_str(value));
85 void error_free(Error *err)
87 if (err) {
88 QDECREF(err->obj);
89 g_free(err->msg);
90 g_free(err);
94 bool error_is_type(Error *err, const char *fmt)
96 const char *error_class;
97 char *ptr;
98 char *end;
100 if (!err) {
101 return false;
104 ptr = strstr(fmt, "'class': '");
105 assert(ptr != NULL);
106 ptr += strlen("'class': '");
108 end = strchr(ptr, '\'');
109 assert(end != NULL);
111 error_class = error_get_field(err, "class");
112 if (strlen(error_class) != end - ptr) {
113 return false;
116 return strncmp(ptr, error_class, end - ptr) == 0;
119 void error_propagate(Error **dst_err, Error *local_err)
121 if (dst_err) {
122 *dst_err = local_err;
123 } else if (local_err) {
124 error_free(local_err);
128 QObject *error_get_qobject(Error *err)
130 QINCREF(err->obj);
131 return QOBJECT(err->obj);
134 void error_set_qobject(Error **errp, QObject *obj)
136 Error *err;
137 if (errp == NULL) {
138 return;
140 err = g_malloc0(sizeof(*err));
141 err->obj = qobject_to_qdict(obj);
142 qobject_incref(obj);
144 *errp = err;