]> granicus.if.org Git - icinga2/blob - lib/base/exception.hpp
d6a9b8470634897ff5c55f78b1b820285d7b0884
[icinga2] / lib / base / exception.hpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org)    *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
20 #ifndef EXCEPTION_H
21 #define EXCEPTION_H
22
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"
31 #include <sstream>
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>
37
38 #ifdef _WIN32
39 #       include <boost/algorithm/string/trim.hpp>
40 #endif /* _WIN32 */
41
42 namespace icinga
43 {
44
45 class I2_BASE_API user_error : virtual public std::exception, virtual public boost::exception
46 { };
47
48 /*
49  * @ingroup base
50  */
51 class I2_BASE_API ScriptError : virtual public user_error
52 {
53 public:
54         ScriptError(const String& message);
55         ScriptError(const String& message, const DebugInfo& di, bool incompleteExpr = false);
56         ~ScriptError(void) throw();
57
58         virtual const char *what(void) const throw();
59
60         DebugInfo GetDebugInfo(void) const;
61         bool IsIncompleteExpression(void) const;
62
63 private:
64         String m_Message;
65         DebugInfo m_DebugInfo;
66         bool m_IncompleteExpr;
67 };
68
69 /*
70  * @ingroup base
71  */
72 class I2_BASE_API ValidationError : virtual public user_error
73 {
74 public:
75         ValidationError(const ConfigObject::Ptr& object, const std::vector<String>& attributePath, const String& message);
76         ~ValidationError(void) throw();
77
78         virtual const char *what(void) const throw();
79
80         ConfigObject::Ptr GetObject(void) const;
81         std::vector<String> GetAttributePath(void) const;
82         String GetMessage(void) const;
83
84         void SetDebugHint(const Dictionary::Ptr& dhint);
85         Dictionary::Ptr GetDebugHint(void) const;
86
87 private:
88         ConfigObject::Ptr m_Object;
89         std::vector<String> m_AttributePath;
90         String m_Message;
91         String m_What;
92         Dictionary::Ptr m_DebugHint;
93 };
94
95 I2_BASE_API StackTrace *GetLastExceptionStack(void);
96 I2_BASE_API void SetLastExceptionStack(const StackTrace& trace);
97
98 I2_BASE_API ContextTrace *GetLastExceptionContext(void);
99 I2_BASE_API void SetLastExceptionContext(const ContextTrace& context);
100
101 I2_BASE_API void RethrowUncaughtException(void);
102
103 typedef boost::error_info<StackTrace, StackTrace> StackTraceErrorInfo;
104
105 inline std::string to_string(const StackTraceErrorInfo& e)
106 {
107         return "";
108 }
109
110 typedef boost::error_info<ContextTrace, ContextTrace> ContextTraceErrorInfo;
111
112 inline std::string to_string(const ContextTraceErrorInfo& e)
113 {
114         std::ostringstream msgbuf;
115         msgbuf << "[Context] = " << e.value();
116         return msgbuf.str();
117 }
118
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);
121
122 class I2_BASE_API posix_error : virtual public std::exception, virtual public boost::exception {
123 public:
124         posix_error(void);
125         virtual ~posix_error(void) throw();
126
127         virtual const char *what(void) const throw();
128
129 private:
130         mutable char *m_Message;
131 };
132
133 #ifdef _WIN32
134 class I2_BASE_API win32_error : virtual public std::exception, virtual public boost::exception { };
135
136 struct errinfo_win32_error_;
137 typedef boost::error_info<struct errinfo_win32_error_, int> errinfo_win32_error;
138
139 inline std::string to_string(const errinfo_win32_error& e)
140 {
141         return "[errinfo_win32_error] = " + Utility::FormatErrorNumber(e.value()) + "\n";
142 }
143 #endif /* _WIN32 */
144
145 struct errinfo_getaddrinfo_error_;
146 typedef boost::error_info<struct errinfo_getaddrinfo_error_, int> errinfo_getaddrinfo_error;
147
148 inline std::string to_string(const errinfo_getaddrinfo_error& e)
149 {
150         return "[errinfo_getaddrinfo_error] = " + String(gai_strerror(e.value())) + "\n";
151 }
152
153 struct errinfo_message_;
154 typedef boost::error_info<struct errinfo_message_, std::string> errinfo_message;
155
156 }
157
158 #endif /* EXCEPTION_H */