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)
5  2008, 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 
26 #include "lib/error.hpp"
27 #include "include/lifecycle.h"
28 #include "lib/meta/util.hpp"
29 #include "lib/util.hpp"
30 
31 #include <exception>
32 #include <typeinfo>
33 #include <iostream>
34 
35 using util::isnil;
36 using std::exception;
37 
38 
39 namespace lumiera {
40  namespace error {
41 
48  inline const string
49  default_usermsg (Error*) noexcept
50  {
51  return "Sorry, Lumiera encountered an internal error.";
52  }
53 
54  CStr
56  {
57  CStr detailinfo = lumiera_error_extra();
58  return isnil (detailinfo)? "Lumiera errorstate detected"
59  : detailinfo;
60  }
61 
62 
63  /* constants to be used as error IDs */
64  LUMIERA_ERROR_DEFINE (LOGIC , "internal logic broken");
65  LUMIERA_ERROR_DEFINE (FATAL , "floundered");
66  LUMIERA_ERROR_DEFINE (CONFIG , "misconfiguration");
67  LUMIERA_ERROR_DEFINE (STATE , "unforeseen state");
68  LUMIERA_ERROR_DEFINE (FLAG , "non-cleared lumiera errorstate");
69  LUMIERA_ERROR_DEFINE (INVALID , "invalid input or parameters");
70  LUMIERA_ERROR_DEFINE (EXTERNAL , "failure in external service");
71  LUMIERA_ERROR_DEFINE (EXCEPTION, "generic Lumiera exception");
72  LUMIERA_ERROR_DEFINE (ASSERTION, "assertion failure");
73 
74  /* some further generic error situations */
75  LUMIERA_ERROR_DEFINE (LIFECYCLE, "Lifecycle assumptions violated");
76  LUMIERA_ERROR_DEFINE (WRONG_TYPE, "runtime type mismatch");
77  LUMIERA_ERROR_DEFINE (ITER_EXHAUST, "end of sequence reached");
78  LUMIERA_ERROR_DEFINE (CAPACITY, "predefined fixed storage capacity");
79  LUMIERA_ERROR_DEFINE (SAFETY_LIMIT, "exceeding fixed internal safety limit");
80  LUMIERA_ERROR_DEFINE (INDEX_BOUNDS, "index out of bounds");
81  LUMIERA_ERROR_DEFINE (BOTTOM_VALUE, "invalid or NIL value");
82  LUMIERA_ERROR_DEFINE (UNCONNECTED, "missing connection");
83  LUMIERA_ERROR_DEFINE (UNIMPLEMENTED,"using a feature not yet implemented....");
84 
85  } // namespace error
86 
87 
88 
89 
90  Error::Error (string description, lumiera_err const id) noexcept
91  : std::exception{}
92  , id_{id}
93  , msg_{error::default_usermsg (this)}
94  , desc_{description}
95  , cause_{}
96  { }
97 
98 
99  Error::Error (std::exception const& cause,
100  string description, lumiera_err const id) noexcept
101  : std::exception{}
102  , id_{id}
103  , msg_{error::default_usermsg (this)}
104  , desc_{description}
105  , cause_{extractCauseMsg(cause)}
106  {
107  string detailInfo{description + (isnil(cause_)? "" : " | cause = "+cause_)};
108  }
109 
110 
111 
112 
118  CStr
119  Error::what() const noexcept
120  {
121  if (isnil (this->what_))
122  {
123  what_ = string(id_);
124  if (!isnil (desc_)) what_ += " ("+desc_+").";
125  if (!isnil (cause_)) what_ += string(" -- caused by: ") + cause_;
126  }
127  return what_.c_str();
128  }
129 
130 
134  const string
135  Error::extractCauseMsg (const exception& cause) noexcept
136  {
137  const Error* err = dynamic_cast<const Error*> (&cause);
138  if (err)
139  {
140  if (isnil (err->cause_))
141  return cause.what(); // cause is root cause
142  else
143  return err->cause_; // cause was caused by another exception
144  }
145  // unknown other exception type
146  return cause.what ();
147  }
148 
149 
150 
151 
152 
153 
154 
155  namespace error {
156  namespace {
158  {
159  std::set_terminate (lumiera_unexpectedException);
160  }
162 
163  std::terminate_handler nextHandler = nullptr;
164  }
165 
166 
168  {
169  CStr is_halted
170  = "### Lumiera halted due to an unexpected Error ###";
171 
172  ERROR (NOBUG_ON, "%s", is_halted);
173  std::cerr << "\n" << is_halted << "\n\n";
174 
175 
176  try { // -----find-out-about-any-Exceptions--------
177  auto lastException = std::current_exception();
178  if (lastException) {
179  std::rethrow_exception (lastException);
180  }
181  } catch(const lumiera::Error& lerr) {
182  std::cout << "\n+++ Caught Exception " << lerr.getID() << "\n\n";
183  ERROR (NOBUG_ON, "+++ caught %s\n+++ messg: %s\n+++ descr: %s"
184  , cStr(util::typeStr(lerr))
185  , cStr(lerr.getUsermsg())
186  , cStr(lerr.what())
187  );
188  if (not isnil(lerr.rootCause()))
189  ERROR (NOBUG_ON, "+++ cause: %s",cStr(lerr.rootCause()));
190 
191  } catch(const std::exception& e) {
192  ERROR (NOBUG_ON, "Generic Exception: %s", e.what());
193  std::cout << "+++ Caught Exception \"" << e.what() << "\"\n";
194  } catch(...) {
195  ERROR (NOBUG_ON, "FATAL -- unknown exception");
196  }
197 
198  if (CStr errorstate = lumiera_error ())
199  ERROR (NOBUG_ON, "last registered error was....\n%s", errorstate);
200 
201  if (nextHandler)
202  nextHandler();
203  else
204  std::abort();
205  }
206 
207 
208  void assertion_terminate (const string& location)
209  {
210  throw Fatal (location, LUMIERA_ERROR_ASSERTION)
211  .setUsermsg("Program terminated because of violating "
212  "an internal consistency check.");
213  }
214 
215  } // namespace error
216 
217 } // 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:94
CStr cStr(std::string const &rendered)
convenience shortcut: forced conversion to c-String via string.
Definition: symbol.hpp:59
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:190
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:115
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
define and register a callback for a specific lifecycle event.
Definition: lifecycle.h:67
Lumiera error handling (C++ interface).
Lumiera public interface.
Definition: advice.cpp:104
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:126
Interface and Base definition for all Lumiera Exceptions.
Definition: error.hpp:62
#define LUMIERA_ERROR_DEFINE(err, msg)
Definition and initialisation of an error constant.
Definition: error.h:71