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"
26 void error_set(Error
**errp
, ErrorClass err_class
, const char *fmt
, ...)
30 int saved_errno
= errno
;
35 assert(*errp
== NULL
);
37 err
= g_malloc0(sizeof(*err
));
40 err
->msg
= g_strdup_vprintf(fmt
, ap
);
42 err
->err_class
= err_class
;
49 void error_set_errno(Error
**errp
, int os_errno
, ErrorClass err_class
,
55 int saved_errno
= errno
;
60 assert(*errp
== NULL
);
62 err
= g_malloc0(sizeof(*err
));
65 msg1
= g_strdup_vprintf(fmt
, ap
);
67 err
->msg
= g_strdup_printf("%s: %s", msg1
, strerror(os_errno
));
73 err
->err_class
= err_class
;
80 void error_setg_file_open(Error
**errp
, int os_errno
, const char *filename
)
82 error_setg_errno(errp
, os_errno
, "Could not open '%s'", filename
);
87 void error_set_win32(Error
**errp
, int win32_err
, ErrorClass err_class
,
97 assert(*errp
== NULL
);
99 err
= g_malloc0(sizeof(*err
));
102 msg1
= g_strdup_vprintf(fmt
, ap
);
103 if (win32_err
!= 0) {
104 char *msg2
= g_win32_error_message(win32_err
);
105 err
->msg
= g_strdup_printf("%s: %s (error: %x)", msg1
, msg2
,
106 (unsigned)win32_err
);
113 err
->err_class
= err_class
;
120 Error
*error_copy(const Error
*err
)
124 err_new
= g_malloc0(sizeof(*err
));
125 err_new
->msg
= g_strdup(err
->msg
);
126 err_new
->err_class
= err
->err_class
;
131 bool error_is_set(Error
**errp
)
133 return (errp
&& *errp
);
136 ErrorClass
error_get_class(const Error
*err
)
138 return err
->err_class
;
141 const char *error_get_pretty(Error
*err
)
146 void error_free(Error
*err
)
154 void error_propagate(Error
**dst_err
, Error
*local_err
)
156 if (dst_err
&& !*dst_err
) {
157 *dst_err
= local_err
;
158 } else if (local_err
) {
159 error_free(local_err
);