blob: ec0faa61760bba7b7595d810a07cceb6ab219401 [file] [log] [blame]
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -05001/*
2 * QEMU Error Objects
3 *
4 * Copyright IBM, Corp. 2011
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
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.
11 */
Stefan Weile4ea5e22011-06-13 23:01:53 +020012
13#include "qemu-common.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010014#include "qapi/error.h"
15#include "qapi/qmp/qjson.h"
16#include "qapi/qmp/qdict.h"
Luiz Capitulino13f59ae2012-07-27 14:09:29 -030017#include "qapi-types.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010018#include "qapi/qmp/qerror.h"
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -050019
20struct Error
21{
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -050022 char *msg;
Luiz Capitulino13f59ae2012-07-27 14:09:29 -030023 ErrorClass err_class;
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -050024};
25
Luiz Capitulino13f59ae2012-07-27 14:09:29 -030026void error_set(Error **errp, ErrorClass err_class, const char *fmt, ...)
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -050027{
28 Error *err;
29 va_list ap;
30
31 if (errp == NULL) {
32 return;
33 }
Paolo Bonzinid1953252012-07-17 16:17:04 +020034 assert(*errp == NULL);
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -050035
Anthony Liguori7267c092011-08-20 22:09:37 -050036 err = g_malloc0(sizeof(*err));
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -050037
38 va_start(ap, fmt);
Luiz Capitulinodf1e6082012-07-27 17:51:03 -030039 err->msg = g_strdup_vprintf(fmt, ap);
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -050040 va_end(ap);
Luiz Capitulino13f59ae2012-07-27 14:09:29 -030041 err->err_class = err_class;
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -050042
43 *errp = err;
44}
45
Paolo Bonzini680d16d2012-10-02 09:00:45 +020046void error_set_errno(Error **errp, int os_errno, ErrorClass err_class,
47 const char *fmt, ...)
48{
49 Error *err;
50 char *msg1;
51 va_list ap;
52
53 if (errp == NULL) {
54 return;
55 }
56 assert(*errp == NULL);
57
58 err = g_malloc0(sizeof(*err));
59
60 va_start(ap, fmt);
61 msg1 = g_strdup_vprintf(fmt, ap);
62 if (os_errno != 0) {
63 err->msg = g_strdup_printf("%s: %s", msg1, strerror(os_errno));
64 g_free(msg1);
65 } else {
66 err->msg = msg1;
67 }
68 va_end(ap);
69 err->err_class = err_class;
70
71 *errp = err;
72}
73
Luiz Capitulino54028d72013-06-07 14:24:49 -040074void error_setg_file_open(Error **errp, int os_errno, const char *filename)
75{
76 error_setg_errno(errp, os_errno, "Could not open '%s'", filename);
77}
78
Tomoki Sekiyama20840d42013-08-07 11:40:11 -040079#ifdef _WIN32
80
81void error_set_win32(Error **errp, int win32_err, ErrorClass err_class,
82 const char *fmt, ...)
83{
84 Error *err;
85 char *msg1;
86 va_list ap;
87
88 if (errp == NULL) {
89 return;
90 }
91 assert(*errp == NULL);
92
93 err = g_malloc0(sizeof(*err));
94
95 va_start(ap, fmt);
96 msg1 = g_strdup_vprintf(fmt, ap);
97 if (win32_err != 0) {
98 char *msg2 = g_win32_error_message(win32_err);
99 err->msg = g_strdup_printf("%s: %s (error: %x)", msg1, msg2,
100 (unsigned)win32_err);
101 g_free(msg2);
102 g_free(msg1);
103 } else {
104 err->msg = msg1;
105 }
106 va_end(ap);
107 err->err_class = err_class;
108
109 *errp = err;
110}
111
112#endif
113
Luiz Capitulino79020cf2011-12-05 16:04:05 -0200114Error *error_copy(const Error *err)
115{
116 Error *err_new;
117
118 err_new = g_malloc0(sizeof(*err));
119 err_new->msg = g_strdup(err->msg);
Luiz Capitulino13f59ae2012-07-27 14:09:29 -0300120 err_new->err_class = err->err_class;
Luiz Capitulino79020cf2011-12-05 16:04:05 -0200121
122 return err_new;
123}
124
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -0500125bool error_is_set(Error **errp)
126{
127 return (errp && *errp);
128}
129
Luiz Capitulinoea25fbc2012-08-01 16:29:38 -0300130ErrorClass error_get_class(const Error *err)
131{
132 return err->err_class;
133}
134
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -0500135const char *error_get_pretty(Error *err)
136{
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -0500137 return err->msg;
138}
139
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -0500140void error_free(Error *err)
141{
142 if (err) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500143 g_free(err->msg);
144 g_free(err);
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -0500145 }
146}
147
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -0500148void error_propagate(Error **dst_err, Error *local_err)
149{
Paolo Bonzinid1953252012-07-17 16:17:04 +0200150 if (dst_err && !*dst_err) {
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -0500151 *dst_err = local_err;
152 } else if (local_err) {
153 error_free(local_err);
154 }
155}