icinga2/lib/base/exception.hpp

147 lines
4.7 KiB
C++
Raw Normal View History

/******************************************************************************
* Icinga 2 *
2018-01-02 12:06:00 +01:00
* Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
* *
* This program 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. *
* *
* 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. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
2012-05-11 13:33:57 +02:00
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
#ifndef EXCEPTION_H
#define EXCEPTION_H
2014-05-25 16:23:35 +02:00
#include "base/i2-base.hpp"
#include "base/string.hpp"
2014-05-25 16:23:35 +02:00
#include "base/stacktrace.hpp"
#include "base/context.hpp"
#include "base/utility.hpp"
#include "base/debuginfo.hpp"
#include "base/dictionary.hpp"
#include "base/configobject.hpp"
2013-03-18 17:04:22 +01:00
#include <boost/exception/errinfo_api_function.hpp>
#include <boost/exception/errinfo_errno.hpp>
#include <boost/exception/errinfo_file_name.hpp>
#include <boost/exception/diagnostic_information.hpp>
#include <boost/exception_ptr.hpp>
2013-03-15 18:21:29 +01:00
2013-03-16 21:18:53 +01:00
#ifdef _WIN32
# include <boost/algorithm/string/trim.hpp>
#endif /* _WIN32 */
namespace icinga
{
2017-12-31 07:22:16 +01:00
class user_error : virtual public std::exception, virtual public boost::exception
{ };
/*
* @ingroup base
*/
2017-12-31 07:22:16 +01:00
class ScriptError : virtual public user_error
{
public:
ScriptError(String message);
ScriptError(String message, DebugInfo di, bool incompleteExpr = false);
~ScriptError() throw() = default;
const char *what(void) const throw() final;
DebugInfo GetDebugInfo() const;
bool IsIncompleteExpression() const;
bool IsHandledByDebugger() const;
void SetHandledByDebugger(bool handled);
private:
String m_Message;
DebugInfo m_DebugInfo;
bool m_IncompleteExpr;
bool m_HandledByDebugger;
};
/*
* @ingroup base
*/
2017-12-31 07:22:16 +01:00
class ValidationError : virtual public user_error
{
public:
ValidationError(const ConfigObject::Ptr& object, const std::vector<String>& attributePath, const String& message);
~ValidationError() throw() override;
const char *what() const throw() override;
ConfigObject::Ptr GetObject() const;
std::vector<String> GetAttributePath() const;
String GetMessage() const;
void SetDebugHint(const Dictionary::Ptr& dhint);
Dictionary::Ptr GetDebugHint() const;
private:
ConfigObject::Ptr m_Object;
std::vector<String> m_AttributePath;
String m_Message;
String m_What;
Dictionary::Ptr m_DebugHint;
};
StackTrace *GetLastExceptionStack();
2017-12-31 07:22:16 +01:00
void SetLastExceptionStack(const StackTrace& trace);
ContextTrace *GetLastExceptionContext();
2017-12-31 07:22:16 +01:00
void SetLastExceptionContext(const ContextTrace& context);
void RethrowUncaughtException();
typedef boost::error_info<StackTrace, StackTrace> StackTraceErrorInfo;
std::string to_string(const StackTraceErrorInfo&);
typedef boost::error_info<ContextTrace, ContextTrace> ContextTraceErrorInfo;
std::string to_string(const ContextTraceErrorInfo& e);
2017-12-31 07:22:16 +01:00
String DiagnosticInformation(const std::exception& ex, bool verbose = true, StackTrace *stack = nullptr, ContextTrace *context = nullptr);
String DiagnosticInformation(const boost::exception_ptr& eptr, bool verbose = true);
2017-12-31 07:22:16 +01:00
class posix_error : virtual public std::exception, virtual public boost::exception {
public:
~posix_error() throw() override;
const char *what(void) const throw() final;
private:
mutable char *m_Message{nullptr};
};
2012-05-18 22:21:28 +02:00
2012-04-22 16:45:31 +02:00
#ifdef _WIN32
2017-12-31 07:22:16 +01:00
class win32_error : virtual public std::exception, virtual public boost::exception { };
2013-03-11 14:03:01 +01:00
2013-03-18 17:04:22 +01:00
struct errinfo_win32_error_;
2013-03-11 13:45:08 +01:00
typedef boost::error_info<struct errinfo_win32_error_, int> errinfo_win32_error;
std::string to_string(const errinfo_win32_error& e);
2012-04-22 16:45:31 +02:00
#endif /* _WIN32 */
struct errinfo_getaddrinfo_error_;
typedef boost::error_info<struct errinfo_getaddrinfo_error_, int> errinfo_getaddrinfo_error;
std::string to_string(const errinfo_getaddrinfo_error& e);
struct errinfo_message_;
typedef boost::error_info<struct errinfo_message_, std::string> errinfo_message;
2012-04-16 16:27:41 +02:00
}
#endif /* EXCEPTION_H */