Imported Upstream version 1.5.0
[platform/upstream/augeas.git] / src / errcode.h
1 /*
2  * errcode.h: internal interface for error reporting
3  *
4  * Copyright (C) 2009-2015 David Lutterkort
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
19  *
20  * Author: David Lutterkort <lutter@redhat.com>
21  */
22
23 #ifndef ERRCODE_H_
24 #define ERRCODE_H_
25
26 #include "internal.h"
27 /* Include augeas.h for the error codes */
28 #include "augeas.h"
29
30 /*
31  * Error details in a separate struct that we can pass around
32  */
33 struct error {
34     aug_errcode_t  code;
35     int            minor;
36     char          *details;       /* Human readable explanation */
37     const char    *minor_details; /* Human readable version of MINOR */
38     /* A dummy info of last resort; this can be used in places where
39      * a struct info is needed but none available
40      */
41     struct info   *info;
42     /* Bit of a kludge to get at struct augeas, but since struct error
43      * is now available in a lot of places (through struct info), this
44      * gives a convenient way to get at the overall state
45      */
46     const struct augeas *aug;
47     /* A preallocated exception so that we can throw something, even
48      * under OOM conditions
49      */
50     struct value *exn;
51 };
52
53 void report_error(struct error *err, aug_errcode_t errcode,
54                   const char *format, ...)
55     ATTRIBUTE_FORMAT(printf, 3, 4);
56
57 void bug_on(struct error *err, const char *srcfile, int srclineno,
58             const char *format, ...)
59     ATTRIBUTE_FORMAT(printf, 4, 5);
60
61 void reset_error(struct error *err);
62
63 #define HAS_ERR(obj) ((obj)->error->code != AUG_NOERROR)
64
65 #define ERR_BAIL(obj) if ((obj)->error->code != AUG_NOERROR) goto error;
66
67 #define ERR_RET(obj) if ((obj)->error->code != AUG_NOERROR) return;
68
69 #define ERR_NOMEM(cond, obj)                             \
70     if (cond) {                                          \
71         report_error((obj)->error, AUG_ENOMEM, NULL);    \
72         goto error;                                      \
73     }
74
75 #define ERR_REPORT(obj, code, fmt ...)          \
76     report_error((obj)->error, code, ## fmt)
77
78 #define ERR_THROW(cond, obj, code, fmt ...)             \
79     do {                                                \
80         if (cond) {                                     \
81             report_error((obj)->error, code, ## fmt);   \
82             goto error;                                 \
83         }                                               \
84     } while(0)
85
86 #define ARG_CHECK(cond, obj, fmt ...)                           \
87     do {                                                        \
88         if (cond) {                                             \
89             report_error((obj)->error, AUG_EBADARG, ## fmt);    \
90             goto error;                                         \
91         }                                                       \
92     } while(0)
93
94 /* A variant of assert that uses our error reporting infrastructure
95  * instead of aborting
96  */
97 #ifdef NDEBUG
98 # define ensure(cond, obj) if (0) goto error
99 # define ensure0(cond, obj) if (0) return NULL
100 #else
101 # define ensure(cond, obj)                                           \
102     if (!(cond)) {                                                   \
103         bug_on((obj)->error, __FILE__, __LINE__, NULL);              \
104         goto error;                                                  \
105     }
106 # define ensure0(cond, obj)                                          \
107     if (!(cond)) {                                                   \
108         bug_on((obj)->error, __FILE__, __LINE__, NULL);              \
109         return NULL;                                                 \
110     }
111 #endif
112
113 #define BUG_ON(cond, obj, fmt ...)                                  \
114     if (cond) {                                                     \
115         bug_on((obj)->error, __FILE__, __LINE__, ## fmt);           \
116         goto error;                                                 \
117     }
118
119 #endif
120
121
122 /*
123  * Local variables:
124  *  indent-tabs-mode: nil
125  *  c-indent-level: 4
126  *  c-basic-offset: 4
127  *  tab-width: 4
128  * End:
129  */