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 "error_int.h"
14 #include "qemu-objects.h"
25 void error_set(Error
**errp
, const char *fmt
, ...)
34 err
= qemu_mallocz(sizeof(*err
));
37 err
->obj
= qobject_to_qdict(qobject_from_jsonv(fmt
, &ap
));
44 bool error_is_set(Error
**errp
)
46 return (errp
&& *errp
);
49 const char *error_get_pretty(Error
*err
)
51 if (err
->msg
== NULL
) {
53 str
= qerror_format(err
->fmt
, err
->obj
);
54 err
->msg
= qemu_strdup(qstring_get_str(str
));
61 const char *error_get_field(Error
*err
, const char *field
)
63 if (strcmp(field
, "class") == 0) {
64 return qdict_get_str(err
->obj
, field
);
66 QDict
*dict
= qdict_get_qdict(err
->obj
, "data");
67 return qdict_get_str(dict
, field
);
71 QDict
*error_get_data(Error
*err
)
73 QDict
*data
= qdict_get_qdict(err
->obj
, "data");
78 void error_set_field(Error
*err
, const char *field
, const char *value
)
80 QDict
*dict
= qdict_get_qdict(err
->obj
, "data");
81 return qdict_put(dict
, field
, qstring_from_str(value
));
84 void error_free(Error
*err
)
93 bool error_is_type(Error
*err
, const char *fmt
)
95 const char *error_class
;
99 ptr
= strstr(fmt
, "'class': '");
101 ptr
+= strlen("'class': '");
103 end
= strchr(ptr
, '\'');
106 error_class
= error_get_field(err
, "class");
107 if (strlen(error_class
) != end
- ptr
) {
111 return strncmp(ptr
, error_class
, end
- ptr
) == 0;
114 void error_propagate(Error
**dst_err
, Error
*local_err
)
117 *dst_err
= local_err
;
118 } else if (local_err
) {
119 error_free(local_err
);
123 QObject
*error_get_qobject(Error
*err
)
126 return QOBJECT(err
->obj
);
129 void error_set_qobject(Error
**errp
, QObject
*obj
)
135 err
= qemu_mallocz(sizeof(*err
));
136 err
->obj
= qobject_to_qdict(obj
);