2012-05-10 12:06:41 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2014-03-19 01:02:29 +01:00
|
|
|
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
|
2012-05-10 12:06:41 +02:00
|
|
|
* *
|
|
|
|
* 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. *
|
2012-05-10 12:06:41 +02:00
|
|
|
******************************************************************************/
|
|
|
|
|
2012-04-03 13:01:00 +02:00
|
|
|
#ifndef EXCEPTION_H
|
|
|
|
#define EXCEPTION_H
|
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/i2-base.hpp"
|
|
|
|
#include "base/qstring.hpp"
|
|
|
|
#include "base/stacktrace.hpp"
|
|
|
|
#include "base/context.hpp"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <sstream>
|
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>
|
2013-11-20 21:55:14 +01:00
|
|
|
#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 */
|
|
|
|
|
2012-04-03 13:01:00 +02:00
|
|
|
namespace icinga
|
|
|
|
{
|
|
|
|
|
2014-10-01 17:01:47 +02:00
|
|
|
class I2_BASE_API user_error : virtual public std::exception, virtual public boost::exception
|
|
|
|
{ };
|
2014-03-22 08:38:46 +01:00
|
|
|
|
2013-11-20 21:55:14 +01:00
|
|
|
I2_BASE_API StackTrace *GetLastExceptionStack(void);
|
|
|
|
I2_BASE_API void SetLastExceptionStack(const StackTrace& trace);
|
|
|
|
|
2013-11-19 07:49:41 +01:00
|
|
|
I2_BASE_API ContextTrace *GetLastExceptionContext(void);
|
|
|
|
I2_BASE_API void SetLastExceptionContext(const ContextTrace& context);
|
|
|
|
|
2014-04-14 03:02:33 +02:00
|
|
|
I2_BASE_API void RethrowUncaughtException(void);
|
|
|
|
|
2013-11-20 21:55:14 +01:00
|
|
|
typedef boost::error_info<StackTrace, StackTrace> StackTraceErrorInfo;
|
2013-11-19 07:49:41 +01:00
|
|
|
typedef boost::error_info<ContextTrace, ContextTrace> ContextTraceErrorInfo;
|
2013-11-20 21:55:14 +01:00
|
|
|
|
|
|
|
template<typename T>
|
2013-11-19 07:49:41 +01:00
|
|
|
String DiagnosticInformation(const T& ex, StackTrace *stack = NULL, ContextTrace *context = NULL)
|
2012-04-03 13:01:00 +02:00
|
|
|
{
|
2013-11-20 21:55:14 +01:00
|
|
|
std::ostringstream result;
|
2013-03-07 19:44:39 +01:00
|
|
|
|
2013-11-20 21:55:14 +01:00
|
|
|
result << boost::diagnostic_information(ex);
|
2012-04-03 13:01:00 +02:00
|
|
|
|
2014-03-22 08:38:46 +01:00
|
|
|
if (dynamic_cast<const user_error *>(&ex) == NULL) {
|
|
|
|
if (boost::get_error_info<StackTraceErrorInfo>(ex) == NULL) {
|
|
|
|
result << std::endl;
|
2013-11-20 21:55:14 +01:00
|
|
|
|
2014-03-29 23:02:55 +01:00
|
|
|
if (!stack)
|
|
|
|
stack = GetLastExceptionStack();
|
|
|
|
|
2014-03-22 08:38:46 +01:00
|
|
|
if (stack)
|
|
|
|
result << *stack;
|
2014-03-29 23:02:55 +01:00
|
|
|
|
2014-03-22 08:38:46 +01:00
|
|
|
}
|
2013-11-20 21:55:14 +01:00
|
|
|
|
2014-03-22 08:38:46 +01:00
|
|
|
if (boost::get_error_info<ContextTraceErrorInfo>(ex) == NULL) {
|
|
|
|
result << std::endl;
|
2013-11-19 07:49:41 +01:00
|
|
|
|
2014-03-29 23:02:55 +01:00
|
|
|
if (!context)
|
|
|
|
context = GetLastExceptionContext();
|
|
|
|
|
2014-03-22 08:38:46 +01:00
|
|
|
if (context)
|
|
|
|
result << *context;
|
|
|
|
}
|
2013-11-19 07:49:41 +01:00
|
|
|
}
|
|
|
|
|
2013-11-20 21:55:14 +01:00
|
|
|
return result.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
I2_BASE_API String DiagnosticInformation(boost::exception_ptr eptr);
|
2013-03-07 19:44:39 +01:00
|
|
|
|
2013-03-11 13:45:08 +01:00
|
|
|
class I2_BASE_API posix_error : virtual public std::exception, virtual public boost::exception { };
|
2012-05-18 22:21:28 +02:00
|
|
|
|
2012-04-22 16:45:31 +02:00
|
|
|
#ifdef _WIN32
|
2013-03-11 14:03:01 +01:00
|
|
|
class I2_BASE_API win32_error : virtual public std::exception, virtual public boost::exception { };
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
inline std::string to_string(const errinfo_win32_error& e)
|
2012-04-22 16:45:31 +02:00
|
|
|
{
|
2013-03-16 21:18:53 +01:00
|
|
|
std::ostringstream tmp;
|
2013-03-11 13:45:08 +01:00
|
|
|
int code = e.value();
|
|
|
|
|
|
|
|
char *message;
|
|
|
|
String result = "Unknown error.";
|
|
|
|
|
|
|
|
DWORD rc = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM, NULL, code, 0, (char *)&message,
|
|
|
|
0, NULL);
|
|
|
|
|
|
|
|
if (rc != 0) {
|
|
|
|
result = String(message);
|
|
|
|
LocalFree(message);
|
|
|
|
|
|
|
|
/* remove trailing new-line characters */
|
|
|
|
boost::algorithm::trim_right(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp << code << ", \"" << result << "\"";
|
|
|
|
return tmp.str();
|
|
|
|
}
|
2012-04-22 16:45:31 +02:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
2013-09-12 07:50:09 +02:00
|
|
|
struct errinfo_getaddrinfo_error_;
|
|
|
|
typedef boost::error_info<struct errinfo_getaddrinfo_error_, int> errinfo_getaddrinfo_error;
|
|
|
|
|
|
|
|
inline std::string to_string(const errinfo_getaddrinfo_error& e)
|
|
|
|
{
|
|
|
|
return gai_strerror(e.value());
|
|
|
|
}
|
|
|
|
|
2013-11-05 12:40:25 +01:00
|
|
|
struct errinfo_message_;
|
|
|
|
typedef boost::error_info<struct errinfo_message_, std::string> errinfo_message;
|
|
|
|
|
2012-04-16 16:27:41 +02:00
|
|
|
}
|
|
|
|
|
2012-04-03 13:01:00 +02:00
|
|
|
#endif /* EXCEPTION_H */
|