2012-05-10 06:06:41 -04:00
|
|
|
/******************************************************************************
|
|
|
|
|
* Icinga 2 *
|
|
|
|
|
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
|
|
|
|
|
* *
|
|
|
|
|
* 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 07:33:57 -04:00
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
2012-05-10 06:06:41 -04:00
|
|
|
******************************************************************************/
|
|
|
|
|
|
2012-04-03 07:01:00 -04:00
|
|
|
#include "i2-base.h"
|
|
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
2012-05-26 14:00:03 -04:00
|
|
|
/**
|
|
|
|
|
* Retrieves the error code for the exception.
|
|
|
|
|
*
|
|
|
|
|
* @returns The error code.
|
|
|
|
|
*/
|
|
|
|
|
int Exception::GetCode(void) const
|
|
|
|
|
{
|
|
|
|
|
return m_Code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the error code for the exception.
|
|
|
|
|
*
|
|
|
|
|
* @param code The error code.
|
|
|
|
|
*/
|
|
|
|
|
void Exception::SetCode(int code)
|
|
|
|
|
{
|
|
|
|
|
m_Code = code;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-08 09:14:20 -04:00
|
|
|
/**
|
|
|
|
|
* Retrieves the description for the exception.
|
|
|
|
|
*
|
|
|
|
|
* @returns The description.
|
|
|
|
|
*/
|
2012-05-26 15:30:04 -04:00
|
|
|
string Exception::GetMessage(void) const
|
2012-04-03 07:01:00 -04:00
|
|
|
{
|
2012-04-22 10:45:31 -04:00
|
|
|
return m_Message;
|
2012-04-03 07:01:00 -04:00
|
|
|
}
|
|
|
|
|
|
2012-05-17 13:14:03 -04:00
|
|
|
/**
|
|
|
|
|
* Retrieves the description for the exception.
|
|
|
|
|
*
|
|
|
|
|
* @returns The description.
|
|
|
|
|
*/
|
|
|
|
|
const char *Exception::what(void) const throw()
|
|
|
|
|
{
|
2012-06-12 05:56:12 -04:00
|
|
|
return m_Message.c_str();
|
2012-05-17 13:14:03 -04:00
|
|
|
}
|
|
|
|
|
|
2012-05-08 09:14:20 -04:00
|
|
|
/**
|
|
|
|
|
* Sets the description for the exception.
|
|
|
|
|
*
|
|
|
|
|
* @param message The description.
|
|
|
|
|
*/
|
2012-05-26 15:30:04 -04:00
|
|
|
void Exception::SetMessage(string message)
|
2012-04-03 07:01:00 -04:00
|
|
|
{
|
2012-05-26 15:30:04 -04:00
|
|
|
m_Message = message;
|
2012-04-22 10:45:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2012-05-08 09:14:20 -04:00
|
|
|
/**
|
|
|
|
|
* Formats an Win32 error code.
|
|
|
|
|
*
|
|
|
|
|
* @param code The error code.
|
|
|
|
|
* @returns A string describing the error.
|
|
|
|
|
*/
|
2012-04-22 10:45:31 -04:00
|
|
|
string Win32Exception::FormatErrorCode(int code)
|
|
|
|
|
{
|
|
|
|
|
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);
|
2012-06-15 13:32:41 -04:00
|
|
|
|
|
|
|
|
/* remove trailing new-line characters */
|
|
|
|
|
boost::algorithm::trim_right(result);
|
2012-04-22 10:45:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
2012-04-03 07:01:00 -04:00
|
|
|
}
|
2012-04-22 10:45:31 -04:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
|
2012-05-08 09:14:20 -04:00
|
|
|
/**
|
|
|
|
|
* Formats a Posix error code.
|
|
|
|
|
*
|
|
|
|
|
* @param code The error code.
|
|
|
|
|
* @returns A string describing the error.
|
|
|
|
|
*/
|
2012-04-22 10:45:31 -04:00
|
|
|
string PosixException::FormatErrorCode(int code)
|
|
|
|
|
{
|
|
|
|
|
return strerror(code);
|
2012-04-23 03:48:20 -04:00
|
|
|
}
|
2012-04-24 08:54:05 -04:00
|
|
|
|
2012-05-08 09:14:20 -04:00
|
|
|
/**
|
|
|
|
|
* Formats an OpenSSL error code.
|
|
|
|
|
*
|
|
|
|
|
* @param code The error code.
|
|
|
|
|
* @returns A string describing the error.
|
|
|
|
|
*/
|
2012-04-24 08:54:05 -04:00
|
|
|
string OpenSSLException::FormatErrorCode(int code)
|
|
|
|
|
{
|
2012-04-26 06:55:48 -04:00
|
|
|
const char *message = ERR_error_string(code, NULL);
|
2012-04-24 08:54:05 -04:00
|
|
|
|
|
|
|
|
if (message == NULL)
|
|
|
|
|
message = "Unknown error.";
|
|
|
|
|
|
|
|
|
|
return message;
|
|
|
|
|
}
|