Merge "package: version up" into develop
[sdk/emulator/qemu.git] / error.c
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
13 #include "qemu-common.h"
14 #include "error.h"
15 #include "qjson.h"
16 #include "qdict.h"
17 #include "qapi-types.h"
18 #include "qerror.h"
19
20 struct Error
21 {
22     char *msg;
23     ErrorClass err_class;
24 };
25
26 void error_set(Error **errp, ErrorClass err_class, const char *fmt, ...)
27 {
28     Error *err;
29     va_list ap;
30
31     if (errp == NULL) {
32         return;
33     }
34     assert(*errp == NULL);
35
36     err = g_malloc0(sizeof(*err));
37
38     va_start(ap, fmt);
39     err->msg = g_strdup_vprintf(fmt, ap);
40     va_end(ap);
41     err->err_class = err_class;
42
43     *errp = err;
44 }
45
46 Error *error_copy(const Error *err)
47 {
48     Error *err_new;
49
50     err_new = g_malloc0(sizeof(*err));
51     err_new->msg = g_strdup(err->msg);
52     err_new->err_class = err->err_class;
53
54     return err_new;
55 }
56
57 bool error_is_set(Error **errp)
58 {
59     return (errp && *errp);
60 }
61
62 ErrorClass error_get_class(const Error *err)
63 {
64     return err->err_class;
65 }
66
67 const char *error_get_pretty(Error *err)
68 {
69     return err->msg;
70 }
71
72 void error_free(Error *err)
73 {
74     if (err) {
75         g_free(err->msg);
76         g_free(err);
77     }
78 }
79
80 void error_propagate(Error **dst_err, Error *local_err)
81 {
82     if (dst_err && !*dst_err) {
83         *dst_err = local_err;
84     } else if (local_err) {
85         error_free(local_err);
86     }
87 }