1 /******************************************************************************
3 * Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
23 #include "base/i2-base.hpp"
24 #include "base/string.hpp"
25 #include "base/stacktrace.hpp"
26 #include "base/context.hpp"
27 #include "base/utility.hpp"
28 #include "base/debuginfo.hpp"
29 #include "base/dictionary.hpp"
30 #include "base/configobject.hpp"
32 #include <boost/exception/errinfo_api_function.hpp>
33 #include <boost/exception/errinfo_errno.hpp>
34 #include <boost/exception/errinfo_file_name.hpp>
35 #include <boost/exception/diagnostic_information.hpp>
36 #include <boost/exception_ptr.hpp>
39 # include <boost/algorithm/string/trim.hpp>
45 class I2_BASE_API user_error : virtual public std::exception, virtual public boost::exception
51 class I2_BASE_API ScriptError : virtual public user_error
54 ScriptError(const String& message);
55 ScriptError(const String& message, const DebugInfo& di, bool incompleteExpr = false);
56 ~ScriptError(void) throw();
58 virtual const char *what(void) const throw();
60 DebugInfo GetDebugInfo(void) const;
61 bool IsIncompleteExpression(void) const;
65 DebugInfo m_DebugInfo;
66 bool m_IncompleteExpr;
72 class I2_BASE_API ValidationError : virtual public user_error
75 ValidationError(const ConfigObject::Ptr& object, const std::vector<String>& attributePath, const String& message);
76 ~ValidationError(void) throw();
78 virtual const char *what(void) const throw();
80 ConfigObject::Ptr GetObject(void) const;
81 std::vector<String> GetAttributePath(void) const;
82 String GetMessage(void) const;
84 void SetDebugHint(const Dictionary::Ptr& dhint);
85 Dictionary::Ptr GetDebugHint(void) const;
88 ConfigObject::Ptr m_Object;
89 std::vector<String> m_AttributePath;
92 Dictionary::Ptr m_DebugHint;
95 I2_BASE_API StackTrace *GetLastExceptionStack(void);
96 I2_BASE_API void SetLastExceptionStack(const StackTrace& trace);
98 I2_BASE_API ContextTrace *GetLastExceptionContext(void);
99 I2_BASE_API void SetLastExceptionContext(const ContextTrace& context);
101 I2_BASE_API void RethrowUncaughtException(void);
103 typedef boost::error_info<StackTrace, StackTrace> StackTraceErrorInfo;
105 inline std::string to_string(const StackTraceErrorInfo& e)
110 typedef boost::error_info<ContextTrace, ContextTrace> ContextTraceErrorInfo;
112 inline std::string to_string(const ContextTraceErrorInfo& e)
114 std::ostringstream msgbuf;
115 msgbuf << "[Context] = " << e.value();
119 I2_BASE_API String DiagnosticInformation(const std::exception& ex, bool verbose = true, StackTrace *stack = NULL, ContextTrace *context = NULL);
120 I2_BASE_API String DiagnosticInformation(boost::exception_ptr eptr, bool verbose = true);
122 class I2_BASE_API posix_error : virtual public std::exception, virtual public boost::exception {
125 virtual ~posix_error(void) throw();
127 virtual const char *what(void) const throw();
130 mutable char *m_Message;
134 class I2_BASE_API win32_error : virtual public std::exception, virtual public boost::exception { };
136 struct errinfo_win32_error_;
137 typedef boost::error_info<struct errinfo_win32_error_, int> errinfo_win32_error;
139 inline std::string to_string(const errinfo_win32_error& e)
141 return "[errinfo_win32_error] = " + Utility::FormatErrorNumber(e.value()) + "\n";
145 struct errinfo_getaddrinfo_error_;
146 typedef boost::error_info<struct errinfo_getaddrinfo_error_, int> errinfo_getaddrinfo_error;
148 inline std::string to_string(const errinfo_getaddrinfo_error& e)
150 return "[errinfo_getaddrinfo_error] = " + String(gai_strerror(e.value())) + "\n";
153 struct errinfo_message_;
154 typedef boost::error_info<struct errinfo_message_, std::string> errinfo_message;
158 #endif /* EXCEPTION_H */