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)
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 
42 
43 #define LERR_(_NAME_) lumiera::error::LUMIERA_ERROR_##_NAME_
44 
45 namespace lumiera {
46 
47  using std::string;
48  using CStr = const char*;
49 
50  namespace error {
52  LUMIERA_ERROR_DECLARE(EXCEPTION);
53  }
54 
55 
62  class Error
63  : public std::exception
64  {
65  lumiera_err const id_;
66  string msg_;
67  string desc_;
68  mutable string what_;
69  const string cause_;
70 
71 
72  public:
73  virtual ~Error () noexcept { };
74 
75 
76  Error (string description=""
77  ,lumiera_err const id =LERR_(EXCEPTION)) noexcept;
78  Error (std::exception const& cause
79  ,string description=""
80  ,lumiera_err const id =LERR_(EXCEPTION)) noexcept;
81 
82  Error (Error &&) = default;
83  Error (Error const&) = default;
84  Error& operator= (Error &&) = delete;
85  Error& operator= (Error const&) = delete;
86 
88  virtual CStr
89  what () const noexcept override;
90 
93  lumiera_err
94  getID () const noexcept
95  {
96  return id_;
97  }
98 
100  string const&
101  getUsermsg () const noexcept
102  {
103  return msg_;
104  }
105 
112  string const&
113  rootCause () const noexcept
114  {
115  return cause_;
116  }
117 
121  Error&
122  setUsermsg (string const& newMsg) noexcept
123  {
124  msg_ = newMsg;
125  return *this;
126  }
127 
129  Error&
130  prependInfo (string const& text) noexcept
131  {
132  desc_.insert (0, text);
133  return *this;
134  }
135 
136 
137  private:
138  static const string
139  extractCauseMsg (std::exception const&) noexcept;
140  };
141 
142 
143 
144 
145 
146 
147  /* === Exception sub-categories === */
148 
149  namespace error {
150 
157  void lumiera_unexpectedException () noexcept;
158 
160  void assertion_terminate (const string& location);
161 
162 
163  /* constants to be used as error IDs */
164  LUMIERA_ERROR_DECLARE (LOGIC );
165  LUMIERA_ERROR_DECLARE (FATAL );
166  LUMIERA_ERROR_DECLARE (CONFIG );
167  LUMIERA_ERROR_DECLARE (STATE );
168  LUMIERA_ERROR_DECLARE (FLAG );
169  LUMIERA_ERROR_DECLARE (INVALID );
170  LUMIERA_ERROR_DECLARE (EXTERNAL );
171  LUMIERA_ERROR_DECLARE (ASSERTION);
172 
173  /* generic error situations */
174  LUMIERA_ERROR_DECLARE (LIFECYCLE);
175  LUMIERA_ERROR_DECLARE (WRONG_TYPE);
176  LUMIERA_ERROR_DECLARE (ITER_EXHAUST);
177  LUMIERA_ERROR_DECLARE (CAPACITY);
178  LUMIERA_ERROR_DECLARE (SAFETY_LIMIT);
179  LUMIERA_ERROR_DECLARE (INDEX_BOUNDS);
180  LUMIERA_ERROR_DECLARE (BOTTOM_VALUE);
181  LUMIERA_ERROR_DECLARE (UNCONNECTED);
182  LUMIERA_ERROR_DECLARE (UNIMPLEMENTED);
183 
184 
185 
189  template<lumiera_err const& eID, class PAR =Error>
191  : public PAR
192  {
193  public:
194  LumieraError (std::string description=""
195  ,lumiera_err const id=eID) noexcept
196  : PAR{description, id? id:eID}
197  { }
198  LumieraError (std::exception const& cause
199  ,std::string description=""
200  ,lumiera_err const id=eID) noexcept
201  : PAR{cause, description, id? id:eID}
202  { }
203  };
204 
205  //----CLASS-------------------ID--------------PARENT------
213 
214 
217  void install_unexpectedException_handler();
218 
220  CStr detailInfo();
221 
222  } // namespace error
223 
224 
225 
232  inline void
234  {
235  if (lumiera_err errorFlag =lumiera_error())
236  {
237  throw error::Flag( error::detailInfo()
238  , errorFlag);
239  } }
240 
247  template<class EX>
248  inline void
249  maybeThrow (string description ="")
250  {
251  if (lumiera_err errorFlag =lumiera_error())
252  {
253  throw EX (error::Flag{error::detailInfo(), errorFlag}
254  ,description);
255  } }
256 
257 
258 } // namespace lumiera
259 
260 /**************************************************/
266 #define ERROR_LOG_AND_IGNORE(_FLAG_,_OP_DESCR_) \
267  catch (std::exception& problem) \
268  { \
269  const char* errID = lumiera_error(); \
270  WARN (_FLAG_, "%s failed: %s", _OP_DESCR_, problem.what()); \
271  TRACE (debugging, "Error flag was: %s", errID);\
272  } \
273  catch (...) \
274  { \
275  const char* errID = lumiera_error(); \
276  ERROR (_FLAG_, "%s failed with unknown exception; " \
277  "error flag is: %s" \
278  , _OP_DESCR_, errID?errID:"??"); \
279  }
280 
281 #define ERROR_LOG_AND_RETHROW(_FLAG_,_OP_DESCR_) \
282  catch (std::exception& problem) \
283  { \
284  const char* errID = lumiera_error(); \
285  WARN (_FLAG_, "%s failed: %s", _OP_DESCR_, problem.what()); \
286  TRACE (debugging, "Error flag was: %s", errID); \
287  throw; \
288  } \
289  catch (...) \
290  { \
291  const char* errID = lumiera_error(); \
292  ERROR (_FLAG_, "%s failed with unknown exception; " \
293  "error flag is: %s" \
294  , _OP_DESCR_, errID?errID:"??"); \
295  throw; \
296  }
297 
298 /******************************************************/
304 #define ON_EXCEPTION_RETURN(_VAL_,_OP_DESCR_) \
305  catch (std::exception& problem) \
306  { \
307  const char* errID = lumiera_error(); \
308  WARN (stage, "%s (Handler) failed: %s", \
309  _OP_DESCR_, problem.what()); \
310  TRACE (debugging, "Error flag was: %s", errID); \
311  return (_VAL_); \
312  } \
313  catch (...) \
314  { \
315  const char* errID = lumiera_error(); \
316  ERROR (stage, "(Handler) %s failed with " \
317  "unknown exception; error flag is: %s" \
318  , _OP_DESCR_, errID?errID:"??"); \
319  return (_VAL_); \
320  }
321 
322 
323 
324 /**************************************************/
328 #if 0
329 #ifdef NOBUG_ABORT
330 #undef NOBUG_ABORT
331 #define LUMIERA_NOBUG_LOCATION \
332  std::string (NOBUG_BASENAME(__FILE__)) +":"+ NOBUG_STRINGIZE(__LINE__) + ", function " + __func__
333 #define NOBUG_ABORT \
334  lumiera::error::assertion_terminate (LUMIERA_NOBUG_LOCATION);
335 #endif
336 #endif
337 
338 #endif // LUMIERA_ERROR_HPP_
lumiera_err getID() const noexcept
the internal Lumiera-error-ID (was set as C-errorstate in ctor)
Definition: error.hpp:94
string desc_
detailed description of the error situation for the developers
Definition: error.hpp:67
void throwOnError()
Check the lumiera error state, which maybe was set by C-code.
Definition: error.hpp:233
#define LUMIERA_ERROR_DECLARE(err)
Forward declare an error constant.
Definition: error.h:62
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:190
lumiera_err const id_
an LUMIERA_ERROR id, which is set as errorstate on construction
Definition: error.hpp:65
lumiera_err lumiera_error(void)
Get and clear current error state.
Definition: error-state.c:115
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:101
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:113
const string cause_
description of first exception encountered in the chain
Definition: error.hpp:69
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:249
Error & prependInfo(string const &text) noexcept
give additional developer info.
Definition: error.hpp:130
string what_
buffer for generating the detailed description on demand
Definition: error.hpp:68
Lumiera public interface.
Definition: advice.cpp:104
string msg_
friendly message intended for users (to be localised)
Definition: error.hpp:66
Error & setUsermsg(string const &newMsg) noexcept
replace the previous or default friendly message for the user.
Definition: error.hpp:122
Interface and Base definition for all Lumiera Exceptions.
Definition: error.hpp:62