blob: 51b63dd4b5289ffaf3a32e44ac2ca76c7f812621 [file] [log] [blame]
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -05001/*
2 * QEMU Error Objects
3 *
4 * Copyright IBM, Corp. 2011
Markus Armbrusteredf6f3b2015-06-19 18:29:24 +02005 * Copyright (C) 2011-2015 Red Hat, Inc.
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -05006 *
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
Markus Armbrusteredf6f3b2015-06-19 18:29:24 +02009 * Markus Armbruster <armbru@redhat.com>
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -050010 *
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 */
Markus Armbrusteredf6f3b2015-06-19 18:29:24 +020014
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 *
Markus Armbrusterf4d00642015-12-18 16:35:08 +010021 * Create an error and add additional explanation:
22 * error_setg(&err, "invalid quark");
23 * error_append_hint(&err, "Valid quarks are up, down, strange, "
24 * "charm, top, bottom.\n");
25 *
26 * Do *not* contract this to
27 * error_setg(&err, "invalid quark\n"
28 * "Valid quarks are up, down, strange, charm, top, bottom.");
29 *
Markus Armbruster10303f02016-02-03 19:03:47 +010030 * Report an error to the current monitor if we have one, else stderr:
Markus Armbrusteredf6f3b2015-06-19 18:29:24 +020031 * error_report_err(err);
32 * This frees the error object.
33 *
Markus Armbruster10303f02016-02-03 19:03:47 +010034 * Likewise, but with additional text prepended:
Markus Armbruster8277d2a2015-12-18 16:35:12 +010035 * error_reportf_err(err, "Could not frobnicate '%s': ", name);
36 *
Markus Armbrusteredf6f3b2015-06-19 18:29:24 +020037 * Report an error somewhere else:
38 * const char *msg = error_get_pretty(err);
39 * do with msg what needs to be done...
40 * error_free(err);
Markus Armbrusterf4d00642015-12-18 16:35:08 +010041 * Note that this loses hints added with error_append_hint().
Markus Armbrusteredf6f3b2015-06-19 18:29:24 +020042 *
43 * Handle an error without reporting it (just for completeness):
44 * error_free(err);
45 *
Eric Blakea12a5a12015-11-05 23:35:31 -070046 * Assert that an expected error occurred, but clean it up without
47 * reporting it (primarily useful in testsuites):
48 * error_free_or_abort(&err);
49 *
Markus Armbrusteredf6f3b2015-06-19 18:29:24 +020050 * Pass an existing error to the caller:
51 * error_propagate(errp, err);
52 * where Error **errp is a parameter, by convention the last one.
53 *
Markus Armbruster8277d2a2015-12-18 16:35:12 +010054 * Pass an existing error to the caller with the message modified:
Markus Armbruster4b576642018-10-17 10:26:25 +020055 * error_propagate_prepend(errp, err);
56 *
57 * Avoid
Markus Armbruster8277d2a2015-12-18 16:35:12 +010058 * error_propagate(errp, err);
59 * error_prepend(errp, "Could not frobnicate '%s': ", name);
Markus Armbruster4b576642018-10-17 10:26:25 +020060 * because this fails to prepend when @errp is &error_fatal.
Markus Armbruster8277d2a2015-12-18 16:35:12 +010061 *
Markus Armbrusteredf6f3b2015-06-19 18:29:24 +020062 * Create a new error and pass it to the caller:
63 * error_setg(errp, "situation normal, all fouled up");
64 *
65 * Call a function and receive an error from it:
66 * Error *err = NULL;
67 * foo(arg, &err);
68 * if (err) {
69 * handle the error...
70 * }
71 *
72 * Call a function ignoring errors:
73 * foo(arg, NULL);
74 *
75 * Call a function aborting on errors:
76 * foo(arg, &error_abort);
77 *
Markus Armbrustera29a37b2015-09-11 16:51:42 +020078 * Call a function treating errors as fatal:
79 * foo(arg, &error_fatal);
80 *
Markus Armbrusteredf6f3b2015-06-19 18:29:24 +020081 * Receive an error and pass it on to the caller:
82 * Error *err = NULL;
83 * foo(arg, &err);
84 * if (err) {
85 * handle the error...
86 * error_propagate(errp, err);
87 * }
88 * where Error **errp is a parameter, by convention the last one.
89 *
90 * Do *not* "optimize" this to
91 * foo(arg, errp);
92 * if (*errp) { // WRONG!
93 * handle the error...
94 * }
95 * because errp may be NULL!
96 *
97 * But when all you do with the error is pass it on, please use
98 * foo(arg, errp);
99 * for readability.
Markus Armbruster8d780f42015-11-17 17:05:49 +0100100 *
101 * Receive and accumulate multiple errors (first one wins):
102 * Error *err = NULL, *local_err = NULL;
103 * foo(arg, &err);
104 * bar(arg, &local_err);
105 * error_propagate(&err, local_err);
106 * if (err) {
107 * handle the error...
108 * }
109 *
110 * Do *not* "optimize" this to
111 * foo(arg, &err);
112 * bar(arg, &err); // WRONG!
113 * if (err) {
114 * handle the error...
115 * }
116 * because this may pass a non-null err to bar().
Markus Armbrusteredf6f3b2015-06-19 18:29:24 +0200117 */
118
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -0500119#ifndef ERROR_H
120#define ERROR_H
121
Markus Armbruster9af23982018-02-11 10:36:01 +0100122#include "qapi/qapi-types-common.h"
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -0500123
Markus Armbrusteredf6f3b2015-06-19 18:29:24 +0200124/*
Eric Blakef22a28b2015-11-18 01:53:00 -0700125 * Overall category of an error.
126 * Based on the qapi type QapiErrorClass, but reproduced here for nicer
127 * enum names.
128 */
129typedef enum ErrorClass {
Eric Blaked20a5802015-11-18 01:53:01 -0700130 ERROR_CLASS_GENERIC_ERROR = QAPI_ERROR_CLASS_GENERICERROR,
131 ERROR_CLASS_COMMAND_NOT_FOUND = QAPI_ERROR_CLASS_COMMANDNOTFOUND,
Eric Blaked20a5802015-11-18 01:53:01 -0700132 ERROR_CLASS_DEVICE_NOT_ACTIVE = QAPI_ERROR_CLASS_DEVICENOTACTIVE,
133 ERROR_CLASS_DEVICE_NOT_FOUND = QAPI_ERROR_CLASS_DEVICENOTFOUND,
134 ERROR_CLASS_KVM_MISSING_CAP = QAPI_ERROR_CLASS_KVMMISSINGCAP,
Eric Blakef22a28b2015-11-18 01:53:00 -0700135} ErrorClass;
136
137/*
Markus Armbrusteredf6f3b2015-06-19 18:29:24 +0200138 * Get @err's human-readable error message.
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -0500139 */
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100140const char *error_get_pretty(const Error *err);
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -0500141
Markus Armbrusteredf6f3b2015-06-19 18:29:24 +0200142/*
143 * Get @err's error class.
144 * Note: use of error classes other than ERROR_CLASS_GENERIC_ERROR is
145 * strongly discouraged.
146 */
147ErrorClass error_get_class(const Error *err);
148
149/*
150 * Create a new error object and assign it to *@errp.
151 * If @errp is NULL, the error is ignored. Don't bother creating one
152 * then.
153 * If @errp is &error_abort, print a suitable message and abort().
Markus Armbrustera29a37b2015-09-11 16:51:42 +0200154 * If @errp is &error_fatal, print a suitable message and exit(1).
Markus Armbrusteredf6f3b2015-06-19 18:29:24 +0200155 * If @errp is anything else, *@errp must be NULL.
156 * The new error's class is ERROR_CLASS_GENERIC_ERROR, and its
157 * human-readable error message is made from printf-style @fmt, ...
Markus Armbrusterf4d00642015-12-18 16:35:08 +0100158 * The resulting message should be a single phrase, with no newline or
159 * trailing punctuation.
Markus Armbruster10303f02016-02-03 19:03:47 +0100160 * Please don't error_setg(&error_fatal, ...), use error_report() and
161 * exit(), because that's more obvious.
162 * Likewise, don't error_setg(&error_abort, ...), use assert().
Markus Armbrusteredf6f3b2015-06-19 18:29:24 +0200163 */
Markus Armbruster1e9b65b2015-06-19 19:21:59 +0200164#define error_setg(errp, fmt, ...) \
165 error_setg_internal((errp), __FILE__, __LINE__, __func__, \
166 (fmt), ## __VA_ARGS__)
167void error_setg_internal(Error **errp,
168 const char *src, int line, const char *func,
169 const char *fmt, ...)
170 GCC_FMT_ATTR(5, 6);
Markus Armbrusteredf6f3b2015-06-19 18:29:24 +0200171
172/*
173 * Just like error_setg(), with @os_error info added to the message.
174 * If @os_error is non-zero, ": " + strerror(os_error) is appended to
175 * the human-readable error message.
Sascha Silbe98cb89a2016-07-27 11:24:26 +0200176 *
177 * The value of errno (which usually can get clobbered by almost any
178 * function call) will be preserved.
Paolo Bonzini680d16d2012-10-02 09:00:45 +0200179 */
Markus Armbruster1e9b65b2015-06-19 19:21:59 +0200180#define error_setg_errno(errp, os_error, fmt, ...) \
181 error_setg_errno_internal((errp), __FILE__, __LINE__, __func__, \
182 (os_error), (fmt), ## __VA_ARGS__)
183void error_setg_errno_internal(Error **errp,
184 const char *fname, int line, const char *func,
185 int os_error, const char *fmt, ...)
186 GCC_FMT_ATTR(6, 7);
Paolo Bonzini680d16d2012-10-02 09:00:45 +0200187
Tomoki Sekiyama20840d42013-08-07 11:40:11 -0400188#ifdef _WIN32
Markus Armbrusteredf6f3b2015-06-19 18:29:24 +0200189/*
190 * Just like error_setg(), with @win32_error info added to the message.
191 * If @win32_error is non-zero, ": " + g_win32_error_message(win32_err)
192 * is appended to the human-readable error message.
Tomoki Sekiyama20840d42013-08-07 11:40:11 -0400193 */
Markus Armbruster1e9b65b2015-06-19 19:21:59 +0200194#define error_setg_win32(errp, win32_err, fmt, ...) \
195 error_setg_win32_internal((errp), __FILE__, __LINE__, __func__, \
196 (win32_err), (fmt), ## __VA_ARGS__)
197void error_setg_win32_internal(Error **errp,
198 const char *src, int line, const char *func,
199 int win32_err, const char *fmt, ...)
200 GCC_FMT_ATTR(6, 7);
Tomoki Sekiyama20840d42013-08-07 11:40:11 -0400201#endif
202
Markus Armbrusteredf6f3b2015-06-19 18:29:24 +0200203/*
204 * Propagate error object (if any) from @local_err to @dst_errp.
205 * If @local_err is NULL, do nothing (because there's nothing to
206 * propagate).
207 * Else, if @dst_errp is NULL, errors are being ignored. Free the
208 * error object.
209 * Else, if @dst_errp is &error_abort, print a suitable message and
210 * abort().
Markus Armbrustera29a37b2015-09-11 16:51:42 +0200211 * Else, if @dst_errp is &error_fatal, print a suitable message and
212 * exit(1).
Markus Armbrusteredf6f3b2015-06-19 18:29:24 +0200213 * Else, if @dst_errp already contains an error, ignore this one: free
214 * the error object.
215 * Else, move the error object from @local_err to *@dst_errp.
216 * On return, @local_err is invalid.
Markus Armbruster10303f02016-02-03 19:03:47 +0100217 * Please don't error_propagate(&error_fatal, ...), use
218 * error_report_err() and exit(), because that's more obvious.
Luiz Capitulino75d789f2012-08-29 11:20:57 -0300219 */
Markus Armbrusteredf6f3b2015-06-19 18:29:24 +0200220void error_propagate(Error **dst_errp, Error *local_err);
Luiz Capitulino75d789f2012-08-29 11:20:57 -0300221
Markus Armbruster4b576642018-10-17 10:26:25 +0200222
223/*
224 * Propagate error object (if any) with some text prepended.
225 * Behaves like
226 * error_prepend(&local_err, fmt, ...);
227 * error_propagate(dst_errp, local_err);
228 */
229void error_propagate_prepend(Error **dst_errp, Error *local_err,
230 const char *fmt, ...);
231
Markus Armbruster8277d2a2015-12-18 16:35:12 +0100232/*
233 * Prepend some text to @errp's human-readable error message.
234 * The text is made by formatting @fmt, @ap like vprintf().
235 */
236void error_vprepend(Error **errp, const char *fmt, va_list ap);
237
238/*
239 * Prepend some text to @errp's human-readable error message.
240 * The text is made by formatting @fmt, ... like printf().
241 */
242void error_prepend(Error **errp, const char *fmt, ...)
243 GCC_FMT_ATTR(2, 3);
244
245/*
Eric Blake50b7b002015-09-10 10:19:16 -0600246 * Append a printf-style human-readable explanation to an existing error.
Markus Armbruster508de472017-08-10 12:06:28 +0200247 * If the error is later reported to a human user with
248 * error_report_err() or warn_report_err(), the hints will be shown,
249 * too. If it's reported via QMP, the hints will be ignored.
250 * Intended use is adding helpful hints on the human user interface,
251 * e.g. a list of valid values. It's not for clarifying a confusing
252 * error message.
Markus Armbrusterf4d00642015-12-18 16:35:08 +0100253 * @errp may be NULL, but not &error_fatal or &error_abort.
254 * Trivially the case if you call it only after error_setg() or
255 * error_propagate().
256 * May be called multiple times. The resulting hint should end with a
257 * newline.
Eric Blake50b7b002015-09-10 10:19:16 -0600258 */
259void error_append_hint(Error **errp, const char *fmt, ...)
260 GCC_FMT_ATTR(2, 3);
261
Markus Armbrusteredf6f3b2015-06-19 18:29:24 +0200262/*
263 * Convenience function to report open() failure.
Luiz Capitulino54028d72013-06-07 14:24:49 -0400264 */
Markus Armbruster1e9b65b2015-06-19 19:21:59 +0200265#define error_setg_file_open(errp, os_errno, filename) \
266 error_setg_file_open_internal((errp), __FILE__, __LINE__, __func__, \
267 (os_errno), (filename))
268void error_setg_file_open_internal(Error **errp,
269 const char *src, int line, const char *func,
270 int os_errno, const char *filename);
Luiz Capitulino54028d72013-06-07 14:24:49 -0400271
Luiz Capitulinoea25fbc2012-08-01 16:29:38 -0300272/*
Markus Armbrusteredf6f3b2015-06-19 18:29:24 +0200273 * Return an exact copy of @err.
Luiz Capitulino79020cf2011-12-05 16:04:05 -0200274 */
275Error *error_copy(const Error *err);
276
Markus Armbrusteredf6f3b2015-06-19 18:29:24 +0200277/*
278 * Free @err.
279 * @err may be NULL.
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -0500280 */
281void error_free(Error *err);
282
Markus Armbrusteredf6f3b2015-06-19 18:29:24 +0200283/*
Eric Blakea12a5a12015-11-05 23:35:31 -0700284 * Convenience function to assert that *@errp is set, then silently free it.
285 */
286void error_free_or_abort(Error **errp);
287
288/*
Alistair Francise43ead12017-07-12 06:57:52 -0700289 * Convenience function to warn_report() and free @err.
Markus Armbruster508de472017-08-10 12:06:28 +0200290 * The report includes hints added with error_append_hint().
Alistair Francise43ead12017-07-12 06:57:52 -0700291 */
292void warn_report_err(Error *err);
293
294/*
Markus Armbrusteredf6f3b2015-06-19 18:29:24 +0200295 * Convenience function to error_report() and free @err.
Markus Armbruster508de472017-08-10 12:06:28 +0200296 * The report includes hints added with error_append_hint().
Peter Crosthwaite5d24ee72014-01-01 18:46:59 -0800297 */
Markus Armbrusterf4d00642015-12-18 16:35:08 +0100298void error_report_err(Error *err);
Peter Crosthwaite5d24ee72014-01-01 18:46:59 -0800299
Markus Armbrusteredf6f3b2015-06-19 18:29:24 +0200300/*
Alistair Francise43ead12017-07-12 06:57:52 -0700301 * Convenience function to error_prepend(), warn_report() and free @err.
302 */
303void warn_reportf_err(Error *err, const char *fmt, ...)
304 GCC_FMT_ATTR(2, 3);
305
306/*
Markus Armbruster8277d2a2015-12-18 16:35:12 +0100307 * Convenience function to error_prepend(), error_report() and free @err.
308 */
309void error_reportf_err(Error *err, const char *fmt, ...)
310 GCC_FMT_ATTR(2, 3);
311
312/*
Markus Armbrusteredf6f3b2015-06-19 18:29:24 +0200313 * Just like error_setg(), except you get to specify the error class.
314 * Note: use of error classes other than ERROR_CLASS_GENERIC_ERROR is
315 * strongly discouraged.
316 */
Markus Armbruster1e9b65b2015-06-19 19:21:59 +0200317#define error_set(errp, err_class, fmt, ...) \
318 error_set_internal((errp), __FILE__, __LINE__, __func__, \
319 (err_class), (fmt), ## __VA_ARGS__)
320void error_set_internal(Error **errp,
321 const char *src, int line, const char *func,
322 ErrorClass err_class, const char *fmt, ...)
323 GCC_FMT_ATTR(6, 7);
Markus Armbrusteredf6f3b2015-06-19 18:29:24 +0200324
325/*
Markus Armbruster10303f02016-02-03 19:03:47 +0100326 * Special error destination to abort on error.
327 * See error_setg() and error_propagate() for details.
Markus Armbrusteredf6f3b2015-06-19 18:29:24 +0200328 */
Peter Crosthwaite5d24ee72014-01-01 18:46:59 -0800329extern Error *error_abort;
330
Markus Armbrustera29a37b2015-09-11 16:51:42 +0200331/*
Markus Armbruster10303f02016-02-03 19:03:47 +0100332 * Special error destination to exit(1) on error.
333 * See error_setg() and error_propagate() for details.
Markus Armbrustera29a37b2015-09-11 16:51:42 +0200334 */
335extern Error *error_fatal;
336
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -0500337#endif