error.h 9.9 KB
Newer Older
1 2 3 4
/*
 * QEMU Error Objects
 *
 * Copyright IBM, Corp. 2011
5
 * Copyright (C) 2011-2015 Red Hat, Inc.
6 7 8
 *
 * Authors:
 *  Anthony Liguori   <aliguori@us.ibm.com>
9
 *  Markus Armbruster <armbru@redhat.com>
10 11 12 13
 *
 * This work is licensed under the terms of the GNU LGPL, version 2.  See
 * the COPYING.LIB file in the top-level directory.
 */
14 15 16 17 18 19 20

/*
 * Error reporting system loosely patterned after Glib's GError.
 *
 * Create an error:
 *     error_setg(&err, "situation normal, all fouled up");
 *
M
Markus Armbruster 已提交
21 22 23 24 25 26 27 28 29
 * Create an error and add additional explanation:
 *     error_setg(&err, "invalid quark");
 *     error_append_hint(&err, "Valid quarks are up, down, strange, "
 *                       "charm, top, bottom.\n");
 *
 * Do *not* contract this to
 *     error_setg(&err, "invalid quark\n"
 *                "Valid quarks are up, down, strange, charm, top, bottom.");
 *
30
 * Report an error to the current monitor if we have one, else stderr:
31 32 33
 *     error_report_err(err);
 * This frees the error object.
 *
34
 * Likewise, but with additional text prepended:
35 36
 *     error_reportf_err(err, "Could not frobnicate '%s': ", name);
 *
37 38 39 40
 * Report an error somewhere else:
 *     const char *msg = error_get_pretty(err);
 *     do with msg what needs to be done...
 *     error_free(err);
M
Markus Armbruster 已提交
41
 * Note that this loses hints added with error_append_hint().
42 43 44 45
 *
 * Handle an error without reporting it (just for completeness):
 *     error_free(err);
 *
46 47 48 49
 * Assert that an expected error occurred, but clean it up without
 * reporting it (primarily useful in testsuites):
 *     error_free_or_abort(&err);
 *
50 51 52 53
 * Pass an existing error to the caller:
 *     error_propagate(errp, err);
 * where Error **errp is a parameter, by convention the last one.
 *
54 55 56 57
 * Pass an existing error to the caller with the message modified:
 *     error_propagate(errp, err);
 *     error_prepend(errp, "Could not frobnicate '%s': ", name);
 *
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
 * Create a new error and pass it to the caller:
 *     error_setg(errp, "situation normal, all fouled up");
 *
 * Call a function and receive an error from it:
 *     Error *err = NULL;
 *     foo(arg, &err);
 *     if (err) {
 *         handle the error...
 *     }
 *
 * Call a function ignoring errors:
 *     foo(arg, NULL);
 *
 * Call a function aborting on errors:
 *     foo(arg, &error_abort);
 *
M
Markus Armbruster 已提交
74 75 76
 * Call a function treating errors as fatal:
 *     foo(arg, &error_fatal);
 *
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
 * Receive an error and pass it on to the caller:
 *     Error *err = NULL;
 *     foo(arg, &err);
 *     if (err) {
 *         handle the error...
 *         error_propagate(errp, err);
 *     }
 * where Error **errp is a parameter, by convention the last one.
 *
 * Do *not* "optimize" this to
 *     foo(arg, errp);
 *     if (*errp) { // WRONG!
 *         handle the error...
 *     }
 * because errp may be NULL!
 *
 * But when all you do with the error is pass it on, please use
 *     foo(arg, errp);
 * for readability.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
 *
 * Receive and accumulate multiple errors (first one wins):
 *     Error *err = NULL, *local_err = NULL;
 *     foo(arg, &err);
 *     bar(arg, &local_err);
 *     error_propagate(&err, local_err);
 *     if (err) {
 *         handle the error...
 *     }
 *
 * Do *not* "optimize" this to
 *     foo(arg, &err);
 *     bar(arg, &err); // WRONG!
 *     if (err) {
 *         handle the error...
 *     }
 * because this may pass a non-null err to bar().
113 114
 */

115 116 117
#ifndef ERROR_H
#define ERROR_H

118
#include "qapi-types.h"
119

E
Eric Blake 已提交
120 121 122 123 124 125
/*
 * Overall category of an error.
 * Based on the qapi type QapiErrorClass, but reproduced here for nicer
 * enum names.
 */
typedef enum ErrorClass {
126 127 128 129 130 131
    ERROR_CLASS_GENERIC_ERROR = QAPI_ERROR_CLASS_GENERICERROR,
    ERROR_CLASS_COMMAND_NOT_FOUND = QAPI_ERROR_CLASS_COMMANDNOTFOUND,
    ERROR_CLASS_DEVICE_ENCRYPTED = QAPI_ERROR_CLASS_DEVICEENCRYPTED,
    ERROR_CLASS_DEVICE_NOT_ACTIVE = QAPI_ERROR_CLASS_DEVICENOTACTIVE,
    ERROR_CLASS_DEVICE_NOT_FOUND = QAPI_ERROR_CLASS_DEVICENOTFOUND,
    ERROR_CLASS_KVM_MISSING_CAP = QAPI_ERROR_CLASS_KVMMISSINGCAP,
E
Eric Blake 已提交
132 133
} ErrorClass;

134 135
/*
 * Get @err's human-readable error message.
136
 */
137
const char *error_get_pretty(const Error *err);
138

139 140 141 142 143 144 145 146 147 148 149 150
/*
 * Get @err's error class.
 * Note: use of error classes other than ERROR_CLASS_GENERIC_ERROR is
 * strongly discouraged.
 */
ErrorClass error_get_class(const Error *err);

/*
 * Create a new error object and assign it to *@errp.
 * If @errp is NULL, the error is ignored.  Don't bother creating one
 * then.
 * If @errp is &error_abort, print a suitable message and abort().
M
Markus Armbruster 已提交
151
 * If @errp is &error_fatal, print a suitable message and exit(1).
152 153 154
 * If @errp is anything else, *@errp must be NULL.
 * The new error's class is ERROR_CLASS_GENERIC_ERROR, and its
 * human-readable error message is made from printf-style @fmt, ...
M
Markus Armbruster 已提交
155 156
 * The resulting message should be a single phrase, with no newline or
 * trailing punctuation.
157 158 159
 * Please don't error_setg(&error_fatal, ...), use error_report() and
 * exit(), because that's more obvious.
 * Likewise, don't error_setg(&error_abort, ...), use assert().
160
 */
161 162 163 164 165 166 167
#define error_setg(errp, fmt, ...)                              \
    error_setg_internal((errp), __FILE__, __LINE__, __func__,   \
                        (fmt), ## __VA_ARGS__)
void error_setg_internal(Error **errp,
                         const char *src, int line, const char *func,
                         const char *fmt, ...)
    GCC_FMT_ATTR(5, 6);
168 169 170 171 172

/*
 * Just like error_setg(), with @os_error info added to the message.
 * If @os_error is non-zero, ": " + strerror(os_error) is appended to
 * the human-readable error message.
173 174 175
 *
 * The value of errno (which usually can get clobbered by almost any
 * function call) will be preserved.
176
 */
177 178 179 180 181 182 183
#define error_setg_errno(errp, os_error, fmt, ...)                      \
    error_setg_errno_internal((errp), __FILE__, __LINE__, __func__,     \
                              (os_error), (fmt), ## __VA_ARGS__)
void error_setg_errno_internal(Error **errp,
                               const char *fname, int line, const char *func,
                               int os_error, const char *fmt, ...)
    GCC_FMT_ATTR(6, 7);
184

185
#ifdef _WIN32
186 187 188 189
/*
 * Just like error_setg(), with @win32_error info added to the message.
 * If @win32_error is non-zero, ": " + g_win32_error_message(win32_err)
 * is appended to the human-readable error message.
190
 */
191 192 193 194 195 196 197
#define error_setg_win32(errp, win32_err, fmt, ...)                     \
    error_setg_win32_internal((errp), __FILE__, __LINE__, __func__,     \
                              (win32_err), (fmt), ## __VA_ARGS__)
void error_setg_win32_internal(Error **errp,
                               const char *src, int line, const char *func,
                               int win32_err, const char *fmt, ...)
    GCC_FMT_ATTR(6, 7);
198 199
#endif

200 201 202 203 204 205 206 207
/*
 * Propagate error object (if any) from @local_err to @dst_errp.
 * If @local_err is NULL, do nothing (because there's nothing to
 * propagate).
 * Else, if @dst_errp is NULL, errors are being ignored.  Free the
 * error object.
 * Else, if @dst_errp is &error_abort, print a suitable message and
 * abort().
M
Markus Armbruster 已提交
208 209
 * Else, if @dst_errp is &error_fatal, print a suitable message and
 * exit(1).
210 211 212 213
 * Else, if @dst_errp already contains an error, ignore this one: free
 * the error object.
 * Else, move the error object from @local_err to *@dst_errp.
 * On return, @local_err is invalid.
214 215
 * Please don't error_propagate(&error_fatal, ...), use
 * error_report_err() and exit(), because that's more obvious.
L
Luiz Capitulino 已提交
216
 */
217
void error_propagate(Error **dst_errp, Error *local_err);
L
Luiz Capitulino 已提交
218

219 220 221 222 223 224 225 226 227 228 229 230 231 232
/*
 * Prepend some text to @errp's human-readable error message.
 * The text is made by formatting @fmt, @ap like vprintf().
 */
void error_vprepend(Error **errp, const char *fmt, va_list ap);

/*
 * Prepend some text to @errp's human-readable error message.
 * The text is made by formatting @fmt, ... like printf().
 */
void error_prepend(Error **errp, const char *fmt, ...)
    GCC_FMT_ATTR(2, 3);

/*
233
 * Append a printf-style human-readable explanation to an existing error.
M
Markus Armbruster 已提交
234 235 236 237 238
 * @errp may be NULL, but not &error_fatal or &error_abort.
 * Trivially the case if you call it only after error_setg() or
 * error_propagate().
 * May be called multiple times.  The resulting hint should end with a
 * newline.
239 240 241 242
 */
void error_append_hint(Error **errp, const char *fmt, ...)
    GCC_FMT_ATTR(2, 3);

243 244
/*
 * Convenience function to report open() failure.
245
 */
246 247 248 249 250 251
#define error_setg_file_open(errp, os_errno, filename)                  \
    error_setg_file_open_internal((errp), __FILE__, __LINE__, __func__, \
                                  (os_errno), (filename))
void error_setg_file_open_internal(Error **errp,
                                   const char *src, int line, const char *func,
                                   int os_errno, const char *filename);
252

L
Luiz Capitulino 已提交
253
/*
254
 * Return an exact copy of @err.
L
Luiz Capitulino 已提交
255 256 257
 */
Error *error_copy(const Error *err);

258 259 260
/*
 * Free @err.
 * @err may be NULL.
261
 */
262
void error_free(Error *err);
263

264 265 266 267 268
/*
 * Convenience function to assert that *@errp is set, then silently free it.
 */
void error_free_or_abort(Error **errp);

269 270
/*
 * Convenience function to error_report() and free @err.
271
 */
M
Markus Armbruster 已提交
272
void error_report_err(Error *err);
273

274 275 276 277 278 279
/*
 * Convenience function to error_prepend(), error_report() and free @err.
 */
void error_reportf_err(Error *err, const char *fmt, ...)
    GCC_FMT_ATTR(2, 3);

280 281 282 283
/*
 * Just like error_setg(), except you get to specify the error class.
 * Note: use of error classes other than ERROR_CLASS_GENERIC_ERROR is
 * strongly discouraged.
284
 */
285 286 287 288 289 290 291
#define error_set(errp, err_class, fmt, ...)                    \
    error_set_internal((errp), __FILE__, __LINE__, __func__,    \
                       (err_class), (fmt), ## __VA_ARGS__)
void error_set_internal(Error **errp,
                        const char *src, int line, const char *func,
                        ErrorClass err_class, const char *fmt, ...)
    GCC_FMT_ATTR(6, 7);
292

293
/*
294 295
 * Special error destination to abort on error.
 * See error_setg() and error_propagate() for details.
P
Peter Crosthwaite 已提交
296 297 298
 */
extern Error *error_abort;

M
Markus Armbruster 已提交
299
/*
300 301
 * Special error destination to exit(1) on error.
 * See error_setg() and error_propagate() for details.
M
Markus Armbruster 已提交
302 303 304
 */
extern Error *error_fatal;

305
#endif