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"
14 #include "qapi/error.h"
15 #include "qapi/qmp/qjson.h"
16 #include "qapi/qmp/qdict.h"
17 #include "qapi-types.h"
18 #include "qapi/qmp/qerror.h"
28 void error_set(Error
**errp
, ErrorClass err_class
, const char *fmt
, ...)
32 int saved_errno
= errno
;
37 assert(*errp
== NULL
);
39 err
= g_malloc0(sizeof(*err
));
42 err
->msg
= g_strdup_vprintf(fmt
, ap
);
44 err
->err_class
= err_class
;
46 if (errp
== &error_abort
) {
47 error_report("%s", error_get_pretty(err
));
56 void error_set_errno(Error
**errp
, int os_errno
, ErrorClass err_class
,
62 int saved_errno
= errno
;
67 assert(*errp
== NULL
);
69 err
= g_malloc0(sizeof(*err
));
72 msg1
= g_strdup_vprintf(fmt
, ap
);
74 err
->msg
= g_strdup_printf("%s: %s", msg1
, strerror(os_errno
));
80 err
->err_class
= err_class
;
82 if (errp
== &error_abort
) {
83 error_report("%s", error_get_pretty(err
));
92 void error_setg_file_open(Error
**errp
, int os_errno
, const char *filename
)
94 error_setg_errno(errp
, os_errno
, "Could not open '%s'", filename
);
99 void error_set_win32(Error
**errp
, int win32_err
, ErrorClass err_class
,
100 const char *fmt
, ...)
109 assert(*errp
== NULL
);
111 err
= g_malloc0(sizeof(*err
));
114 msg1
= g_strdup_vprintf(fmt
, ap
);
115 if (win32_err
!= 0) {
116 char *msg2
= g_win32_error_message(win32_err
);
117 err
->msg
= g_strdup_printf("%s: %s (error: %x)", msg1
, msg2
,
118 (unsigned)win32_err
);
125 err
->err_class
= err_class
;
127 if (errp
== &error_abort
) {
128 error_report("%s", error_get_pretty(err
));
137 Error
*error_copy(const Error
*err
)
141 err_new
= g_malloc0(sizeof(*err
));
142 err_new
->msg
= g_strdup(err
->msg
);
143 err_new
->err_class
= err
->err_class
;
148 bool error_is_set(Error
**errp
)
150 return (errp
&& *errp
);
153 ErrorClass
error_get_class(const Error
*err
)
155 return err
->err_class
;
158 const char *error_get_pretty(Error
*err
)
163 void error_free(Error
*err
)
171 void error_propagate(Error
**dst_err
, Error
*local_err
)
173 if (local_err
&& dst_err
== &error_abort
) {
174 error_report("%s", error_get_pretty(local_err
));
176 } else if (dst_err
&& !*dst_err
) {
177 *dst_err
= local_err
;
178 } else if (local_err
) {
179 error_free(local_err
);