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"
15 #include "error_int.h"
16 #include "qemu-objects.h"
26 void error_set(Error
**errp
, const char *fmt
, ...)
35 err
= g_malloc0(sizeof(*err
));
38 err
->obj
= qobject_to_qdict(qobject_from_jsonv(fmt
, &ap
));
45 bool error_is_set(Error
**errp
)
47 return (errp
&& *errp
);
50 const char *error_get_pretty(Error
*err
)
52 if (err
->msg
== NULL
) {
54 str
= qerror_format(err
->fmt
, err
->obj
);
55 err
->msg
= g_strdup(qstring_get_str(str
));
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
);
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");
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
)
94 bool error_is_type(Error
*err
, const char *fmt
)
96 const char *error_class
;
104 ptr
= strstr(fmt
, "'class': '");
106 ptr
+= strlen("'class': '");
108 end
= strchr(ptr
, '\'');
111 error_class
= error_get_field(err
, "class");
112 if (strlen(error_class
) != end
- ptr
) {
116 return strncmp(ptr
, error_class
, end
- ptr
) == 0;
119 void error_propagate(Error
**dst_err
, Error
*local_err
)
122 *dst_err
= local_err
;
123 } else if (local_err
) {
124 error_free(local_err
);
128 QObject
*error_get_qobject(Error
*err
)
131 return QOBJECT(err
->obj
);
134 void error_set_qobject(Error
**errp
, QObject
*obj
)
140 err
= g_malloc0(sizeof(*err
));
141 err
->obj
= qobject_to_qdict(obj
);