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 bool error_is_set(Error
**errp
)
48 return (errp
&& *errp
);
51 const char *error_get_pretty(Error
*err
)
53 if (err
->msg
== NULL
) {
55 str
= qerror_format(err
->fmt
, err
->obj
);
56 err
->msg
= g_strdup(qstring_get_str(str
));
63 const char *error_get_field(Error
*err
, const char *field
)
65 if (strcmp(field
, "class") == 0) {
66 return qdict_get_str(err
->obj
, field
);
68 QDict
*dict
= qdict_get_qdict(err
->obj
, "data");
69 return qdict_get_str(dict
, field
);
73 QDict
*error_get_data(Error
*err
)
75 QDict
*data
= qdict_get_qdict(err
->obj
, "data");
80 void error_set_field(Error
*err
, const char *field
, const char *value
)
82 QDict
*dict
= qdict_get_qdict(err
->obj
, "data");
83 return qdict_put(dict
, field
, qstring_from_str(value
));
86 void error_free(Error
*err
)
95 bool error_is_type(Error
*err
, const char *fmt
)
97 const char *error_class
;
105 ptr
= strstr(fmt
, "'class': '");
107 ptr
+= strlen("'class': '");
109 end
= strchr(ptr
, '\'');
112 error_class
= error_get_field(err
, "class");
113 if (strlen(error_class
) != end
- ptr
) {
117 return strncmp(ptr
, error_class
, end
- ptr
) == 0;
120 void error_propagate(Error
**dst_err
, Error
*local_err
)
123 *dst_err
= local_err
;
124 } else if (local_err
) {
125 error_free(local_err
);
129 QObject
*error_get_qobject(Error
*err
)
132 return QOBJECT(err
->obj
);
135 void error_set_qobject(Error
**errp
, QObject
*obj
)
141 err
= g_malloc0(sizeof(*err
));
142 err
->obj
= qobject_to_qdict(obj
);