xref: /qemu/include/qapi/error.h (revision bfa3ab61)
1 /*
2  * QEMU Error Objects
3  *
4  * Copyright IBM, Corp. 2011
5  *
6  * Authors:
7  *  Anthony Liguori   <aliguori@us.ibm.com>
8  *
9  * This work is licensed under the terms of the GNU LGPL, version 2.  See
10  * the COPYING.LIB file in the top-level directory.
11  */
12 #ifndef ERROR_H
13 #define ERROR_H
14 
15 #include "qemu/compiler.h"
16 #include "qapi-types.h"
17 #include <stdbool.h>
18 
19 /**
20  * A class representing internal errors within QEMU.  An error has a ErrorClass
21  * code and a human message.
22  */
23 typedef struct Error Error;
24 
25 /**
26  * Set an indirect pointer to an error given a ErrorClass value and a
27  * printf-style human message.  This function is not meant to be used outside
28  * of QEMU.
29  */
30 void error_set(Error **errp, ErrorClass err_class, const char *fmt, ...)
31     GCC_FMT_ATTR(3, 4);
32 
33 /**
34  * Set an indirect pointer to an error given a ErrorClass value and a
35  * printf-style human message, followed by a strerror() string if
36  * @os_error is not zero.
37  */
38 void error_set_errno(Error **errp, int os_error, ErrorClass err_class,
39                      const char *fmt, ...) GCC_FMT_ATTR(4, 5);
40 
41 #ifdef _WIN32
42 /**
43  * Set an indirect pointer to an error given a ErrorClass value and a
44  * printf-style human message, followed by a g_win32_error_message() string if
45  * @win32_err is not zero.
46  */
47 void error_set_win32(Error **errp, int win32_err, ErrorClass err_class,
48                      const char *fmt, ...) GCC_FMT_ATTR(4, 5);
49 #endif
50 
51 /**
52  * Same as error_set(), but sets a generic error
53  */
54 #define error_setg(errp, fmt, ...) \
55     error_set(errp, ERROR_CLASS_GENERIC_ERROR, fmt, ## __VA_ARGS__)
56 #define error_setg_errno(errp, os_error, fmt, ...) \
57     error_set_errno(errp, os_error, ERROR_CLASS_GENERIC_ERROR, \
58                     fmt, ## __VA_ARGS__)
59 #ifdef _WIN32
60 #define error_setg_win32(errp, win32_err, fmt, ...) \
61     error_set_win32(errp, win32_err, ERROR_CLASS_GENERIC_ERROR, \
62                     fmt, ## __VA_ARGS__)
63 #endif
64 
65 /**
66  * Helper for open() errors
67  */
68 void error_setg_file_open(Error **errp, int os_errno, const char *filename);
69 
70 /*
71  * Get the error class of an error object.
72  */
73 ErrorClass error_get_class(const Error *err);
74 
75 /**
76  * Returns an exact copy of the error passed as an argument.
77  */
78 Error *error_copy(const Error *err);
79 
80 /**
81  * Get a human readable representation of an error object.
82  */
83 const char *error_get_pretty(Error *err);
84 
85 /**
86  * Convenience function to error_report() and free an error object.
87  */
88 void error_report_err(Error *);
89 
90 /**
91  * Propagate an error to an indirect pointer to an error.  This function will
92  * always transfer ownership of the error reference and handles the case where
93  * dst_err is NULL correctly.  Errors after the first are discarded.
94  */
95 void error_propagate(Error **dst_errp, Error *local_err);
96 
97 /**
98  * Free an error object.
99  */
100 void error_free(Error *err);
101 
102 /**
103  * If passed to error_set and friends, abort().
104  */
105 
106 extern Error *error_abort;
107 
108 #endif
109