Lumiera  0.pre.03
»edityourfreedom«
error.hpp
Go to the documentation of this file.
1 /*
2  ERROR.hpp - Lumiera Exception Interface (C++)
3 
4  Copyright (C) Lumiera.org
5  2008,2010 Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 */
22 
23 
39 #ifndef LUMIERA_ERROR_HPP_
40 #define LUMIERA_ERROR_HPP_
41 
42 #define _STDBOOL_H // prevent <atomic> from including stdbool.h
43 
44 #include "include/logging.h"
45 #include "include/lifecycle.h"
46 #include "lib/error.h"
47 
48 #include <exception>
49 #include <string>
50 
51 
52 namespace lumiera {
53 
54  using std::string;
55  using CStr = const char*;
56 
57 
58 #define LERR_(_NAME_) LUMIERA_ERROR_##_NAME_
59 
61  LUMIERA_ERROR_DECLARE(EXCEPTION);
62 
69  class Error
70  : public std::exception
71  {
72  lumiera_err const id_;
73  string msg_;
74  string desc_;
75  mutable string what_;
76  const string cause_;
77 
78 
79  public:
80  virtual ~Error () noexcept { };
81 
82 
83  Error (string description=""
84  ,lumiera_err const id =LERR_(EXCEPTION)) noexcept;
85  Error (std::exception const& cause
86  ,string description=""
87  ,lumiera_err const id =LERR_(EXCEPTION)) noexcept;
88 
89  Error (Error &&) = default;
90  Error (Error const&) = default;
91  Error& operator= (Error &&) = delete;
92  Error& operator= (Error const&) = delete;
93 
95  virtual CStr
96  what () const noexcept override;
97 
101  getID () const noexcept
102  {
103  return id_;
104  }
105 
107  string const&
108  getUsermsg () const noexcept
109  {
110  return msg_;
111  }
112 
119  string const&
120  rootCause () const noexcept
121  {
122  return cause_;
123  }
124 
128  Error&
129  setUsermsg (string const& newMsg) noexcept
130  {
131  msg_ = newMsg;
132  return *this;
133  }
134 
136  Error&
137  prependInfo (string const& text) noexcept
138  {
139  desc_.insert (0, text);
140  return *this;
141  }
142 
143 
144  private:
145  static const string
146  extractCauseMsg (std::exception const&) noexcept;
147  };
148 
149 
150 
151 
152 
153 
154  /* === Exception sub-categories === */
155 
156  namespace error {
157 
164  void lumiera_unexpectedException () noexcept;
165 
167  void assertion_terminate (const string& location);
168 
169 
170  /* constants to be used as error IDs */
171  LUMIERA_ERROR_DECLARE (LOGIC );
172  LUMIERA_ERROR_DECLARE (FATAL );
173  LUMIERA_ERROR_DECLARE (CONFIG );
174  LUMIERA_ERROR_DECLARE (STATE );
175  LUMIERA_ERROR_DECLARE (FLAG );
176  LUMIERA_ERROR_DECLARE (INVALID );
177  LUMIERA_ERROR_DECLARE (EXTERNAL );
178  LUMIERA_ERROR_DECLARE (ASSERTION);
179 
180  /* generic error situations */
181  LUMIERA_ERROR_DECLARE (LIFECYCLE);
182  LUMIERA_ERROR_DECLARE (WRONG_TYPE);
183  LUMIERA_ERROR_DECLARE (ITER_EXHAUST);
184  LUMIERA_ERROR_DECLARE (CAPACITY);
185  LUMIERA_ERROR_DECLARE (INDEX_BOUNDS);
186  LUMIERA_ERROR_DECLARE (BOTTOM_VALUE);
187  LUMIERA_ERROR_DECLARE (UNCONNECTED);
188  LUMIERA_ERROR_DECLARE (UNIMPLEMENTED);
189 
190 
191 
195  template<lumiera_err const& eID, class PAR =Error>
197  : public PAR
198  {
199  public:
200  LumieraError (std::string description=""
201  ,lumiera_err const id=eID) noexcept
202  : PAR{description, id? id:eID}
203  { }
204  LumieraError (std::exception const& cause
205  ,std::string description=""
206  ,lumiera_err const id=eID) noexcept
207  : PAR{cause, description, id? id:eID}
208  { }
209  };
210 
211  //----CLASS-------------------ID--------------PARENT------
219 
220 
224 
226  CStr detailInfo();
227 
228  } // namespace error
229 
230 
231 
238  inline void
240  {
241  if (lumiera_err errorFlag =lumiera_error())
242  {
244  , errorFlag);
245  } } //causes the error state to be set
246 
253  template<class EX>
254  inline void
255  maybeThrow (string description ="")
256  {
257  if (lumiera_err errorFlag =lumiera_error())
258  {
259  throw EX (error::Flag{error::detailInfo(), errorFlag}
260  ,description);
261  } }
262 
263 
264 } // namespace lumiera
265 
266 /**************************************************/
272 #define ERROR_LOG_AND_IGNORE(_FLAG_,_OP_DESCR_) \
273  catch (std::exception& problem) \
274  { \
275  const char* errID = lumiera_error(); \
276  WARN (_FLAG_, "%s failed: %s", _OP_DESCR_, problem.what()); \
277  TRACE (debugging, "Error flag was: %s", errID);\
278  } \
279  catch (...) \
280  { \
281  const char* errID = lumiera_error(); \
282  ERROR (_FLAG_, "%s failed with unknown exception; " \
283  "error flag is: %s" \
284  , _OP_DESCR_, errID); \
285  }
286 
287 
288 
289 /**************************************************/
293 #if 0
294 #ifdef NOBUG_ABORT
295 #undef NOBUG_ABORT
296 #define LUMIERA_NOBUG_LOCATION \
297  std::string (NOBUG_BASENAME(__FILE__)) +":"+ NOBUG_STRINGIZE(__LINE__) + ", function " + __func__
298 #define NOBUG_ABORT \
299  lumiera::error::assertion_terminate (LUMIERA_NOBUG_LOCATION);
300 #endif
301 #endif
302 
303 #endif // LUMIERA_ERROR_HPP_
void lumiera_unexpectedException() noexcept
global function for handling unknown exceptions encountered at functions declaring not to throw this ...
Installing and invoking of application lifecycle event callbacks.
void assertion_terminate(const string &location)
throw an error::Fatal indicating "assertion failure"
LumieraError< LERR_(FLAG), State > Flag
Definition: error.hpp:215
lumiera_err getID() const noexcept
the internal Lumiera-error-ID (was set as C-errorstate in ctor)
Definition: error.hpp:101
string desc_
detailed description of the error situation for the developers
Definition: error.hpp:74
const char * CStr
Definition: error.hpp:55
virtual CStr what() const noexcept override
std::exception interface : yield a diagnostic message
void throwOnError()
Check the lumiera error state, which maybe was set by C-code.
Definition: error.hpp:239
void install_unexpectedException_handler()
install our own handler for undeclared exceptions.
This header is for including and configuring NoBug.
Error(string description="", lumiera_err const id=LERR_(EXCEPTION)) noexcept
this is an interface
LUMIERA_ERROR_DECLARE(EXCEPTION)
error-ID for unspecified exceptions
Lumiera error handling (C interface).
Derived specific exceptions within Lumiera&#39;s exception hierarchy.
Definition: error.hpp:196
lumiera_err const id_
an LUMIERA_ERROR id, which is set as errorstate on construction
Definition: error.hpp:72
static const string extractCauseMsg(std::exception const &) noexcept
#define LERR_(_NAME_)
Definition: error.hpp:58
lumiera_err lumiera_error(void)
Get and clear current error state.
Definition: error-state.c:124
const char * lumiera_err
Definition: error.h:57
string const & getUsermsg() const noexcept
extract the message to be displayed for the user
Definition: error.hpp:108
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:120
const string cause_
description of first exception encountered in the chain
Definition: error.hpp:76
virtual ~Error() noexcept
Definition: error.hpp:80
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:255
LumieraError(std::string description="", lumiera_err const id=eID) noexcept
Definition: error.hpp:200
Error & prependInfo(string const &text) noexcept
give additional developer info.
Definition: error.hpp:137
Error & operator=(Error &&)=delete
string what_
buffer for generating the detailed description on demand
Definition: error.hpp:75
Lumiera public interface.
LumieraError(std::exception const &cause, std::string description="", lumiera_err const id=eID) noexcept
Definition: error.hpp:204
string msg_
friendly message intended for users (to be localised)
Definition: error.hpp:73
Error & setUsermsg(string const &newMsg) noexcept
replace the previous or default friendly message for the user.
Definition: error.hpp:129
Interface and Base definition for all Lumiera Exceptions.
Definition: error.hpp:69