error: Fix examples in error.h's big comment
[qemu.git] / include / qapi / error.h
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 * 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" // WRONG!
28 * "Valid quarks are up, down, strange, charm, top, bottom.");
29 *
30 * Report an error to the current monitor if we have one, else stderr:
31 * error_report_err(err);
32 * This frees the error object.
33 *
34 * Likewise, but with additional text prepended:
35 * error_reportf_err(err, "Could not frobnicate '%s': ", name);
36 *
37 * 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);
41 * Note that this loses hints added with error_append_hint().
42 *
43 * Handle an error without reporting it (just for completeness):
44 * error_free(err);
45 *
46 * 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 *
50 * 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 *
54 * Pass an existing error to the caller with the message modified:
55 * error_propagate_prepend(errp, err,
56 * "Could not frobnicate '%s': ", name);
57 *
58 * Avoid
59 * error_propagate(errp, err);
60 * error_prepend(errp, "Could not frobnicate '%s': ", name);
61 * because this fails to prepend when @errp is &error_fatal.
62 *
63 * Create a new error and pass it to the caller:
64 * error_setg(errp, "situation normal, all fouled up");
65 *
66 * Call a function and receive an error from it:
67 * Error *err = NULL;
68 * foo(arg, &err);
69 * if (err) {
70 * handle the error...
71 * }
72 *
73 * Call a function ignoring errors:
74 * foo(arg, NULL);
75 *
76 * Call a function aborting on errors:
77 * foo(arg, &error_abort);
78 *
79 * Call a function treating errors as fatal:
80 * foo(arg, &error_fatal);
81 *
82 * Receive an error and pass it on to the caller:
83 * Error *err = NULL;
84 * foo(arg, &err);
85 * if (err) {
86 * handle the error...
87 * error_propagate(errp, err);
88 * }
89 * where Error **errp is a parameter, by convention the last one.
90 *
91 * Do *not* "optimize" this to
92 * foo(arg, errp);
93 * if (*errp) { // WRONG!
94 * handle the error...
95 * }
96 * because errp may be NULL!
97 *
98 * But when all you do with the error is pass it on, please use
99 * foo(arg, errp);
100 * for readability.
101 *
102 * Receive and accumulate multiple errors (first one wins):
103 * Error *err = NULL, *local_err = NULL;
104 * foo(arg, &err);
105 * bar(arg, &local_err);
106 * error_propagate(&err, local_err);
107 * if (err) {
108 * handle the error...
109 * }
110 *
111 * Do *not* "optimize" this to
112 * Error *err = NULL;
113 * foo(arg, &err);
114 * bar(arg, &err); // WRONG!
115 * if (err) {
116 * handle the error...
117 * }
118 * because this may pass a non-null err to bar().
119 *
120 * Likewise, do *not*
121 * Error *err = NULL;
122 * if (cond1) {
123 * error_setg(&err, ...);
124 * }
125 * if (cond2) {
126 * error_setg(&err, ...); // WRONG!
127 * }
128 * because this may pass a non-null err to error_setg().
129 */
130
131 #ifndef ERROR_H
132 #define ERROR_H
133
134 #include "qapi/qapi-types-error.h"
135
136 /*
137 * Overall category of an error.
138 * Based on the qapi type QapiErrorClass, but reproduced here for nicer
139 * enum names.
140 */
141 typedef enum ErrorClass {
142 ERROR_CLASS_GENERIC_ERROR = QAPI_ERROR_CLASS_GENERICERROR,
143 ERROR_CLASS_COMMAND_NOT_FOUND = QAPI_ERROR_CLASS_COMMANDNOTFOUND,
144 ERROR_CLASS_DEVICE_NOT_ACTIVE = QAPI_ERROR_CLASS_DEVICENOTACTIVE,
145 ERROR_CLASS_DEVICE_NOT_FOUND = QAPI_ERROR_CLASS_DEVICENOTFOUND,
146 ERROR_CLASS_KVM_MISSING_CAP = QAPI_ERROR_CLASS_KVMMISSINGCAP,
147 } ErrorClass;
148
149 /*
150 * Get @err's human-readable error message.
151 */
152 const char *error_get_pretty(const Error *err);
153
154 /*
155 * Get @err's error class.
156 * Note: use of error classes other than ERROR_CLASS_GENERIC_ERROR is
157 * strongly discouraged.
158 */
159 ErrorClass error_get_class(const Error *err);
160
161 /*
162 * Create a new error object and assign it to *@errp.
163 * If @errp is NULL, the error is ignored. Don't bother creating one
164 * then.
165 * If @errp is &error_abort, print a suitable message and abort().
166 * If @errp is &error_fatal, print a suitable message and exit(1).
167 * If @errp is anything else, *@errp must be NULL.
168 * The new error's class is ERROR_CLASS_GENERIC_ERROR, and its
169 * human-readable error message is made from printf-style @fmt, ...
170 * The resulting message should be a single phrase, with no newline or
171 * trailing punctuation.
172 * Please don't error_setg(&error_fatal, ...), use error_report() and
173 * exit(), because that's more obvious.
174 * Likewise, don't error_setg(&error_abort, ...), use assert().
175 */
176 #define error_setg(errp, fmt, ...) \
177 error_setg_internal((errp), __FILE__, __LINE__, __func__, \
178 (fmt), ## __VA_ARGS__)
179 void error_setg_internal(Error **errp,
180 const char *src, int line, const char *func,
181 const char *fmt, ...)
182 GCC_FMT_ATTR(5, 6);
183
184 /*
185 * Just like error_setg(), with @os_error info added to the message.
186 * If @os_error is non-zero, ": " + strerror(os_error) is appended to
187 * the human-readable error message.
188 *
189 * The value of errno (which usually can get clobbered by almost any
190 * function call) will be preserved.
191 */
192 #define error_setg_errno(errp, os_error, fmt, ...) \
193 error_setg_errno_internal((errp), __FILE__, __LINE__, __func__, \
194 (os_error), (fmt), ## __VA_ARGS__)
195 void error_setg_errno_internal(Error **errp,
196 const char *fname, int line, const char *func,
197 int os_error, const char *fmt, ...)
198 GCC_FMT_ATTR(6, 7);
199
200 #ifdef _WIN32
201 /*
202 * Just like error_setg(), with @win32_error info added to the message.
203 * If @win32_error is non-zero, ": " + g_win32_error_message(win32_err)
204 * is appended to the human-readable error message.
205 */
206 #define error_setg_win32(errp, win32_err, fmt, ...) \
207 error_setg_win32_internal((errp), __FILE__, __LINE__, __func__, \
208 (win32_err), (fmt), ## __VA_ARGS__)
209 void error_setg_win32_internal(Error **errp,
210 const char *src, int line, const char *func,
211 int win32_err, const char *fmt, ...)
212 GCC_FMT_ATTR(6, 7);
213 #endif
214
215 /*
216 * Propagate error object (if any) from @local_err to @dst_errp.
217 * If @local_err is NULL, do nothing (because there's nothing to
218 * propagate).
219 * Else, if @dst_errp is NULL, errors are being ignored. Free the
220 * error object.
221 * Else, if @dst_errp is &error_abort, print a suitable message and
222 * abort().
223 * Else, if @dst_errp is &error_fatal, print a suitable message and
224 * exit(1).
225 * Else, if @dst_errp already contains an error, ignore this one: free
226 * the error object.
227 * Else, move the error object from @local_err to *@dst_errp.
228 * On return, @local_err is invalid.
229 * Please don't error_propagate(&error_fatal, ...), use
230 * error_report_err() and exit(), because that's more obvious.
231 */
232 void error_propagate(Error **dst_errp, Error *local_err);
233
234
235 /*
236 * Propagate error object (if any) with some text prepended.
237 * Behaves like
238 * error_prepend(&local_err, fmt, ...);
239 * error_propagate(dst_errp, local_err);
240 */
241 void error_propagate_prepend(Error **dst_errp, Error *local_err,
242 const char *fmt, ...);
243
244 /*
245 * Prepend some text to @errp's human-readable error message.
246 * The text is made by formatting @fmt, @ap like vprintf().
247 */
248 void error_vprepend(Error *const *errp, const char *fmt, va_list ap);
249
250 /*
251 * Prepend some text to @errp's human-readable error message.
252 * The text is made by formatting @fmt, ... like printf().
253 */
254 void error_prepend(Error *const *errp, const char *fmt, ...)
255 GCC_FMT_ATTR(2, 3);
256
257 /*
258 * Append a printf-style human-readable explanation to an existing error.
259 * If the error is later reported to a human user with
260 * error_report_err() or warn_report_err(), the hints will be shown,
261 * too. If it's reported via QMP, the hints will be ignored.
262 * Intended use is adding helpful hints on the human user interface,
263 * e.g. a list of valid values. It's not for clarifying a confusing
264 * error message.
265 * @errp may be NULL, but not &error_fatal or &error_abort.
266 * Trivially the case if you call it only after error_setg() or
267 * error_propagate().
268 * May be called multiple times. The resulting hint should end with a
269 * newline.
270 */
271 void error_append_hint(Error *const *errp, const char *fmt, ...)
272 GCC_FMT_ATTR(2, 3);
273
274 /*
275 * Convenience function to report open() failure.
276 */
277 #define error_setg_file_open(errp, os_errno, filename) \
278 error_setg_file_open_internal((errp), __FILE__, __LINE__, __func__, \
279 (os_errno), (filename))
280 void error_setg_file_open_internal(Error **errp,
281 const char *src, int line, const char *func,
282 int os_errno, const char *filename);
283
284 /*
285 * Return an exact copy of @err.
286 */
287 Error *error_copy(const Error *err);
288
289 /*
290 * Free @err.
291 * @err may be NULL.
292 */
293 void error_free(Error *err);
294
295 /*
296 * Convenience function to assert that *@errp is set, then silently free it.
297 */
298 void error_free_or_abort(Error **errp);
299
300 /*
301 * Convenience function to warn_report() and free @err.
302 * The report includes hints added with error_append_hint().
303 */
304 void warn_report_err(Error *err);
305
306 /*
307 * Convenience function to error_report() and free @err.
308 * The report includes hints added with error_append_hint().
309 */
310 void error_report_err(Error *err);
311
312 /*
313 * Convenience function to error_prepend(), warn_report() and free @err.
314 */
315 void warn_reportf_err(Error *err, const char *fmt, ...)
316 GCC_FMT_ATTR(2, 3);
317
318 /*
319 * Convenience function to error_prepend(), error_report() and free @err.
320 */
321 void error_reportf_err(Error *err, const char *fmt, ...)
322 GCC_FMT_ATTR(2, 3);
323
324 /*
325 * Just like error_setg(), except you get to specify the error class.
326 * Note: use of error classes other than ERROR_CLASS_GENERIC_ERROR is
327 * strongly discouraged.
328 */
329 #define error_set(errp, err_class, fmt, ...) \
330 error_set_internal((errp), __FILE__, __LINE__, __func__, \
331 (err_class), (fmt), ## __VA_ARGS__)
332 void error_set_internal(Error **errp,
333 const char *src, int line, const char *func,
334 ErrorClass err_class, const char *fmt, ...)
335 GCC_FMT_ATTR(6, 7);
336
337 /*
338 * Special error destination to abort on error.
339 * See error_setg() and error_propagate() for details.
340 */
341 extern Error *error_abort;
342
343 /*
344 * Special error destination to exit(1) on error.
345 * See error_setg() and error_propagate() for details.
346 */
347 extern Error *error_fatal;
348
349 #endif