]>
git.proxmox.com Git - qemu.git/blob - error.c
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
;
100 ptr
= strstr(fmt
, "'class': '");
102 ptr
+= strlen("'class': '");
104 end
= strchr(ptr
, '\'');
107 error_class
= error_get_field(err
, "class");
108 if (strlen(error_class
) != end
- ptr
) {
112 return strncmp(ptr
, error_class
, end
- ptr
) == 0;
115 void error_propagate(Error
**dst_err
, Error
*local_err
)
118 *dst_err
= local_err
;
119 } else if (local_err
) {
120 error_free(local_err
);
124 QObject
*error_get_qobject(Error
*err
)
127 return QOBJECT(err
->obj
);
130 void error_set_qobject(Error
**errp
, QObject
*obj
)
136 err
= g_malloc0(sizeof(*err
));
137 err
->obj
= qobject_to_qdict(obj
);