Lumiera  0.pre.03
»edit your freedom«
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 "lib/hash-standard.hpp"
46 #include "lib/error.h"
47 
48 #include <exception>
49 #include <string>
50 
51 
52 #define LERR_(_NAME_) lumiera::error::LUMIERA_ERROR_##_NAME_
53 
54 namespace lumiera {
55 
56  using std::string;
57  using CStr = const char*;
58 
59  namespace error {
61  LUMIERA_ERROR_DECLARE(EXCEPTION);
62  }
63 
64 
71  class Error
72  : public std::exception
73  {
74  lumiera_err const id_;
75  string msg_;
76  string desc_;
77  mutable string what_;
78  const string cause_;
79 
80 
81  public:
82  virtual ~Error () noexcept { };
83 
84 
85  Error (string description=""
86  ,lumiera_err const id =LERR_(EXCEPTION)) noexcept;
87  Error (std::exception const& cause
88  ,string description=""
89  ,lumiera_err const id =LERR_(EXCEPTION)) noexcept;
90 
91  Error (Error &&) = default;
92  Error (Error const&) = default;
93  Error& operator= (Error &&) = delete;
94  Error& operator= (Error const&) = delete;
95 
97  virtual CStr
98  what () const noexcept override;
99 
102  lumiera_err
103  getID () const noexcept
104  {
105  return id_;
106  }
107 
109  string const&
110  getUsermsg () const noexcept
111  {
112  return msg_;
113  }
114 
121  string const&
122  rootCause () const noexcept
123  {
124  return cause_;
125  }
126 
130  Error&
131  setUsermsg (string const& newMsg) noexcept
132  {
133  msg_ = newMsg;
134  return *this;
135  }
136 
138  Error&
139  prependInfo (string const& text) noexcept
140  {
141  desc_.insert (0, text);
142  return *this;
143  }
144 
145 
146  private:
147  static const string
148  extractCauseMsg (std::exception const&) noexcept;
149  };
150 
151 
152 
153 
154 
155 
156  /* === Exception sub-categories === */
157 
158  namespace error {
159 
166  void lumiera_unexpectedException () noexcept;
167 
169  void assertion_terminate (const string& location);
170 
171 
172  /* constants to be used as error IDs */
173  LUMIERA_ERROR_DECLARE (LOGIC );
174  LUMIERA_ERROR_DECLARE (FATAL );
175  LUMIERA_ERROR_DECLARE (CONFIG );
176  LUMIERA_ERROR_DECLARE (STATE );
177  LUMIERA_ERROR_DECLARE (FLAG );
178  LUMIERA_ERROR_DECLARE (INVALID );
179  LUMIERA_ERROR_DECLARE (EXTERNAL );
180  LUMIERA_ERROR_DECLARE (ASSERTION);
181 
182  /* generic error situations */
183  LUMIERA_ERROR_DECLARE (LIFECYCLE);
184  LUMIERA_ERROR_DECLARE (WRONG_TYPE);
185  LUMIERA_ERROR_DECLARE (ITER_EXHAUST);
186  LUMIERA_ERROR_DECLARE (CAPACITY);
187  LUMIERA_ERROR_DECLARE (SAFETY_LIMIT);
188  LUMIERA_ERROR_DECLARE (INDEX_BOUNDS);
189  LUMIERA_ERROR_DECLARE (BOTTOM_VALUE);
190  LUMIERA_ERROR_DECLARE (UNCONNECTED);
191  LUMIERA_ERROR_DECLARE (UNIMPLEMENTED);
192 
193 
194 
198  template<lumiera_err const& eID, class PAR =Error>
200  : public PAR
201  {
202  public:
203  LumieraError (std::string description=""
204  ,lumiera_err const id=eID) noexcept
205  : PAR{description, id? id:eID}
206  { }
207  LumieraError (std::exception const& cause
208  ,std::string description=""
209  ,lumiera_err const id=eID) noexcept
210  : PAR{cause, description, id? id:eID}
211  { }
212  };
213 
214  //----CLASS-------------------ID--------------PARENT------
222 
223 
226  void install_unexpectedException_handler();
227 
229  CStr detailInfo();
230 
231  } // namespace error
232 
233 
234 
241  inline void
243  {
244  if (lumiera_err errorFlag =lumiera_error())
245  {
246  throw error::Flag( error::detailInfo()
247  , errorFlag);
248  } }
249 
256  template<class EX>
257  inline void
258  maybeThrow (string description ="")
259  {
260  if (lumiera_err errorFlag =lumiera_error())
261  {
262  throw EX (error::Flag{error::detailInfo(), errorFlag}
263  ,description);
264  } }
265 
266 
267 } // namespace lumiera
268 
269 /**************************************************/
275 #define ERROR_LOG_AND_IGNORE(_FLAG_,_OP_DESCR_) \
276  catch (std::exception& problem) \
277  { \
278  const char* errID = lumiera_error(); \
279  WARN (_FLAG_, "%s failed: %s", _OP_DESCR_, problem.what()); \
280  TRACE (debugging, "Error flag was: %s", errID);\
281  } \
282  catch (...) \
283  { \
284  const char* errID = lumiera_error(); \
285  ERROR (_FLAG_, "%s failed with unknown exception; " \
286  "error flag is: %s" \
287  , _OP_DESCR_, errID?errID:"??"); \
288  }
289 
290 #define ERROR_LOG_AND_RETHROW(_FLAG_,_OP_DESCR_) \
291  catch (std::exception& problem) \
292  { \
293  const char* errID = lumiera_error(); \
294  WARN (_FLAG_, "%s failed: %s", _OP_DESCR_, problem.what()); \
295  TRACE (debugging, "Error flag was: %s", errID); \
296  throw; \
297  } \
298  catch (...) \
299  { \
300  const char* errID = lumiera_error(); \
301  ERROR (_FLAG_, "%s failed with unknown exception; " \
302  "error flag is: %s" \
303  , _OP_DESCR_, errID?errID:"??"); \
304  throw; \
305  }
306 
307 /******************************************************/
313 #define ON_EXCEPTION_RETURN(_VAL_,_OP_DESCR_) \
314  catch (std::exception& problem) \
315  { \
316  const char* errID = lumiera_error(); \
317  WARN (stage, "%s (Handler) failed: %s", \
318  _OP_DESCR_, problem.what()); \
319  TRACE (debugging, "Error flag was: %s", errID); \
320  return (_VAL_); \
321  } \
322  catch (...) \
323  { \
324  const char* errID = lumiera_error(); \
325  ERROR (stage, "(Handler) %s failed with " \
326  "unknown exception; error flag is: %s" \
327  , _OP_DESCR_, errID?errID:"??"); \
328  return (_VAL_); \
329  }
330 
331 
332 
333 /**************************************************/
337 #if 0
338 #ifdef NOBUG_ABORT
339 #undef NOBUG_ABORT
340 #define LUMIERA_NOBUG_LOCATION \
341  std::string (NOBUG_BASENAME(__FILE__)) +":"+ NOBUG_STRINGIZE(__LINE__) + ", function " + __func__
342 #define NOBUG_ABORT \
343  lumiera::error::assertion_terminate (LUMIERA_NOBUG_LOCATION);
344 #endif
345 #endif
346 
347 #endif // LUMIERA_ERROR_HPP_
lumiera_err getID() const noexcept
the internal Lumiera-error-ID (was set as C-errorstate in ctor)
Definition: error.hpp:103
string desc_
detailed description of the error situation for the developers
Definition: error.hpp:76
void throwOnError()
Check the lumiera error state, which maybe was set by C-code.
Definition: error.hpp:242
#define LUMIERA_ERROR_DECLARE(err)
Forward declare an error constant.
Definition: error.h:71
This header is for including and configuring NoBug.
Lumiera error handling (C interface).
Derived specific exceptions within Lumiera&#39;s exception hierarchy.
Definition: error.hpp:199
lumiera_err const id_
an LUMIERA_ERROR id, which is set as errorstate on construction
Definition: error.hpp:74
lumiera_err lumiera_error(void)
Get and clear current error state.
Definition: error-state.c:124
Helper to use a single extension point for specialised hash functions.
string const & getUsermsg() const noexcept
extract the message to be displayed for the user
Definition: error.hpp:110
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:122
const string cause_
description of first exception encountered in the chain
Definition: error.hpp:78
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:258
Error & prependInfo(string const &text) noexcept
give additional developer info.
Definition: error.hpp:139
string what_
buffer for generating the detailed description on demand
Definition: error.hpp:77
Lumiera public interface.
Definition: advice.cpp:113
string msg_
friendly message intended for users (to be localised)
Definition: error.hpp:75
Error & setUsermsg(string const &newMsg) noexcept
replace the previous or default friendly message for the user.
Definition: error.hpp:131
Interface and Base definition for all Lumiera Exceptions.
Definition: error.hpp:71