Lumiera  0.pre.03
»edit your freedom«
error-exception.cpp
Go to the documentation of this file.
1 /*
2  ERROR-EXCEPTION - Lumiera exception classes
3 
4  Copyright (C) Lumiera.org
5  2008, 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 
35 #include "lib/error.hpp"
36 #include "include/lifecycle.h"
37 #include "lib/meta/util.hpp"
38 #include "lib/util.hpp"
39 
40 #include <exception>
41 #include <typeinfo>
42 #include <iostream>
43 
44 using util::isnil;
45 using std::exception;
46 
47 
48 namespace lumiera {
49  namespace error {
50 
57  inline const string
58  default_usermsg (Error*) noexcept
59  {
60  return "Sorry, Lumiera encountered an internal error.";
61  }
62 
63  CStr
65  {
66  CStr detailinfo = lumiera_error_extra();
67  return isnil (detailinfo)? "Lumiera errorstate detected"
68  : detailinfo;
69  }
70 
71 
72  /* constants to be used as error IDs */
73  LUMIERA_ERROR_DEFINE (LOGIC , "internal logic broken");
74  LUMIERA_ERROR_DEFINE (FATAL , "floundered");
75  LUMIERA_ERROR_DEFINE (CONFIG , "misconfiguration");
76  LUMIERA_ERROR_DEFINE (STATE , "unforeseen state");
77  LUMIERA_ERROR_DEFINE (FLAG , "non-cleared lumiera errorstate");
78  LUMIERA_ERROR_DEFINE (INVALID , "invalid input or parameters");
79  LUMIERA_ERROR_DEFINE (EXTERNAL , "failure in external service");
80  LUMIERA_ERROR_DEFINE (EXCEPTION, "generic Lumiera exception");
81  LUMIERA_ERROR_DEFINE (ASSERTION, "assertion failure");
82 
83  /* some further generic error situations */
84  LUMIERA_ERROR_DEFINE (LIFECYCLE, "Lifecycle assumptions violated");
85  LUMIERA_ERROR_DEFINE (WRONG_TYPE, "runtime type mismatch");
86  LUMIERA_ERROR_DEFINE (ITER_EXHAUST, "end of sequence reached");
87  LUMIERA_ERROR_DEFINE (CAPACITY, "predefined fixed storage capacity");
88  LUMIERA_ERROR_DEFINE (SAFETY_LIMIT, "exceeding fixed internal safety limit");
89  LUMIERA_ERROR_DEFINE (INDEX_BOUNDS, "index out of bounds");
90  LUMIERA_ERROR_DEFINE (BOTTOM_VALUE, "invalid or NIL value");
91  LUMIERA_ERROR_DEFINE (UNCONNECTED, "missing connection");
92  LUMIERA_ERROR_DEFINE (UNIMPLEMENTED,"using a feature not yet implemented....");
93 
94  } // namespace error
95 
96 
97 
98 
99  Error::Error (string description, lumiera_err const id) noexcept
100  : std::exception{}
101  , id_{id}
102  , msg_{error::default_usermsg (this)}
103  , desc_{description}
104  , cause_{}
105  { }
106 
107 
108  Error::Error (std::exception const& cause,
109  string description, lumiera_err const id) noexcept
110  : std::exception{}
111  , id_{id}
112  , msg_{error::default_usermsg (this)}
113  , desc_{description}
114  , cause_{extractCauseMsg(cause)}
115  {
116  string detailInfo{description + (isnil(cause_)? "" : " | cause = "+cause_)};
117  }
118 
119 
120 
121 
127  CStr
128  Error::what() const noexcept
129  {
130  if (isnil (this->what_))
131  {
132  what_ = string(id_);
133  if (!isnil (desc_)) what_ += " ("+desc_+").";
134  if (!isnil (cause_)) what_ += string(" -- caused by: ") + cause_;
135  }
136  return what_.c_str();
137  }
138 
139 
143  const string
144  Error::extractCauseMsg (const exception& cause) noexcept
145  {
146  const Error* err = dynamic_cast<const Error*> (&cause);
147  if (err)
148  {
149  if (isnil (err->cause_))
150  return cause.what(); // cause is root cause
151  else
152  return err->cause_; // cause was caused by another exception
153  }
154  // unknown other exception type
155  return cause.what ();
156  }
157 
158 
159 
160 
161 
162 
163 
164  namespace error {
165  namespace {
167  {
168  std::set_terminate (lumiera_unexpectedException);
169  }
171 
172  std::terminate_handler nextHandler = nullptr;
173  }
174 
175 
177  {
178  CStr is_halted
179  = "### Lumiera halted due to an unexpected Error ###";
180 
181  ERROR (NOBUG_ON, "%s", is_halted);
182  std::cerr << "\n" << is_halted << "\n\n";
183 
184 
185  try { // -----find-out-about-any-Exceptions--------
186  auto lastException = std::current_exception();
187  if (lastException) {
188  std::rethrow_exception (lastException);
189  }
190  } catch(const lumiera::Error& lerr) {
191  std::cout << "\n+++ Caught Exception " << lerr.getID() << "\n\n";
192  ERROR (NOBUG_ON, "+++ caught %s\n+++ messg: %s\n+++ descr: %s"
193  , cStr(util::typeStr(lerr))
194  , cStr(lerr.getUsermsg())
195  , cStr(lerr.what())
196  );
197  if (not isnil(lerr.rootCause()))
198  ERROR (NOBUG_ON, "+++ cause: %s",cStr(lerr.rootCause()));
199 
200  } catch(const std::exception& e) {
201  ERROR (NOBUG_ON, "Generic Exception: %s", e.what());
202  std::cout << "+++ Caught Exception \"" << e.what() << "\"\n";
203  } catch(...) {
204  ERROR (NOBUG_ON, "FATAL -- unknown exception");
205  }
206 
207  if (CStr errorstate = lumiera_error ())
208  ERROR (NOBUG_ON, "last registered error was....\n%s", errorstate);
209 
210  if (nextHandler)
211  nextHandler();
212  else
213  std::abort();
214  }
215 
216 
217  void assertion_terminate (const string& location)
218  {
219  throw Fatal (location, LUMIERA_ERROR_ASSERTION)
220  .setUsermsg("Program terminated because of violating "
221  "an internal consistency check.");
222  }
223 
224  } // namespace error
225 
226 } // namespace lumiera
void assertion_terminate(const string &location)
throw an error::Fatal indicating "assertion failure"
const char * ON_BASIC_INIT
automatic static init. treated specially to run as soon as possible
Installing and invoking of application lifecycle event callbacks.
lumiera_err getID() const noexcept
the internal Lumiera-error-ID (was set as C-errorstate in ctor)
Definition: error.hpp:103
CStr cStr(std::string const &rendered)
convenience shortcut: forced conversion to c-String via string.
Definition: symbol.hpp:68
Simple and lightweight helpers for metaprogramming and type detection.
virtual CStr what() const noexcept override
std::exception interface : yield a diagnostic message
Error(string description="", lumiera_err const id=LERR_(EXCEPTION)) noexcept
this is an interface
Derived specific exceptions within Lumiera&#39;s exception hierarchy.
Definition: error.hpp:199
static const string extractCauseMsg(std::exception const &) noexcept
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
lumiera_err lumiera_error(void)
Get and clear current error state.
Definition: error-state.c:124
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
define and register a callback for a specific lifecycle event.
Definition: lifecycle.h:76
Lumiera error handling (C++ interface).
Lumiera public interface.
Definition: advice.cpp:113
void install_unexpectedException_handler()
install our own handler for undeclared exceptions.
const string default_usermsg(Error *) noexcept
the message shown to the user per default if an exception reaches one of the top-level catch clauses...
void lumiera_unexpectedException() noexcept
global function for handling unknown exceptions encountered at functions declaring not to throw this ...
const char * lumiera_error_extra(void)
Query the extra context for the last error.
Definition: error-state.c:135
Interface and Base definition for all Lumiera Exceptions.
Definition: error.hpp:71
#define LUMIERA_ERROR_DEFINE(err, msg)
Definition and initialisation of an error constant.
Definition: error.h:80