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"
26 void error_set(Error
**errp
, const char *fmt
, ...)
34 assert(*errp
== NULL
);
36 err
= g_malloc0(sizeof(*err
));
39 err
->obj
= qobject_to_qdict(qobject_from_jsonv(fmt
, &ap
));
41 err
->msg
= qerror_format(fmt
, err
->obj
);
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
->obj
= err
->obj
;
53 QINCREF(err_new
->obj
);
58 bool error_is_set(Error
**errp
)
60 return (errp
&& *errp
);
63 const char *error_get_pretty(Error
*err
)
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
);
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");
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
)
100 bool error_is_type(Error
*err
, const char *fmt
)
102 const char *error_class
;
110 ptr
= strstr(fmt
, "'class': '");
112 ptr
+= strlen("'class': '");
114 end
= strchr(ptr
, '\'');
117 error_class
= error_get_field(err
, "class");
118 if (strlen(error_class
) != end
- ptr
) {
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
)
137 return QOBJECT(err
->obj
);
140 void error_set_qobject(Error
**errp
, QObject
*obj
)
146 err
= g_malloc0(sizeof(*err
));
147 err
->obj
= qobject_to_qdict(obj
);