qemu-iotests: s390x: fix test 051
[qemu.git] / include / qapi / error.h
blob6285cf50d1f6ee66c2658817464f4f23bfc3d934
1 /*
2 * QEMU Error Objects
4 * Copyright IBM, Corp. 2011
5 * Copyright (C) 2011-2015 Red Hat, Inc.
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Markus Armbruster <armbru@redhat.com>
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.
16 * Error reporting system loosely patterned after Glib's GError.
18 * Create an error:
19 * error_setg(&err, "situation normal, all fouled up");
21 * Report an error to stderr:
22 * error_report_err(err);
23 * This frees the error object.
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);
30 * Handle an error without reporting it (just for completeness):
31 * error_free(err);
33 * Assert that an expected error occurred, but clean it up without
34 * reporting it (primarily useful in testsuites):
35 * error_free_or_abort(&err);
37 * Pass an existing error to the caller:
38 * error_propagate(errp, err);
39 * where Error **errp is a parameter, by convention the last one.
41 * Create a new error and pass it to the caller:
42 * error_setg(errp, "situation normal, all fouled up");
44 * Call a function and receive an error from it:
45 * Error *err = NULL;
46 * foo(arg, &err);
47 * if (err) {
48 * handle the error...
49 * }
51 * Call a function ignoring errors:
52 * foo(arg, NULL);
54 * Call a function aborting on errors:
55 * foo(arg, &error_abort);
57 * Call a function treating errors as fatal:
58 * foo(arg, &error_fatal);
60 * Receive an error and pass it on to the caller:
61 * Error *err = NULL;
62 * foo(arg, &err);
63 * if (err) {
64 * handle the error...
65 * error_propagate(errp, err);
66 * }
67 * where Error **errp is a parameter, by convention the last one.
69 * Do *not* "optimize" this to
70 * foo(arg, errp);
71 * if (*errp) { // WRONG!
72 * handle the error...
73 * }
74 * because errp may be NULL!
76 * But when all you do with the error is pass it on, please use
77 * foo(arg, errp);
78 * for readability.
81 #ifndef ERROR_H
82 #define ERROR_H
84 #include "qemu/compiler.h"
85 #include "qapi-types.h"
86 #include <stdbool.h>
89 * Opaque error object.
91 typedef struct Error Error;
94 * Overall category of an error.
95 * Based on the qapi type QapiErrorClass, but reproduced here for nicer
96 * enum names.
98 typedef enum ErrorClass {
99 ERROR_CLASS_GENERIC_ERROR = QAPI_ERROR_CLASS_GENERICERROR,
100 ERROR_CLASS_COMMAND_NOT_FOUND = QAPI_ERROR_CLASS_COMMANDNOTFOUND,
101 ERROR_CLASS_DEVICE_ENCRYPTED = QAPI_ERROR_CLASS_DEVICEENCRYPTED,
102 ERROR_CLASS_DEVICE_NOT_ACTIVE = QAPI_ERROR_CLASS_DEVICENOTACTIVE,
103 ERROR_CLASS_DEVICE_NOT_FOUND = QAPI_ERROR_CLASS_DEVICENOTFOUND,
104 ERROR_CLASS_KVM_MISSING_CAP = QAPI_ERROR_CLASS_KVMMISSINGCAP,
105 } ErrorClass;
108 * Get @err's human-readable error message.
110 const char *error_get_pretty(Error *err);
113 * Get @err's error class.
114 * Note: use of error classes other than ERROR_CLASS_GENERIC_ERROR is
115 * strongly discouraged.
117 ErrorClass error_get_class(const Error *err);
120 * Create a new error object and assign it to *@errp.
121 * If @errp is NULL, the error is ignored. Don't bother creating one
122 * then.
123 * If @errp is &error_abort, print a suitable message and abort().
124 * If @errp is &error_fatal, print a suitable message and exit(1).
125 * If @errp is anything else, *@errp must be NULL.
126 * The new error's class is ERROR_CLASS_GENERIC_ERROR, and its
127 * human-readable error message is made from printf-style @fmt, ...
129 #define error_setg(errp, fmt, ...) \
130 error_setg_internal((errp), __FILE__, __LINE__, __func__, \
131 (fmt), ## __VA_ARGS__)
132 void error_setg_internal(Error **errp,
133 const char *src, int line, const char *func,
134 const char *fmt, ...)
135 GCC_FMT_ATTR(5, 6);
138 * Just like error_setg(), with @os_error info added to the message.
139 * If @os_error is non-zero, ": " + strerror(os_error) is appended to
140 * the human-readable error message.
142 #define error_setg_errno(errp, os_error, fmt, ...) \
143 error_setg_errno_internal((errp), __FILE__, __LINE__, __func__, \
144 (os_error), (fmt), ## __VA_ARGS__)
145 void error_setg_errno_internal(Error **errp,
146 const char *fname, int line, const char *func,
147 int os_error, const char *fmt, ...)
148 GCC_FMT_ATTR(6, 7);
150 #ifdef _WIN32
152 * Just like error_setg(), with @win32_error info added to the message.
153 * If @win32_error is non-zero, ": " + g_win32_error_message(win32_err)
154 * is appended to the human-readable error message.
156 #define error_setg_win32(errp, win32_err, fmt, ...) \
157 error_setg_win32_internal((errp), __FILE__, __LINE__, __func__, \
158 (win32_err), (fmt), ## __VA_ARGS__)
159 void error_setg_win32_internal(Error **errp,
160 const char *src, int line, const char *func,
161 int win32_err, const char *fmt, ...)
162 GCC_FMT_ATTR(6, 7);
163 #endif
166 * Propagate error object (if any) from @local_err to @dst_errp.
167 * If @local_err is NULL, do nothing (because there's nothing to
168 * propagate).
169 * Else, if @dst_errp is NULL, errors are being ignored. Free the
170 * error object.
171 * Else, if @dst_errp is &error_abort, print a suitable message and
172 * abort().
173 * Else, if @dst_errp is &error_fatal, print a suitable message and
174 * exit(1).
175 * Else, if @dst_errp already contains an error, ignore this one: free
176 * the error object.
177 * Else, move the error object from @local_err to *@dst_errp.
178 * On return, @local_err is invalid.
180 void error_propagate(Error **dst_errp, Error *local_err);
183 * Append a printf-style human-readable explanation to an existing error.
184 * May be called multiple times, and safe if @errp is NULL.
186 void error_append_hint(Error **errp, const char *fmt, ...)
187 GCC_FMT_ATTR(2, 3);
190 * Convenience function to report open() failure.
192 #define error_setg_file_open(errp, os_errno, filename) \
193 error_setg_file_open_internal((errp), __FILE__, __LINE__, __func__, \
194 (os_errno), (filename))
195 void error_setg_file_open_internal(Error **errp,
196 const char *src, int line, const char *func,
197 int os_errno, const char *filename);
200 * Return an exact copy of @err.
202 Error *error_copy(const Error *err);
205 * Free @err.
206 * @err may be NULL.
208 void error_free(Error *err);
211 * Convenience function to assert that *@errp is set, then silently free it.
213 void error_free_or_abort(Error **errp);
216 * Convenience function to error_report() and free @err.
218 void error_report_err(Error *);
221 * Just like error_setg(), except you get to specify the error class.
222 * Note: use of error classes other than ERROR_CLASS_GENERIC_ERROR is
223 * strongly discouraged.
225 #define error_set(errp, err_class, fmt, ...) \
226 error_set_internal((errp), __FILE__, __LINE__, __func__, \
227 (err_class), (fmt), ## __VA_ARGS__)
228 void error_set_internal(Error **errp,
229 const char *src, int line, const char *func,
230 ErrorClass err_class, const char *fmt, ...)
231 GCC_FMT_ATTR(6, 7);
234 * Pass to error_setg() & friends to abort() on error.
236 extern Error *error_abort;
239 * Pass to error_setg() & friends to exit(1) on error.
241 extern Error *error_fatal;
243 #endif