Loading...
Searching...
No Matches
CriticalError.hpp
Go to the documentation of this file.
/*
Copyright 2025 Equinor ASA
This file is part of the Open Porous Media project (OPM).
OPM 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 3 of the License, or
(at your option) any later version.
OPM 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.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OPM_COMMON_CRITICAL_ERROR_HPP
#define OPM_COMMON_CRITICAL_ERROR_HPP
#include <exception>
#include <iostream>
#include <stdexcept>
#include <string>
namespace Opm
{
class CriticalError : public std::exception
{
public:
explicit CriticalError(const std::string_view& message,
std::exception_ptr inner_exception = nullptr)
: m_message(message)
, m_innerException(inner_exception)
{
}
const char* what() const noexcept override
{
return m_message.c_str();
}
std::exception_ptr getInnerException() const
{
return m_innerException;
}
private:
std::string m_message;
std::exception_ptr m_innerException;
};
namespace detail
{
inline std::string makeCriticalErrorMessageErrorHint(const std::string& text)
{
return std::string("\nError hint: ") + text;
}
inline std::string makeCriticalErrorMessageErrorHint()
{
return std::string("");
}
} // namespace detail
#define OPM_CATCH_AND_RETHROW_AS_CRITICAL_ERROR(...) \
catch (const ::Opm::CriticalError&) \
{ \
throw; \
} \
catch (const std::exception& exp) \
{ \
const auto messageForCriticalError = std::string("Error rethrown as CriticalError at [") + __FILE__ + ":" \
+ std::to_string(__LINE__) + "].\nOriginal error: " + exp.what() \
+ ::Opm::detail::makeCriticalErrorMessageErrorHint(__VA_ARGS__); \
throw ::Opm::CriticalError(messageForCriticalError, std::current_exception()); \
} \
catch (...) \
{ \
const auto messageForCriticalError = std::string("Error rethrown as CriticalError at [") + __FILE__ + ":" \
+ std::to_string(__LINE__) + "]. Unknown original error." \
+ ::Opm::detail::makeCriticalErrorMessageErrorHint(__VA_ARGS__); \
throw ::Opm::CriticalError(messageForCriticalError, std::current_exception()); \
}
#define OPM_TRY_THROW_AS_CRITICAL_ERROR(expr, ...) \
try { \
expr; \
} catch (const ::Opm::CriticalError&) { \
throw; \
} catch (const std::exception& exp) { \
const auto messageForCriticalError = std::string("Error rethrown as CriticalError at [") + __FILE__ + ":" \
+ std::to_string(__LINE__) + "].\nOriginal error: " + exp.what() \
+ ::Opm::detail::makeCriticalErrorMessageErrorHint(__VA_ARGS__); \
throw ::Opm::CriticalError(messageForCriticalError, std::current_exception()); \
} catch (...) { \
const auto messageForCriticalError = std::string("Error rethrown as CriticalError at [") + __FILE__ + ":" \
+ std::to_string(__LINE__) + "]. Unknown original error." \
+ ::Opm::detail::makeCriticalErrorMessageErrorHint(__VA_ARGS__); \
throw ::Opm::CriticalError(messageForCriticalError, std::current_exception()); \
}
} // namespace Opm
#endif // OPM_COMMON_CRITICAL_ERROR_HPP
CriticalError::CriticalError
CriticalError(const std::string_view &message, std::exception_ptr inner_exception=nullptr)
Constructs a CriticalError with a specified message and an optional inner exception.
Definition CriticalError.hpp:82
CriticalError::getInnerException
std::exception_ptr getInnerException() const
Retrieves the inner exception.
Definition CriticalError.hpp:105
CriticalError::what
const char * what() const noexcept override
Returns the error message.
Definition CriticalError.hpp:95
Opm
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition Exceptions.hpp:30