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 * Assert that an expected error occurred, but clean it up without 34 * reporting it (primarily useful in testsuites): 35 * error_free_or_abort(&err); 36 * 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. 40 * 41 * Create a new error and pass it to the caller: 42 * error_setg(errp, "situation normal, all fouled up"); 43 * 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 * } 50 * 51 * Call a function ignoring errors: 52 * foo(arg, NULL); 53 * 54 * Call a function aborting on errors: 55 * foo(arg, &error_abort); 56 * 57 * Call a function treating errors as fatal: 58 * foo(arg, &error_fatal); 59 * 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. 68 * 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! 75 * 76 * But when all you do with the error is pass it on, please use 77 * foo(arg, errp); 78 * for readability. 79 */ 80 81 #ifndef ERROR_H 82 #define ERROR_H 83 84 #include "qemu/compiler.h" 85 #include "qapi-types.h" 86 #include <stdbool.h> 87 88 /* 89 * Opaque error object. 90 */ 91 typedef struct Error Error; 92 93 /* 94 * Get @err's human-readable error message. 95 */ 96 const char *error_get_pretty(Error *err); 97 98 /* 99 * Get @err's error class. 100 * Note: use of error classes other than ERROR_CLASS_GENERIC_ERROR is 101 * strongly discouraged. 102 */ 103 ErrorClass error_get_class(const Error *err); 104 105 /* 106 * Create a new error object and assign it to *@errp. 107 * If @errp is NULL, the error is ignored. Don't bother creating one 108 * then. 109 * If @errp is &error_abort, print a suitable message and abort(). 110 * If @errp is &error_fatal, print a suitable message and exit(1). 111 * If @errp is anything else, *@errp must be NULL. 112 * The new error's class is ERROR_CLASS_GENERIC_ERROR, and its 113 * human-readable error message is made from printf-style @fmt, ... 114 */ 115 #define error_setg(errp, fmt, ...) \ 116 error_setg_internal((errp), __FILE__, __LINE__, __func__, \ 117 (fmt), ## __VA_ARGS__) 118 void error_setg_internal(Error **errp, 119 const char *src, int line, const char *func, 120 const char *fmt, ...) 121 GCC_FMT_ATTR(5, 6); 122 123 /* 124 * Just like error_setg(), with @os_error info added to the message. 125 * If @os_error is non-zero, ": " + strerror(os_error) is appended to 126 * the human-readable error message. 127 */ 128 #define error_setg_errno(errp, os_error, fmt, ...) \ 129 error_setg_errno_internal((errp), __FILE__, __LINE__, __func__, \ 130 (os_error), (fmt), ## __VA_ARGS__) 131 void error_setg_errno_internal(Error **errp, 132 const char *fname, int line, const char *func, 133 int os_error, const char *fmt, ...) 134 GCC_FMT_ATTR(6, 7); 135 136 #ifdef _WIN32 137 /* 138 * Just like error_setg(), with @win32_error info added to the message. 139 * If @win32_error is non-zero, ": " + g_win32_error_message(win32_err) 140 * is appended to the human-readable error message. 141 */ 142 #define error_setg_win32(errp, win32_err, fmt, ...) \ 143 error_setg_win32_internal((errp), __FILE__, __LINE__, __func__, \ 144 (win32_err), (fmt), ## __VA_ARGS__) 145 void error_setg_win32_internal(Error **errp, 146 const char *src, int line, const char *func, 147 int win32_err, const char *fmt, ...) 148 GCC_FMT_ATTR(6, 7); 149 #endif 150 151 /* 152 * Propagate error object (if any) from @local_err to @dst_errp. 153 * If @local_err is NULL, do nothing (because there's nothing to 154 * propagate). 155 * Else, if @dst_errp is NULL, errors are being ignored. Free the 156 * error object. 157 * Else, if @dst_errp is &error_abort, print a suitable message and 158 * abort(). 159 * Else, if @dst_errp is &error_fatal, print a suitable message and 160 * exit(1). 161 * Else, if @dst_errp already contains an error, ignore this one: free 162 * the error object. 163 * Else, move the error object from @local_err to *@dst_errp. 164 * On return, @local_err is invalid. 165 */ 166 void error_propagate(Error **dst_errp, Error *local_err); 167 168 /** 169 * Append a printf-style human-readable explanation to an existing error. 170 * May be called multiple times, and safe if @errp is NULL. 171 */ 172 void error_append_hint(Error **errp, const char *fmt, ...) 173 GCC_FMT_ATTR(2, 3); 174 175 /* 176 * Convenience function to report open() failure. 177 */ 178 #define error_setg_file_open(errp, os_errno, filename) \ 179 error_setg_file_open_internal((errp), __FILE__, __LINE__, __func__, \ 180 (os_errno), (filename)) 181 void error_setg_file_open_internal(Error **errp, 182 const char *src, int line, const char *func, 183 int os_errno, const char *filename); 184 185 /* 186 * Return an exact copy of @err. 187 */ 188 Error *error_copy(const Error *err); 189 190 /* 191 * Free @err. 192 * @err may be NULL. 193 */ 194 void error_free(Error *err); 195 196 /* 197 * Convenience function to assert that *@errp is set, then silently free it. 198 */ 199 void error_free_or_abort(Error **errp); 200 201 /* 202 * Convenience function to error_report() and free @err. 203 */ 204 void error_report_err(Error *); 205 206 /* 207 * Just like error_setg(), except you get to specify the error class. 208 * Note: use of error classes other than ERROR_CLASS_GENERIC_ERROR is 209 * strongly discouraged. 210 */ 211 #define error_set(errp, err_class, fmt, ...) \ 212 error_set_internal((errp), __FILE__, __LINE__, __func__, \ 213 (err_class), (fmt), ## __VA_ARGS__) 214 void error_set_internal(Error **errp, 215 const char *src, int line, const char *func, 216 ErrorClass err_class, const char *fmt, ...) 217 GCC_FMT_ATTR(6, 7); 218 219 /* 220 * Pass to error_setg() & friends to abort() on error. 221 */ 222 extern Error *error_abort; 223 224 /* 225 * Pass to error_setg() & friends to exit(1) on error. 226 */ 227 extern Error *error_fatal; 228 229 #endif 230