Lumiera 0.pre.04~rc.1
»edit your freedom«
Loading...
Searching...
No Matches
error.hpp
Go to the documentation of this file.
1/*
2 ERROR.hpp - Lumiera Exception Interface (C++)
3
4 Copyright (C)
5 2008,2010 Hermann Vosseler <Ichthyostega@web.de>
6
7  **Lumiera** is free software; you can redistribute it and/or modify it
8  under the terms of the GNU General Public License as published by the
9  Free Software Foundation; either version 2 of the License, or (at your
10  option) any later version. See the file COPYING for further details.
11
12*/
13
14
30#ifndef LUMIERA_ERROR_HPP_
31#define LUMIERA_ERROR_HPP_
32
33#define _STDBOOL_H // prevent <atomic> from including stdbool.h
34
35#include "include/logging.h"
36#include "lib/hash-standard.hpp"
37#include "lib/error.h"
38
39#include <exception>
40#include <string>
41
42using CStr = const char*;
43
44
45#define LERR_(_NAME_) lumiera::error::LUMIERA_ERROR_##_NAME_
46
47namespace lumiera {
48 namespace error {
51 }
52
53 using std::string;
54
55
56
63 class Error
64 : public std::exception
65 {
67 string msg_;
68 string desc_;
69 mutable string what_;
70 const string cause_;
71
72
73 public:
74 virtual ~Error () noexcept { };
75
76
77 Error (string description=""
78 ,lumiera_err const id =LERR_(EXCEPTION)) noexcept;
79 Error (std::exception const& cause
80 ,string description=""
81 ,lumiera_err const id =LERR_(EXCEPTION)) noexcept;
82
83 Error (Error &&) = default;
84 Error (Error const&) = default;
85 Error& operator= (Error &&) = delete;
86 Error& operator= (Error const&) = delete;
87
89 virtual CStr
90 what () const noexcept override;
91
95 getID () const noexcept
96 {
97 return id_;
98 }
99
101 string const&
102 getUsermsg () const noexcept
103 {
104 return msg_;
105 }
106
113 string const&
114 rootCause () const noexcept
115 {
116 return cause_;
117 }
118
122 Error&
123 setUsermsg (string const& newMsg) noexcept
124 {
125 msg_ = newMsg;
126 return *this;
127 }
128
130 Error&
131 prependInfo (string const& text) noexcept
132 {
133 desc_.insert (0, text);
134 return *this;
135 }
136
137
138 private:
139 static const string
140 extractCauseMsg (std::exception const&) noexcept;
141 };
142
143
144
145
146
147
148 /* === Exception sub-categories === */
149
150 namespace error {
151
158 void lumiera_unexpectedException () noexcept;
159
161 void assertion_terminate (const string& location);
162
163
164 /* constants to be used as error IDs */
173
174 /* generic error situations */
177 LUMIERA_ERROR_DECLARE (ITER_EXHAUST);
179 LUMIERA_ERROR_DECLARE (SAFETY_LIMIT);
180 LUMIERA_ERROR_DECLARE (INDEX_BOUNDS);
181 LUMIERA_ERROR_DECLARE (BOTTOM_VALUE);
182 LUMIERA_ERROR_DECLARE (UNCONNECTED);
183 LUMIERA_ERROR_DECLARE (UNIMPLEMENTED);
184
185
186
190 template<lumiera_err const& eID, class PAR =Error>
192 : public PAR
193 {
194 public:
195 LumieraError (std::string description=""
196 ,lumiera_err const id=eID) noexcept
197 : PAR{description, id? id:eID}
198 { }
199 LumieraError (std::exception const& cause
200 ,std::string description=""
201 ,lumiera_err const id=eID) noexcept
202 : PAR{cause, description, id? id:eID}
203 { }
204 };
205
206 //----CLASS-------------------ID--------------PARENT------
207 using Logic = LumieraError<LERR_(LOGIC)>;
208 using Fatal = LumieraError<LERR_(FATAL), Logic>;
209 using State = LumieraError<LERR_(STATE)>;
210 using Flag = LumieraError<LERR_(FLAG), State>;
211 using Invalid = LumieraError<LERR_(INVALID)>;
212 using Config = LumieraError<LERR_(CONFIG), Invalid>;
213 using External = LumieraError<LERR_(EXTERNAL)>;
214
215
219
222
223 } // namespace error
224
225
226
233 inline void
235 {
236 if (lumiera_err errorFlag =lumiera_error())
237 {
239 , errorFlag);
240 } }
241
248 template<class EX>
249 inline void
250 maybeThrow (string description ="")
251 {
252 if (lumiera_err errorFlag =lumiera_error())
253 {
254 throw EX (error::Flag{error::detailInfo(), errorFlag}
255 ,description);
256 } }
257
258
259} // namespace lumiera
260
261/**************************************************/
267#define ERROR_LOG_AND_IGNORE(_FLAG_,_OP_DESCR_) \
268 catch (std::exception& problem) \
269 { \
270 CStr errID = lumiera_error(); \
271 WARN (_FLAG_, "%s failed: %s", _OP_DESCR_, problem.what()); \
272 TRACE (debugging, "Error flag was: %s", errID);\
273 } \
274 catch (...) \
275 { \
276 CStr errID = lumiera_error(); \
277 ERROR (_FLAG_, "%s failed with unknown exception; " \
278 "error flag is: %s" \
279 , _OP_DESCR_, errID?errID:"??"); \
280 }
281
282#define ERROR_LOG_AND_RETHROW(_FLAG_,_OP_DESCR_) \
283 catch (std::exception& problem) \
284 { \
285 CStr errID = lumiera_error(); \
286 WARN (_FLAG_, "%s failed: %s", _OP_DESCR_, problem.what()); \
287 TRACE (debugging, "Error flag was: %s", errID); \
288 throw; \
289 } \
290 catch (...) \
291 { \
292 CStr errID = lumiera_error(); \
293 ERROR (_FLAG_, "%s failed with unknown exception; " \
294 "error flag is: %s" \
295 , _OP_DESCR_, errID?errID:"??"); \
296 throw; \
297 }
298
299/******************************************************/
305#define ON_EXCEPTION_RETURN(_VAL_,_OP_DESCR_) \
306 catch (std::exception& problem) \
307 { \
308 CStr errID = lumiera_error(); \
309 WARN (stage, "%s (Handler) failed: %s", \
310 _OP_DESCR_, problem.what()); \
311 TRACE (debugging, "Error flag was: %s", errID); \
312 return (_VAL_); \
313 } \
314 catch (...) \
315 { \
316 CStr errID = lumiera_error(); \
317 ERROR (stage, "(Handler) %s failed with " \
318 "unknown exception; error flag is: %s" \
319 , _OP_DESCR_, errID?errID:"??"); \
320 return (_VAL_); \
321 }
322
323
324
325/**************************************************/
329#if 0
330#ifdef NOBUG_ABORT
331#undef NOBUG_ABORT
332#define LUMIERA_NOBUG_LOCATION \
333 std::string (NOBUG_BASENAME(__FILE__)) +":"+ NOBUG_STRINGIZE(__LINE__) + ", function " + __func__
334#define NOBUG_ABORT \
335 lumiera::error::assertion_terminate (LUMIERA_NOBUG_LOCATION);
336#endif
337#endif
338
339#endif // LUMIERA_ERROR_HPP_
Interface and Base definition for all Lumiera Exceptions.
Definition error.hpp:65
const string cause_
description of first exception encountered in the chain
Definition error.hpp:70
string const & getUsermsg() const noexcept
extract the message to be displayed for the user
Definition error.hpp:102
Error & prependInfo(string const &text) noexcept
give additional developer info.
Definition error.hpp:131
virtual CStr what() const noexcept override
std::exception interface : yield a diagnostic message
string desc_
detailed description of the error situation for the developers
Definition error.hpp:68
Error(Error const &)=default
virtual ~Error() noexcept
Definition error.hpp:74
static const string extractCauseMsg(std::exception const &) noexcept
Error(Error &&)=default
lumiera_err const id_
an LUMIERA_ERROR id, which is set as errorstate on construction
Definition error.hpp:66
Error & operator=(Error &&)=delete
lumiera_err getID() const noexcept
the internal Lumiera-error-ID (was set as C-errorstate in ctor)
Definition error.hpp:95
string const & rootCause() const noexcept
If this exception was caused by a chain of further exceptions, return the description of the first on...
Definition error.hpp:114
string what_
buffer for generating the detailed description on demand
Definition error.hpp:69
string msg_
friendly message intended for users (to be localised)
Definition error.hpp:67
Error & setUsermsg(string const &newMsg) noexcept
replace the previous or default friendly message for the user.
Definition error.hpp:123
Derived specific exceptions within Lumiera's exception hierarchy.
Definition error.hpp:193
LumieraError(std::string description="", lumiera_err const id=eID) noexcept
Definition error.hpp:195
LumieraError(std::exception const &cause, std::string description="", lumiera_err const id=eID) noexcept
Definition error.hpp:199
lumiera_err lumiera_error(void)
Get and clear current error state.
Lumiera error handling (C interface).
const char * lumiera_err
Definition error.h:48
#define LUMIERA_ERROR_DECLARE(err)
Forward declare an error constant.
Definition error.h:62
#define LERR_(_NAME_)
Definition error.hpp:45
const char * CStr
Definition error.hpp:42
Helper to use a single extension point for specialised hash functions.
This header is for including and configuring NoBug.
void assertion_terminate(const string &location)
throw an error::Fatal indicating "assertion failure"
LumieraError< LERR_(STATE)> State
Definition error.hpp:209
void lumiera_unexpectedException() noexcept
global function for handling unknown exceptions encountered at functions declaring not to throw this ...
void install_unexpectedException_handler()
install our own handler for undeclared exceptions.
LumieraError< LERR_(FLAG), State > Flag
Definition error.hpp:210
LumieraError< LERR_(LOGIC)> Logic
Definition error.hpp:207
LumieraError< LERR_(CONFIG), Invalid > Config
Definition error.hpp:212
LumieraError< LERR_(EXTERNAL)> External
Definition error.hpp:213
Lumiera public interface.
Definition advice.hpp:102
void throwOnError()
Check the lumiera error state, which maybe was set by C-code.
Definition error.hpp:234
void maybeThrow(string description="")
Check the lumiera error state and throw a specific exception in case a non-cleared errorflag is detec...
Definition error.hpp:250