MoonUnit
 All Data Structures Variables Enumerations Enumerator Groups Pages
error.h
1 /*
2  * Copyright (c) 2007, Brian Koropoff
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the Moonunit project nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY BRIAN KOROPOFF ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL BRIAN KOROPOFF BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef __MU_ERROR_H__
29 #define __MU_ERROR_H__
30 
31 #include <stdbool.h>
32 
33 #include <moonunit/internal/boilerplate.h>
34 
35 /* Moonunit error code facility
36  * Used internally and for plugins
37  * Patterned after GError in glib
38  */
39 
40 C_BEGIN_DECLS
41 
42 typedef enum MuStatusCode
43 {
45  MU_ERROR_SUCCESS = 0,
47  MU_ERROR_GENERAL = 1,
49  MU_ERROR_MEMORY = 2,
51  MU_ERROR_SYSTEM = 3,
53  MU_ERROR_LOAD_LIBRARY = 4,
55  MU_ERROR_CONSTRUCT_LIBRARY = 5,
57  MU_ERROR_DESTRUCT_LIBRARY = 6
58 } MuStatusCode;
59 
60 typedef struct MuError
61 {
62  int code;
63  char* message;
64 } MuError;
65 
66 void mu_error_raise(MuError** err, MuStatusCode code, const char* format, ...);
67 void mu_error_handle(MuError** err);
68 void mu_error_reraise(MuError** err, MuError* src);
69 bool mu_error_equal(MuError* err, int code);
70 
71 #define MU_RAISE_RETURN_VOID(err, code, ...) \
72  do \
73  { \
74  mu_error_raise(err, code, __VA_ARGS__); \
75  return; \
76  } while (0) \
77 
78 #define MU_RAISE_RETURN(ret, err, code, ...) \
79  do \
80  { \
81  mu_error_raise(err, code, __VA_ARGS__); \
82  return (ret); \
83  } while (0) \
84 
85 #define MU_RAISE_GOTO(lab, err, code, ...) \
86  do \
87  { \
88  mu_error_raise(err, code, __VA_ARGS__); \
89  goto lab; \
90  } while (0) \
91 
92 #define MU_RERAISE_RETURN_VOID(err, src) \
93  do \
94  { \
95  mu_error_reraise(err, src); \
96  return; \
97  } while (0) \
98 
99 #define MU_RERAISE_RETURN(ret, err, src) \
100  do \
101  { \
102  mu_error_reraise(err, src); \
103  return (ret); \
104  } while (0) \
105 
106 #define MU_RERAISE_GOTO(lab, err, src) \
107  do \
108  { \
109  mu_error_reraise(err, src); \
110  goto lab; \
111  } while (0) \
112 
113 #define MU_CATCH_GOTO(lab, code) \
114  do \
115  { \
116  if (mu_error_equal(err, code)) \
117  goto lab; \
118  } while (0) \
119 
120 #define MU_PROPAGATE(lab, err, src) \
121  do \
122  { \
123  MuError* e_ = (src); \
124  if (e_ != NULL) \
125  { \
126  MU_RERAISE_GOTO(lab, err, e_); \
127  } \
128  } while (0)
129 
130 #define MU_CATCH(err, code) \
131  if (mu_error_equal(err, code)) \
132 
133 #define MU_CATCH_ALL(err) \
134  if (err) \
135 
136 #define MU_HANDLE(err) \
137  mu_error_handle(err)
138 
139 C_END_DECLS
140 
141 #endif
Definition: error.h:60