2007-08-14 08:14:21 +02:00
|
|
|
/*
|
2013-01-07 05:43:01 +01:00
|
|
|
ERROR-EXCEPTION - Lumiera exception classes
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2008-03-10 04:25:03 +01:00
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2008, Hermann Vosseler <Ichthyostega@web.de>
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2007-08-14 08:14:21 +02:00
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License as
|
2010-12-17 23:28:49 +01:00
|
|
|
published by the Free Software Foundation; either version 2 of
|
|
|
|
|
the License, or (at your option) any later version.
|
|
|
|
|
|
2007-08-14 08:14:21 +02:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2007-08-14 08:14:21 +02:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2007-08-14 08:14:21 +02:00
|
|
|
* *****************************************************/
|
|
|
|
|
|
|
|
|
|
|
2013-01-07 05:43:01 +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"
|
2016-01-10 11:21:34 +01:00
|
|
|
#include "lib/meta/util.hpp"
|
2008-12-17 17:53:32 +01:00
|
|
|
#include "lib/util.hpp"
|
2007-08-19 21:57:19 +02:00
|
|
|
|
2007-08-26 19:14:39 +02:00
|
|
|
#include <exception>
|
|
|
|
|
#include <typeinfo>
|
2007-08-19 21:57:19 +02:00
|
|
|
#include <iostream>
|
|
|
|
|
|
2007-08-26 19:14:39 +02:00
|
|
|
using util::isnil;
|
|
|
|
|
using std::exception;
|
|
|
|
|
|
2007-08-14 08:14:21 +02:00
|
|
|
|
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
|
2007-08-26 19:14:39 +02:00
|
|
|
*/
|
2008-12-07 06:28:26 +01:00
|
|
|
inline const string
|
2018-04-01 23:45:00 +02:00
|
|
|
default_usermsg (Error* exception_obj) noexcept
|
2007-08-26 19:14:39 +02:00
|
|
|
{
|
2008-03-10 06:09:44 +01:00
|
|
|
return string("Sorry, Lumiera encountered an internal error. (")
|
2016-01-10 11:21:34 +01:00
|
|
|
+ util::typeStr(*exception_obj) + ")";
|
2007-08-26 19:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
2010-02-13 20:56:41 +01:00
|
|
|
CStr
|
|
|
|
|
detailInfo ()
|
|
|
|
|
{
|
2018-04-01 23:45:00 +02:00
|
|
|
CStr detailinfo = lumiera_error_extra();
|
2010-02-13 20:56:41 +01:00
|
|
|
return isnil (detailinfo)? "Lumiera errorstate detected"
|
|
|
|
|
: detailinfo;
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-26 19:14:39 +02:00
|
|
|
|
|
|
|
|
/* constants to be used as error IDs */
|
2010-02-13 20:56:41 +01:00
|
|
|
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");
|
2008-03-10 06:09:44 +01:00
|
|
|
LUMIERA_ERROR_DEFINE (ASSERTION, "assertion failure");
|
2010-02-13 20:56:41 +01:00
|
|
|
|
2009-07-04 03:32:15 +02:00
|
|
|
/* 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");
|
2012-01-04 04:05:03 +01:00
|
|
|
LUMIERA_ERROR_DEFINE (CAPACITY, "predefined fixed storage capacity");
|
2012-01-02 06:11:27 +01:00
|
|
|
LUMIERA_ERROR_DEFINE (INDEX_BOUNDS, "index out of bounds");
|
2010-02-13 20:56:41 +01:00
|
|
|
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....");
|
2009-07-04 03:32:15 +02:00
|
|
|
|
2007-08-26 19:14:39 +02:00
|
|
|
} // namespace error
|
|
|
|
|
|
2010-02-13 20:56:41 +01:00
|
|
|
LUMIERA_ERROR_DEFINE (EXCEPTION, "generic Lumiera exception");
|
2007-08-26 19:14:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @note we set the C-style errorstate as a side effect */
|
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_{}
|
|
|
|
|
{
|
|
|
|
|
lumiera_error_set (this->id_, description.c_str());
|
|
|
|
|
}
|
2007-08-26 19:14:39 +02:00
|
|
|
|
|
|
|
|
|
2009-08-02 18:00:03 +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)}
|
|
|
|
|
{
|
|
|
|
|
lumiera_error_set (this->id_, description.c_str());
|
|
|
|
|
}
|
2007-08-26 19:14:39 +02:00
|
|
|
|
2008-11-30 03:08:48 +01:00
|
|
|
|
|
|
|
|
|
2007-08-14 08:14:21 +02: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.
|
2007-08-26 19:14:39 +02:00
|
|
|
* If a root cause can be obtained, this will be included in the
|
2007-08-14 08:14:21 +02:00
|
|
|
* generated output as well.
|
|
|
|
|
*/
|
2010-02-13 20:56:41 +01:00
|
|
|
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-14 08:14:21 +02:00
|
|
|
|
2008-11-30 03:08:48 +01:00
|
|
|
|
2007-08-26 19:14:39 +02:00
|
|
|
/** @internal get at the description message of the
|
|
|
|
|
* first exception encountered in a chain of exceptions
|
2007-08-14 08:14:21 +02:00
|
|
|
*/
|
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-14 08:14:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-08-26 19:14:39 +02:00
|
|
|
|
|
|
|
|
namespace error
|
|
|
|
|
{
|
|
|
|
|
|
2008-03-10 06:09:44 +01:00
|
|
|
void lumiera_unexpectedException () throw()
|
2007-08-26 19:14:39 +02:00
|
|
|
{
|
2010-02-13 20:56:41 +01:00
|
|
|
CCStr 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
|
|
|
|
|
|
|
|
std::cerr << "\n" << is_halted << "\n\n";
|
|
|
|
|
ERROR (NOBUG_ON, "%s", is_halted);
|
|
|
|
|
|
2010-02-13 20:56:41 +01:00
|
|
|
if (CCStr errorstate = lumiera_error ())
|
2007-08-26 19:14:39 +02:00
|
|
|
ERROR (NOBUG_ON, "last registered error was....\n%s", errorstate);
|
|
|
|
|
|
|
|
|
|
std::terminate();
|
|
|
|
|
}
|
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 "
|
2010-02-13 20:56:41 +01:00
|
|
|
"an internal consistency check.");
|
2007-08-26 19:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-04-14 05:15:16 +02:00
|
|
|
void install_unexpectedException_handler ()
|
|
|
|
|
{
|
2010-02-13 20:56:41 +01:00
|
|
|
std::set_unexpected (lumiera_unexpectedException);
|
2008-04-14 05:15:16 +02:00
|
|
|
}
|
|
|
|
|
|
2008-12-04 23:53:17 +01:00
|
|
|
namespace {
|
2010-02-13 20:56:41 +01:00
|
|
|
LifecycleHook schedule_ (ON_BASIC_INIT, &install_unexpectedException_handler);
|
2008-12-04 23:53:17 +01:00
|
|
|
}
|
2007-08-26 19:14:39 +02:00
|
|
|
|
2010-02-13 20:56:41 +01:00
|
|
|
|
|
|
|
|
} // namespace error
|
|
|
|
|
|
2008-03-10 06:09:44 +01:00
|
|
|
} // namespace lumiera
|