xref: /openbmc/qemu/include/qapi/error.h (revision c11b0583)
1 /*
2  * QEMU Error Objects
3  *
4  * Copyright IBM, Corp. 2011
5  * Copyright (C) 2011-2015 Red Hat, Inc.
6  *
7  * Authors:
8  *  Anthony Liguori   <aliguori@us.ibm.com>
9  *  Markus Armbruster <armbru@redhat.com>
10  *
11  * This work is licensed under the terms of the GNU LGPL, version 2.  See
12  * the COPYING.LIB file in the top-level directory.
13  */
14 
15 /*
16  * Error reporting system loosely patterned after Glib's GError.
17  *
18  * Create an error:
19  *     error_setg(&err, "situation normal, all fouled up");
20  *
21  * Report an error to stderr:
22  *     error_report_err(err);
23  * This frees the error object.
24  *
25  * Report an error somewhere else:
26  *     const char *msg = error_get_pretty(err);
27  *     do with msg what needs to be done...
28  *     error_free(err);
29  *
30  * Handle an error without reporting it (just for completeness):
31  *     error_free(err);
32  *
33  * Pass an existing error to the caller:
34  *     error_propagate(errp, err);
35  * where Error **errp is a parameter, by convention the last one.
36  *
37  * Create a new error and pass it to the caller:
38  *     error_setg(errp, "situation normal, all fouled up");
39  *
40  * Call a function and receive an error from it:
41  *     Error *err = NULL;
42  *     foo(arg, &err);
43  *     if (err) {
44  *         handle the error...
45  *     }
46  *
47  * Call a function ignoring errors:
48  *     foo(arg, NULL);
49  *
50  * Call a function aborting on errors:
51  *     foo(arg, &error_abort);
52  *
53  * Receive an error and pass it on to the caller:
54  *     Error *err = NULL;
55  *     foo(arg, &err);
56  *     if (err) {
57  *         handle the error...
58  *         error_propagate(errp, err);
59  *     }
60  * where Error **errp is a parameter, by convention the last one.
61  *
62  * Do *not* "optimize" this to
63  *     foo(arg, errp);
64  *     if (*errp) { // WRONG!
65  *         handle the error...
66  *     }
67  * because errp may be NULL!
68  *
69  * But when all you do with the error is pass it on, please use
70  *     foo(arg, errp);
71  * for readability.
72  */
73 
74 #ifndef ERROR_H
75 #define ERROR_H
76 
77 #include "qemu/compiler.h"
78 #include "qapi-types.h"
79 #include <stdbool.h>
80 
81 /*
82  * Opaque error object.
83  */
84 typedef struct Error Error;
85 
86 /*
87  * Get @err's human-readable error message.
88  */
89 const char *error_get_pretty(Error *err);
90 
91 /*
92  * Get @err's error class.
93  * Note: use of error classes other than ERROR_CLASS_GENERIC_ERROR is
94  * strongly discouraged.
95  */
96 ErrorClass error_get_class(const Error *err);
97 
98 /*
99  * Create a new error object and assign it to *@errp.
100  * If @errp is NULL, the error is ignored.  Don't bother creating one
101  * then.
102  * If @errp is &error_abort, print a suitable message and abort().
103  * If @errp is anything else, *@errp must be NULL.
104  * The new error's class is ERROR_CLASS_GENERIC_ERROR, and its
105  * human-readable error message is made from printf-style @fmt, ...
106  */
107 #define error_setg(errp, fmt, ...)                              \
108     error_setg_internal((errp), __FILE__, __LINE__, __func__,   \
109                         (fmt), ## __VA_ARGS__)
110 void error_setg_internal(Error **errp,
111                          const char *src, int line, const char *func,
112                          const char *fmt, ...)
113     GCC_FMT_ATTR(5, 6);
114 
115 /*
116  * Just like error_setg(), with @os_error info added to the message.
117  * If @os_error is non-zero, ": " + strerror(os_error) is appended to
118  * the human-readable error message.
119  */
120 #define error_setg_errno(errp, os_error, fmt, ...)                      \
121     error_setg_errno_internal((errp), __FILE__, __LINE__, __func__,     \
122                               (os_error), (fmt), ## __VA_ARGS__)
123 void error_setg_errno_internal(Error **errp,
124                                const char *fname, int line, const char *func,
125                                int os_error, const char *fmt, ...)
126     GCC_FMT_ATTR(6, 7);
127 
128 #ifdef _WIN32
129 /*
130  * Just like error_setg(), with @win32_error info added to the message.
131  * If @win32_error is non-zero, ": " + g_win32_error_message(win32_err)
132  * is appended to the human-readable error message.
133  */
134 #define error_setg_win32(errp, win32_err, fmt, ...)                     \
135     error_setg_win32_internal((errp), __FILE__, __LINE__, __func__,     \
136                               (win32_err), (fmt), ## __VA_ARGS__)
137 void error_setg_win32_internal(Error **errp,
138                                const char *src, int line, const char *func,
139                                int win32_err, const char *fmt, ...)
140     GCC_FMT_ATTR(6, 7);
141 #endif
142 
143 /*
144  * Propagate error object (if any) from @local_err to @dst_errp.
145  * If @local_err is NULL, do nothing (because there's nothing to
146  * propagate).
147  * Else, if @dst_errp is NULL, errors are being ignored.  Free the
148  * error object.
149  * Else, if @dst_errp is &error_abort, print a suitable message and
150  * abort().
151  * Else, if @dst_errp already contains an error, ignore this one: free
152  * the error object.
153  * Else, move the error object from @local_err to *@dst_errp.
154  * On return, @local_err is invalid.
155  */
156 void error_propagate(Error **dst_errp, Error *local_err);
157 
158 /*
159  * Convenience function to report open() failure.
160  */
161 #define error_setg_file_open(errp, os_errno, filename)                  \
162     error_setg_file_open_internal((errp), __FILE__, __LINE__, __func__, \
163                                   (os_errno), (filename))
164 void error_setg_file_open_internal(Error **errp,
165                                    const char *src, int line, const char *func,
166                                    int os_errno, const char *filename);
167 
168 /*
169  * Return an exact copy of @err.
170  */
171 Error *error_copy(const Error *err);
172 
173 /*
174  * Free @err.
175  * @err may be NULL.
176  */
177 void error_free(Error *err);
178 
179 /*
180  * Convenience function to error_report() and free @err.
181  */
182 void error_report_err(Error *);
183 
184 /*
185  * Just like error_setg(), except you get to specify the error class.
186  * Note: use of error classes other than ERROR_CLASS_GENERIC_ERROR is
187  * strongly discouraged.
188  */
189 #define error_set(errp, err_class, fmt, ...)                    \
190     error_set_internal((errp), __FILE__, __LINE__, __func__,    \
191                        (err_class), (fmt), ## __VA_ARGS__)
192 void error_set_internal(Error **errp,
193                         const char *src, int line, const char *func,
194                         ErrorClass err_class, const char *fmt, ...)
195     GCC_FMT_ATTR(6, 7);
196 
197 /*
198  * Pass to error_setg() & friends to abort() on error.
199  */
200 extern Error *error_abort;
201 
202 #endif
203