]> granicus.if.org Git - icinga2/blob - lib/base/exception.hpp
Merge pull request #7185 from Icinga/bugfix/gelfwriter-wrong-log-facility
[icinga2] / lib / base / exception.hpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #ifndef EXCEPTION_H
4 #define EXCEPTION_H
5
6 #include "base/i2-base.hpp"
7 #include "base/string.hpp"
8 #include "base/stacktrace.hpp"
9 #include "base/context.hpp"
10 #include "base/debuginfo.hpp"
11 #include "base/dictionary.hpp"
12 #include "base/configobject.hpp"
13 #include <boost/exception/errinfo_api_function.hpp>
14 #include <boost/exception/errinfo_errno.hpp>
15 #include <boost/exception/errinfo_file_name.hpp>
16 #include <boost/exception/diagnostic_information.hpp>
17 #include <boost/exception_ptr.hpp>
18
19 #ifdef _WIN32
20 #       include <boost/algorithm/string/trim.hpp>
21 #endif /* _WIN32 */
22
23 namespace icinga
24 {
25
26 class user_error : virtual public std::exception, virtual public boost::exception
27 { };
28
29 /*
30  * @ingroup base
31  */
32 class ScriptError : virtual public user_error
33 {
34 public:
35         ScriptError(String message);
36         ScriptError(String message, DebugInfo di, bool incompleteExpr = false);
37         ~ScriptError() throw() = default;
38
39         const char *what(void) const throw() final;
40
41         DebugInfo GetDebugInfo() const;
42         bool IsIncompleteExpression() const;
43
44         bool IsHandledByDebugger() const;
45         void SetHandledByDebugger(bool handled);
46
47 private:
48         String m_Message;
49         DebugInfo m_DebugInfo;
50         bool m_IncompleteExpr;
51         bool m_HandledByDebugger;
52 };
53
54 /*
55  * @ingroup base
56  */
57 class ValidationError : virtual public user_error
58 {
59 public:
60         ValidationError(const ConfigObject::Ptr& object, const std::vector<String>& attributePath, const String& message);
61         ~ValidationError() throw() override;
62
63         const char *what() const throw() override;
64
65         ConfigObject::Ptr GetObject() const;
66         std::vector<String> GetAttributePath() const;
67         String GetMessage() const;
68
69         void SetDebugHint(const Dictionary::Ptr& dhint);
70         Dictionary::Ptr GetDebugHint() const;
71
72 private:
73         ConfigObject::Ptr m_Object;
74         std::vector<String> m_AttributePath;
75         String m_Message;
76         String m_What;
77         Dictionary::Ptr m_DebugHint;
78 };
79
80 StackTrace *GetLastExceptionStack();
81 void SetLastExceptionStack(const StackTrace& trace);
82
83 ContextTrace *GetLastExceptionContext();
84 void SetLastExceptionContext(const ContextTrace& context);
85
86 void RethrowUncaughtException();
87
88 typedef boost::error_info<StackTrace, StackTrace> StackTraceErrorInfo;
89
90 std::string to_string(const StackTraceErrorInfo&);
91
92 typedef boost::error_info<ContextTrace, ContextTrace> ContextTraceErrorInfo;
93
94 std::string to_string(const ContextTraceErrorInfo& e);
95
96 String DiagnosticInformation(const std::exception& ex, bool verbose = true, StackTrace *stack = nullptr, ContextTrace *context = nullptr);
97 String DiagnosticInformation(const boost::exception_ptr& eptr, bool verbose = true);
98
99 class posix_error : virtual public std::exception, virtual public boost::exception {
100 public:
101         ~posix_error() throw() override;
102
103         const char *what(void) const throw() final;
104
105 private:
106         mutable char *m_Message{nullptr};
107 };
108
109 #ifdef _WIN32
110 class win32_error : virtual public std::exception, virtual public boost::exception { };
111
112 struct errinfo_win32_error_;
113 typedef boost::error_info<struct errinfo_win32_error_, int> errinfo_win32_error;
114
115 std::string to_string(const errinfo_win32_error& e);
116 #endif /* _WIN32 */
117
118 struct errinfo_getaddrinfo_error_;
119 typedef boost::error_info<struct errinfo_getaddrinfo_error_, int> errinfo_getaddrinfo_error;
120
121 std::string to_string(const errinfo_getaddrinfo_error& e);
122
123 struct errinfo_message_;
124 typedef boost::error_info<struct errinfo_message_, std::string> errinfo_message;
125
126 }
127
128 #endif /* EXCEPTION_H */