LUMIERA.clone/src/lib/error-exception.cpp

218 lines
6.5 KiB
C++
Raw Normal View History

/*
ERROR-EXCEPTION - Lumiera exception classes
2010-12-17 23:28:49 +01:00
Copyright: clarify and simplify the file headers * Lumiera source code always was copyrighted by individual contributors * there is no entity "Lumiera.org" which holds any copyrights * Lumiera source code is provided under the GPL Version 2+ == Explanations == Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above. For this to become legally effective, the ''File COPYING in the root directory is sufficient.'' The licensing header in each file is not strictly necessary, yet considered good practice; attaching a licence notice increases the likeliness that this information is retained in case someone extracts individual code files. However, it is not by the presence of some text, that legally binding licensing terms become effective; rather the fact matters that a given piece of code was provably copyrighted and published under a license. Even reformatting the code, renaming some variables or deleting parts of the code will not alter this legal situation, but rather creates a derivative work, which is likewise covered by the GPL! The most relevant information in the file header is the notice regarding the time of the first individual copyright claim. By virtue of this initial copyright, the first author is entitled to choose the terms of licensing. All further modifications are permitted and covered by the License. The specific wording or format of the copyright header is not legally relevant, as long as the intention to publish under the GPL remains clear. The extended wording was based on a recommendation by the FSF. It can be shortened, because the full terms of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
Copyright (C)
2008, Hermann Vosseler <Ichthyostega@web.de>
2010-12-17 23:28:49 +01:00
Copyright: clarify and simplify the file headers * Lumiera source code always was copyrighted by individual contributors * there is no entity "Lumiera.org" which holds any copyrights * Lumiera source code is provided under the GPL Version 2+ == Explanations == Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above. For this to become legally effective, the ''File COPYING in the root directory is sufficient.'' The licensing header in each file is not strictly necessary, yet considered good practice; attaching a licence notice increases the likeliness that this information is retained in case someone extracts individual code files. However, it is not by the presence of some text, that legally binding licensing terms become effective; rather the fact matters that a given piece of code was provably copyrighted and published under a license. Even reformatting the code, renaming some variables or deleting parts of the code will not alter this legal situation, but rather creates a derivative work, which is likewise covered by the GPL! The most relevant information in the file header is the notice regarding the time of the first individual copyright claim. By virtue of this initial copyright, the first author is entitled to choose the terms of licensing. All further modifications are permitted and covered by the License. The specific wording or format of the copyright header is not legally relevant, as long as the intention to publish under the GPL remains clear. The extended wording was based on a recommendation by the FSF. It can be shortened, because the full terms of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
  **Lumiera** is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by the
  Free Software Foundation; either version 2 of the License, or (at your
  option) any later version. See the file COPYING for further details.
2010-12-17 23:28:49 +01:00
Copyright: clarify and simplify the file headers * Lumiera source code always was copyrighted by individual contributors * there is no entity "Lumiera.org" which holds any copyrights * Lumiera source code is provided under the GPL Version 2+ == Explanations == Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above. For this to become legally effective, the ''File COPYING in the root directory is sufficient.'' The licensing header in each file is not strictly necessary, yet considered good practice; attaching a licence notice increases the likeliness that this information is retained in case someone extracts individual code files. However, it is not by the presence of some text, that legally binding licensing terms become effective; rather the fact matters that a given piece of code was provably copyrighted and published under a license. Even reformatting the code, renaming some variables or deleting parts of the code will not alter this legal situation, but rather creates a derivative work, which is likewise covered by the GPL! The most relevant information in the file header is the notice regarding the time of the first individual copyright claim. By virtue of this initial copyright, the first author is entitled to choose the terms of licensing. All further modifications are permitted and covered by the License. The specific wording or format of the copyright header is not legally relevant, as long as the intention to publish under the GPL remains clear. The extended wording was based on a recommendation by the FSF. It can be shortened, because the full terms of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
* *****************************************************************/
/** @file error-exception.cpp
** implementation of C++-style error handling.
** This compilation unit defines a custom exception class hierarchy,
** and is tightly integrated with the C-style error handling.
**
** @see error-state.c
** @see error.hpp
**
*/
2007-08-26 19:14:39 +02:00
2008-12-27 00:53:35 +01:00
#include "lib/error.hpp"
#include "include/lifecycle.h"
#include "lib/meta/util.hpp"
#include "lib/util.hpp"
2007-08-26 19:14:39 +02:00
#include <exception>
#include <typeinfo>
#include <iostream>
2007-08-26 19:14:39 +02:00
using util::isnil;
using std::exception;
2008-11-30 03:08:48 +01:00
namespace lumiera {
namespace error {
2007-08-26 19:14:39 +02:00
/** the message shown to the user per default
* if an exception reaches one of the top-level
* catch clauses.
2008-11-30 03:08:48 +01:00
* @todo to be localised
* @todo develop a framework to set more specific yet friendly messages
2007-08-26 19:14:39 +02:00
*/
inline const string
default_usermsg (Error*) noexcept
2007-08-26 19:14:39 +02:00
{
return "Sorry, Lumiera encountered an internal error.";
2007-08-26 19:14:39 +02:00
}
CStr
detailInfo ()
{
2018-04-01 23:45:00 +02:00
CStr detailinfo = lumiera_error_extra();
return isnil (detailinfo)? "Lumiera errorstate detected"
: detailinfo;
}
2007-08-26 19:14:39 +02:00
/* constants to be used as error IDs */
LUMIERA_ERROR_DEFINE (LOGIC , "internal logic broken");
LUMIERA_ERROR_DEFINE (FATAL , "floundered");
LUMIERA_ERROR_DEFINE (CONFIG , "misconfiguration");
LUMIERA_ERROR_DEFINE (STATE , "unforeseen state");
LUMIERA_ERROR_DEFINE (FLAG , "non-cleared lumiera errorstate");
LUMIERA_ERROR_DEFINE (INVALID , "invalid input or parameters");
LUMIERA_ERROR_DEFINE (EXTERNAL , "failure in external service");
LUMIERA_ERROR_DEFINE (EXCEPTION, "generic Lumiera exception");
2008-03-10 06:09:44 +01:00
LUMIERA_ERROR_DEFINE (ASSERTION, "assertion failure");
/* some further generic error situations */
2018-04-01 23:45:00 +02:00
LUMIERA_ERROR_DEFINE (LIFECYCLE, "Lifecycle assumptions violated");
LUMIERA_ERROR_DEFINE (WRONG_TYPE, "runtime type mismatch");
2009-12-21 05:44:29 +01:00
LUMIERA_ERROR_DEFINE (ITER_EXHAUST, "end of sequence reached");
LUMIERA_ERROR_DEFINE (CAPACITY, "predefined fixed storage capacity");
LUMIERA_ERROR_DEFINE (SAFETY_LIMIT, "exceeding fixed internal safety limit");
LUMIERA_ERROR_DEFINE (INDEX_BOUNDS, "index out of bounds");
LUMIERA_ERROR_DEFINE (BOTTOM_VALUE, "invalid or NIL value");
2018-04-01 23:45:00 +02:00
LUMIERA_ERROR_DEFINE (UNCONNECTED, "missing connection");
LUMIERA_ERROR_DEFINE (UNIMPLEMENTED,"using a feature not yet implemented....");
2007-08-26 19:14:39 +02:00
} // namespace error
2018-04-01 23:45:00 +02:00
Error::Error (string description, lumiera_err const id) noexcept
: std::exception{}
, id_{id}
, msg_{error::default_usermsg (this)}
, desc_{description}
, cause_{}
{ }
2007-08-26 19:14:39 +02:00
Error::Error (std::exception const& cause,
2018-04-01 23:45:00 +02:00
string description, lumiera_err const id) noexcept
: std::exception{}
, id_{id}
, msg_{error::default_usermsg (this)}
, desc_{description}
, cause_{extractCauseMsg(cause)}
{
string detailInfo{description + (isnil(cause_)? "" : " | cause = "+cause_)};
2018-04-01 23:45:00 +02:00
}
2007-08-26 19:14:39 +02:00
2008-11-30 03:08:48 +01:00
/** Description of the problem, including the internal char constant
2008-03-10 06:09:44 +01:00
* in accordance to Lumiera's error identification scheme.
* If a root cause can be obtained, this will be included
* in the generated output as well.
*/
CStr
2018-04-01 23:45:00 +02:00
Error::what() const noexcept
2007-08-26 19:14:39 +02:00
{
if (isnil (this->what_))
{
what_ = string(id_);
if (!isnil (desc_)) what_ += " ("+desc_+").";
if (!isnil (cause_)) what_ += string(" -- caused by: ") + cause_;
}
return what_.c_str();
2007-08-26 19:14:39 +02:00
}
2008-11-30 03:08:48 +01:00
/** @internal get at the description message of the
2007-08-26 19:14:39 +02:00
* first exception encountered in a chain of exceptions
*/
2007-08-26 19:14:39 +02:00
const string
2018-04-01 23:45:00 +02:00
Error::extractCauseMsg (const exception& cause) noexcept
2007-08-26 19:14:39 +02:00
{
2018-04-01 23:45:00 +02:00
const Error* err = dynamic_cast<const Error*> (&cause);
2007-08-26 19:14:39 +02:00
if (err)
2008-09-05 17:01:24 +02:00
{
if (isnil (err->cause_))
return cause.what(); // cause is root cause
else
return err->cause_; // cause was caused by another exception
}
2007-08-26 19:14:39 +02:00
// unknown other exception type
return cause.what ();
}
2007-08-26 19:14:39 +02:00
namespace error {
namespace {
void install_unexpectedException_handler ()
{
std::set_terminate (lumiera_unexpectedException);
}
LifecycleHook schedule_ (ON_BASIC_INIT, &install_unexpectedException_handler);
std::terminate_handler nextHandler = nullptr;
}
2007-08-26 19:14:39 +02:00
void lumiera_unexpectedException () noexcept
2007-08-26 19:14:39 +02:00
{
CStr is_halted
2008-03-10 06:09:44 +01:00
= "### Lumiera halted due to an unexpected Error ###";
2007-08-26 19:14:39 +02:00
ERROR (NOBUG_ON, "%s", is_halted);
std::cerr << "\n" << is_halted << "\n\n";
2007-08-26 19:14:39 +02:00
try { // -----find-out-about-any-Exceptions--------
auto lastException = std::current_exception();
if (lastException) {
std::rethrow_exception (lastException);
}
} catch(const lumiera::Error& lerr) {
std::cout << "\n+++ Caught Exception " << lerr.getID() << "\n\n";
ERROR (NOBUG_ON, "+++ caught %s\n+++ messg: %s\n+++ descr: %s"
, cStr(util::typeStr(lerr))
, cStr(lerr.getUsermsg())
, cStr(lerr.what())
);
if (not isnil(lerr.rootCause()))
ERROR (NOBUG_ON, "+++ cause: %s",cStr(lerr.rootCause()));
} catch(const std::exception& e) {
ERROR (NOBUG_ON, "Generic Exception: %s", e.what());
std::cout << "+++ Caught Exception \"" << e.what() << "\"\n";
} catch(...) {
ERROR (NOBUG_ON, "FATAL -- unknown exception");
}
if (CStr errorstate = lumiera_error ())
2007-08-26 19:14:39 +02:00
ERROR (NOBUG_ON, "last registered error was....\n%s", errorstate);
if (nextHandler)
nextHandler();
else
std::abort();
2007-08-26 19:14:39 +02:00
}
2008-11-30 03:08:48 +01:00
2007-08-26 19:14:39 +02:00
void assertion_terminate (const string& location)
{
2008-03-10 06:09:44 +01:00
throw Fatal (location, LUMIERA_ERROR_ASSERTION)
2007-08-26 19:14:39 +02:00
.setUsermsg("Program terminated because of violating "
"an internal consistency check.");
2007-08-26 19:14:39 +02:00
}
} // namespace error
2008-03-10 06:09:44 +01:00
} // namespace lumiera