#ifdef _WIN32
WSADATA wsaData;
if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
- throw Win32Exception("WSAStartup failed", WSAGetLastError());
+ throw_exception(Win32Exception("WSAStartup failed", WSAGetLastError()));
#else /* _WIN32 */
lt_dlinit();
#endif /* _WIN32 */
HMODULE hModule = LoadLibrary(path.c_str());
if (hModule == NULL)
- throw Win32Exception("LoadLibrary('" + path + "') failed", GetLastError());
+ throw_exception(Win32Exception("LoadLibrary('" + path + "') failed", GetLastError()));
#else /* _WIN32 */
lt_dlhandle hModule = lt_dlopen(path.c_str());
if (hModule == NULL) {
- throw runtime_error("Could not load module '" + path + "': " + lt_dlerror());
+ throw_exception(runtime_error("Could not load module '" + path + "': " + lt_dlerror()));
}
#endif /* _WIN32 */
#endif /* _WIN32 */
if (pCreateComponent == NULL)
- throw runtime_error("Loadable module does not contain "
- "CreateComponent function");
+ throw_exception(runtime_error("Loadable module does not contain "
+ "CreateComponent function"));
component = Component::Ptr(pCreateComponent());
component->SetConfig(componentConfig);
char buffer[MAXPATHLEN];
if (getcwd(buffer, sizeof(buffer)) == NULL)
- throw PosixException("getcwd failed", errno);
+ throw_exception(PosixException("getcwd failed", errno));
string workingDirectory = buffer;
if (argv0[0] != '/')
if (!foundPath) {
executablePath.clear();
- throw runtime_error("Could not determine executable path.");
+ throw_exception(runtime_error("Could not determine executable path."));
}
}
}
if (realpath(executablePath.c_str(), buffer) == NULL)
- throw PosixException("realpath failed", errno);
+ throw_exception(PosixException("realpath failed", errno));
result = buffer;
#else /* _WIN32 */
char FullExePath[MAXPATHLEN];
if (!GetModuleFileName(NULL, FullExePath, sizeof(FullExePath)))
- throw Win32Exception("GetModuleFileName() failed", GetLastError());
+ throw_exception(Win32Exception("GetModuleFileName() failed", GetLastError()));
result = FullExePath;
#endif /* _WIN32 */
m_PidFile = fopen(filename.c_str(), "w");
if (m_PidFile == NULL)
- throw runtime_error("Could not open PID file '" + filename + "'");
+ throw_exception(runtime_error("Could not open PID file '" + filename + "'"));
#ifndef _WIN32
if (flock(fileno(m_PidFile), LOCK_EX | LOCK_NB) < 0) {
ClosePidFile();
- throw runtime_error("Another instance of the application is "
+ throw_exception(runtime_error("Another instance of the application is "
"already running. Remove the '" + filename + "' file if "
- "you're certain that this is not the case.");
+ "you're certain that this is not the case."));
}
#endif /* _WIN32 */
TResult GetResult(void)
{
if (!m_Finished)
- throw runtime_error("GetResult called on an unfinished AsyncTask");
+ throw_exception(runtime_error("GetResult called on an unfinished AsyncTask"));
if (m_ResultRetrieved)
- throw runtime_error("GetResult called on an AsyncTask whose result was already retrieved.");
+ throw_exception(runtime_error("GetResult called on an AsyncTask whose result was already retrieved."));
if (m_Exception)
- boost::rethrow_exception(m_Exception);
+ rethrow_exception(m_Exception);
m_ResultRetrieved = true;
ScriptFunction::Ptr func = ScriptFunction::GetByName(funcName);
if (!func)
- throw invalid_argument("Function '" + funcName + "' does not exist.");
+ throw_exception(invalid_argument("Function '" + funcName + "' does not exist."));
ScriptTask::Ptr task = boost::make_shared<ScriptTask>(func, arguments);
task->Start(callback);
char *newBuffer = (char *)realloc(m_Buffer, newSize);
if (newBuffer == NULL)
- throw bad_alloc();
+ throw_exception(bad_alloc());
m_Buffer = newBuffer;
using std::ofstream;
using std::exception;
+using std::bad_alloc;
using std::bad_cast;
using std::runtime_error;
using std::logic_error;
using boost::condition_variable;
using boost::system_time;
using boost::tie;
+using boost::throw_exception;
+using boost::rethrow_exception;
+using boost::current_exception;
namespace tuples = boost::tuples;
case LogCritical:
return "critical";
default:
- throw invalid_argument("Invalid severity.");
+ throw_exception(invalid_argument("Invalid severity."));
}
}
else if (severity == "critical")
return LogCritical;
else
- throw invalid_argument("Invalid severity: " + severity);
+ throw_exception(invalid_argument("Invalid severity: " + severity));
}
#endif /* _MSC_VER */
if (m_FP == NULL)
- throw runtime_error("Could not create process.");
+ throw_exception(runtime_error("Could not create process."));
}
bool Process::RunTask(void)
int flags;
flags = fcntl(fd, F_GETFL, 0);
if (flags < 0)
- throw PosixException("fcntl failed", errno);
+ throw_exception(PosixException("fcntl failed", errno));
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0)
- throw PosixException("fcntl failed", errno);
+ throw_exception(PosixException("fcntl failed", errno));
#else /* F_GETFL */
unsigned long lTrue = 1;
ioctlsocket(fd, FIONBIO, &lTrue);
if (m_FD == INVALID_SOCKET)
return;
+ SetConnected(false);
+
closesocket(m_FD);
m_FD = INVALID_SOCKET;
#endif /* _WIN32 */
}
-/**
- * Handles a socket error by calling the OnError event or throwing an exception
- * when there are no observers for the OnError event.
- *
- * @param ex An exception.
- */
-void Socket::HandleSocketError(const exception& ex)
-{
- if (!OnError.empty()) {
- Event::Post(boost::bind(boost::ref(OnError), GetSelf(), runtime_error(ex.what())));
-
- CloseInternal(false);
- } else {
- throw ex;
- }
-}
-
/**
* Processes errors that have occured for the socket.
*/
void Socket::HandleException(void)
{
- HandleSocketError(SocketException(
- "select() returned fd in except fdset", GetError()));
+ throw_exception(SocketException("select() returned fd in except fdset", GetError()));
}
/**
char host[NI_MAXHOST];
char service[NI_MAXSERV];
- if (getnameinfo(address, len, host, sizeof(host), service, sizeof(service), NI_NUMERICHOST | NI_NUMERICSERV) < 0)
- throw SocketException("getnameinfo() failed",
- GetLastSocketError());
+ if (getnameinfo(address, len, host, sizeof(host), service,
+ sizeof(service), NI_NUMERICHOST | NI_NUMERICSERV) < 0)
+ throw_exception(SocketException("getnameinfo() failed",
+ GetLastSocketError()));
stringstream s;
s << "[" << host << "]:" << service;
sockaddr_storage sin;
socklen_t len = sizeof(sin);
- if (getsockname(GetFD(), (sockaddr *)&sin, &len) < 0) {
- HandleSocketError(SocketException(
- "getsockname() failed", GetError()));
-
- return string();
- }
+ if (getsockname(GetFD(), (sockaddr *)&sin, &len) < 0)
+ throw_exception(SocketException("getsockname() failed", GetError()));
return GetAddressFromSockaddr((sockaddr *)&sin, len);
}
sockaddr_storage sin;
socklen_t len = sizeof(sin);
- if (getpeername(GetFD(), (sockaddr *)&sin, &len) < 0) {
- HandleSocketError(SocketException(
- "getpeername() failed", GetError()));
-
- return string();
- }
+ if (getpeername(GetFD(), (sockaddr *)&sin, &len) < 0)
+ throw_exception(SocketException("getpeername() failed", GetError()));
return GetAddressFromSockaddr((sockaddr *)&sin, len);
}
if (GetFD() == INVALID_SOCKET)
return;
- if (rc < 0) {
- HandleSocketError(SocketException("select() failed", GetError()));
- return;
- }
+ try {
+ if (rc < 0)
+ throw_exception(SocketException("select() failed", GetError()));
- if (FD_ISSET(fd, &readfds))
- HandleReadable();
+ if (FD_ISSET(fd, &readfds))
+ HandleReadable();
- if (FD_ISSET(fd, &exceptfds))
- HandleException();
+ if (FD_ISSET(fd, &exceptfds))
+ HandleException();
+ } catch (const exception&) {
+ m_Exception = boost::current_exception();
+
+ CloseInternal(false);
+
+ break;
+ }
if (WantsToWrite())
m_WriteCV.notify_all(); /* notify Write thread */
if (GetFD() == INVALID_SOCKET)
return;
- if (rc < 0) {
- HandleSocketError(SocketException("select() failed", GetError()));
- return;
- }
+ try {
+ if (rc < 0)
+ throw_exception(SocketException("select() failed", GetError()));
- if (FD_ISSET(fd, &writefds))
- HandleWritable();
+ if (FD_ISSET(fd, &writefds))
+ HandleWritable();
+ } catch (const exception&) {
+ m_Exception = boost::current_exception();
+
+ CloseInternal(false);
+
+ break;
+ }
}
}
{
return m_Connected;
}
+
+void Socket::CheckException(void)
+{
+ if (m_Exception)
+ rethrow_exception(m_Exception);
+}
~Socket(void);
- boost::signal<void (const Socket::Ptr&, const exception&)> OnError;
boost::signal<void (const Socket::Ptr&)> OnClosed;
virtual void Start(void);
mutex& GetMutex(void) const;
+ bool IsConnected(void) const;
+
+ void CheckException(void);
+
protected:
Socket(void);
SOCKET GetFD(void) const;
void SetConnected(bool connected);
- bool IsConnected(void) const;
int GetError(void) const;
static int GetLastSocketError(void);
- void HandleSocketError(const exception& ex);
virtual bool WantsToRead(void) const;
virtual bool WantsToWrite(void) const;
virtual void CloseInternal(bool from_dtor);
- mutable mutex m_Mutex;
-
private:
SOCKET m_FD; /**< The socket descriptor. */
bool m_Connected;
condition_variable m_WriteCV;
+ mutable mutex m_Mutex;
+ boost::exception_ptr m_Exception;
+
void ReadThreadProc(void);
void WriteThreadProc(void);
stream->open(filename.c_str(), ofstream::out | ofstream::trunc);
if (!stream->good())
- throw runtime_error("Could not open logfile '" + filename + "'");
+ throw_exception(runtime_error("Could not open logfile '" + filename + "'"));
} catch (const exception&) {
delete stream;
throw;
int rc = getaddrinfo(node.c_str(), service.c_str(), &hints, &result);
- if (rc < 0) {
- HandleSocketError(SocketException(
- "getaddrinfo() failed", GetLastSocketError()));
- return;
- }
+ if (rc < 0)
+ throw_exception(SocketException("getaddrinfo() failed", GetLastSocketError()));
int fd = INVALID_SOCKET;
freeaddrinfo(result);
if (fd == INVALID_SOCKET)
- HandleSocketError(runtime_error(
- "Could not create a suitable socket."));
+ throw_exception(runtime_error("Could not create a suitable socket."));
}
void TcpClient::HandleWritable(void)
if (rc <= 0) {
SetConnected(false);
- HandleSocketError(SocketException("send() failed", GetError()));
- return;
+ throw_exception(SocketException("send() failed", GetError()));
}
SetConnected(true);
if (rc <= 0) {
SetConnected(false);
- HandleSocketError(SocketException("recv() failed", GetError()));
- return;
+ throw_exception(SocketException("recv() failed", GetError()));
}
SetConnected(true);
*/
void TcpServer::Listen(void)
{
- if (listen(GetFD(), SOMAXCONN) < 0) {
- HandleSocketError(SocketException(
- "listen() failed", GetError()));
- return;
- }
+ if (listen(GetFD(), SOMAXCONN) < 0)
+ throw_exception(SocketException("listen() failed", GetError()));
}
/**
fd = accept(GetFD(), (sockaddr *)&addr, &addrlen);
- if (fd < 0) {
- HandleSocketError(SocketException(
- "accept() failed", GetError()));
- return;
- }
+ if (fd < 0)
+ throw_exception(SocketException("accept() failed", GetError()));
TcpClient::Ptr client = m_ClientFactory(fd);
int fd = socket(family, SOCK_STREAM, 0);
- if (fd == INVALID_SOCKET) {
- HandleSocketError(SocketException(
- "socket() failed", GetLastSocketError()));
-
- return;
- }
+ if (fd == INVALID_SOCKET)
+ throw_exception(SocketException("socket() failed", GetLastSocketError()));
SetFD(fd);
}
hints.ai_flags = AI_PASSIVE;
if (getaddrinfo(node.empty() ? NULL : node.c_str(),
- service.c_str(), &hints, &result) < 0) {
- HandleSocketError(SocketException(
- "getaddrinfo() failed", GetLastSocketError()));
-
- return;
- }
+ service.c_str(), &hints, &result) < 0)
+ throw_exception(SocketException("getaddrinfo() failed", GetLastSocketError()));
int fd = INVALID_SOCKET;
freeaddrinfo(result);
if (fd == INVALID_SOCKET)
- HandleSocketError(runtime_error(
- "Could not create a suitable socket."));
+ throw_exception(runtime_error("Could not create a suitable socket."));
}
*
* @param interval The new interval.
*/
-void Timer::SetInterval(long interval)
+void Timer::SetInterval(unsigned long interval)
{
- if (interval <= 0)
- throw invalid_argument("interval");
-
m_Interval = interval;
}
*
* @returns The interval.
*/
-long Timer::GetInterval(void) const
+unsigned long Timer::GetInterval(void) const
{
return m_Interval;
}
Timer(void);
- void SetInterval(long interval);
- long GetInterval(void) const;
+ void SetInterval(unsigned long interval);
+ unsigned long GetInterval(void) const;
static long ProcessTimers(void);
boost::signal<void(const Timer::Ptr&)> OnTimerExpired;
private:
- long m_Interval; /**< The interval of the timer. */
+ unsigned long m_Interval; /**< The interval of the timer. */
time_t m_Next; /**< When the next event should happen. */
static Timer::CollectionType m_Timers;
m_SSL = shared_ptr<SSL>(SSL_new(m_SSLContext.get()), SSL_free);
if (!m_SSL)
- throw OpenSSLException("SSL_new failed", ERR_get_error());
+ throw_exception(OpenSSLException("SSL_new failed", ERR_get_error()));
if (!GetClientCertificate())
- throw logic_error("No X509 client certificate was specified.");
+ throw_exception(logic_error("No X509 client certificate was specified."));
if (!m_SSLIndexInitialized) {
m_SSLIndex = SSL_get_ex_new_index(0, (void *)"TlsClient", NULL, NULL, NULL);
CloseInternal(false);
goto post_event;
default:
- HandleSocketError(OpenSSLException(
- "SSL_read failed", ERR_get_error()));
- goto post_event;
+ throw_exception(OpenSSLException("SSL_read failed", ERR_get_error()));
}
}
CloseInternal(false);
return;
default:
- HandleSocketError(OpenSSLException(
- "SSL_write failed", ERR_get_error()));
- return;
+ throw_exception(OpenSSLException("SSL_write failed", ERR_get_error()));
}
}
pid = fork();
if (pid < 0)
- throw PosixException("fork() failed", errno);
+ throw_exception PosixException("fork() failed", errno);
if (pid)
exit(0);
fd = open("/dev/null", O_RDWR);
if (fd < 0)
- throw PosixException("open() failed", errno);
+ throw_exception PosixException("open() failed", errno);
if (fd != STDIN_FILENO)
dup2(fd, STDIN_FILENO);
close(fd);
if (setsid() < 0)
- throw PosixException("setsid() failed", errno);
+ throw_exception PosixException("setsid() failed", errno);
#endif
}
SSL_CTX_set_mode(sslContext.get(), SSL_MODE_ENABLE_PARTIAL_WRITE | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
if (!SSL_CTX_use_certificate_chain_file(sslContext.get(), pubkey.c_str()))
- throw OpenSSLException("Could not load public X509 key file", ERR_get_error());
+ throw_exception(OpenSSLException("Could not load public X509 key file", ERR_get_error()));
if (!SSL_CTX_use_PrivateKey_file(sslContext.get(), privkey.c_str(), SSL_FILETYPE_PEM))
- throw OpenSSLException("Could not load private X509 key file", ERR_get_error());
+ throw_exception(OpenSSLException("Could not load private X509 key file", ERR_get_error()));
if (!SSL_CTX_load_verify_locations(sslContext.get(), cakey.c_str(), NULL))
- throw OpenSSLException("Could not load public CA key file", ERR_get_error());
+ throw_exception(OpenSSLException("Could not load public CA key file", ERR_get_error()));
STACK_OF(X509_NAME) *cert_names;
cert_names = SSL_load_client_CA_file(cakey.c_str());
if (cert_names == NULL)
- throw OpenSSLException("SSL_load_client_CA_file() failed", ERR_get_error());
+ throw_exception(OpenSSLException("SSL_load_client_CA_file() failed", ERR_get_error()));
SSL_CTX_set_client_CA_list(sslContext.get(), cert_names);
int rc = X509_NAME_get_text_by_NID(X509_get_subject_name(certificate.get()), NID_commonName, buffer, sizeof(buffer));
if (rc == -1)
- throw OpenSSLException("X509 certificate has no CN attribute", ERR_get_error());
+ throw_exception(OpenSSLException("X509 certificate has no CN attribute", ERR_get_error()));
return buffer;
}
BIO *fpcert = BIO_new(BIO_s_file());
if (fpcert == NULL)
- throw OpenSSLException("BIO_new failed", ERR_get_error());
+ throw_exception(OpenSSLException("BIO_new failed", ERR_get_error()));
if (BIO_read_filename(fpcert, pemfile.c_str()) < 0)
- throw OpenSSLException("BIO_read_filename failed", ERR_get_error());
+ throw_exception(OpenSSLException("BIO_read_filename failed", ERR_get_error()));
cert = PEM_read_bio_X509_AUX(fpcert, NULL, NULL, NULL);
if (cert == NULL)
- throw OpenSSLException("PEM_read_bio_X509_AUX failed", ERR_get_error());
+ throw_exception(OpenSSLException("PEM_read_bio_X509_AUX failed", ERR_get_error()));
BIO_free(fpcert);
string result;
if (dir == NULL)
- throw std::bad_alloc();
+ throw_exception(bad_alloc());
#ifndef _WIN32
result = dirname(dir);
#else /* _WIN32 */
if (!PathRemoveFileSpec(dir)) {
free(dir);
- throw Win32Exception("PathRemoveFileSpec() failed", GetLastError());
+ throw_exception(Win32Exception("PathRemoveFileSpec() failed", GetLastError()));
}
result = dir;
string result;
if (dir == NULL)
- throw std::bad_alloc();
+ throw_exception(bad_alloc());
#ifndef _WIN32
result = basename(dir);
Object::Ptr object = dynamic_pointer_cast<Object>(value);
if (!object)
- throw invalid_argument("shared_ptr value type must inherit from Object class.");
+ throw_exception(invalid_argument("shared_ptr value type must inherit from Object class."));
m_Value = object;
}
shared_ptr<T> object = dynamic_pointer_cast<T>(boost::get<Object::Ptr>(m_Value));
if (!object)
- throw bad_cast();
+ throw_exception(bad_cast());
return object;
}
ConfigObject::Ptr configObject = ConfigObject::GetObject("host", name);
if (!configObject)
- throw invalid_argument("Host '" + name + "' does not exist.");
+ throw_exception(invalid_argument("Host '" + name + "' does not exist."));
return Host(configObject);
}
ConfigObject::Ptr configObject = ConfigObject::GetObject("hostgroup", name);
if (!configObject)
- throw invalid_argument("HostGroup '" + name + "' does not exist.");
+ throw_exception(invalid_argument("HostGroup '" + name + "' does not exist."));
return HostGroup(configObject);
}
pos_second = result.find_first_of('$', pos_first + 1);
if (pos_second == string::npos)
- throw runtime_error("Closing $ not found in macro format string.");
+ throw_exception(runtime_error("Closing $ not found in macro format string."));
string name = result.substr(pos_first + 1, pos_second - pos_first - 1);
string value;
}
if (!resolved)
- throw runtime_error("Macro '" + name + "' is not defined.");
+ throw_exception(runtime_error("Macro '" + name + "' is not defined."));
result.replace(pos_first, pos_second - pos_first + 1, value);
void NagiosCheckTask::ScriptFunc(const ScriptTask::Ptr& task, const vector<Variant>& arguments)
{
if (arguments.size() < 1)
- throw invalid_argument("Missing argument: Service must be specified.");
+ throw_exception(invalid_argument("Missing argument: Service must be specified."));
Variant vservice = arguments[0];
if (!vservice.IsObjectType<ConfigObject>())
- throw invalid_argument("Argument must be a config object.");
+ throw_exception(invalid_argument("Argument must be a config object."));
Service service = static_cast<ConfigObject::Ptr>(vservice);
void NullCheckTask::ScriptFunc(const ScriptTask::Ptr& task, const vector<Variant>& arguments)
{
if (arguments.size() < 1)
- throw invalid_argument("Missing argument: Service must be specified.");
+ throw_exception(invalid_argument("Missing argument: Service must be specified."));
time_t now;
time(&now);
ConfigObject::Ptr configObject = ConfigObject::GetObject("service", name);
if (!configObject)
- throw invalid_argument("Service '" + name + "' does not exist.");
+ throw_exception(invalid_argument("Service '" + name + "' does not exist."));
return configObject;
}
{
string hostname;
if (!GetProperty("host_name", &hostname))
- throw runtime_error("Service object is missing the 'host_name' property.");
+ throw_exception(runtime_error("Service object is missing the 'host_name' property."));
return Host::GetByName(hostname);
}
{
Dictionary::Ptr value;
if (!GetTag("last_result", &value))
- throw invalid_argument("Service has no last check result.");
+ throw_exception(invalid_argument("Service has no last check result."));
return CheckResult(value);
}
ConfigObject::Ptr configObject = ConfigObject::GetObject("hostgroup", name);
if (!configObject)
- throw invalid_argument("ServiceGroup '" + name + "' does not exist.");
+ throw_exception(invalid_argument("ServiceGroup '" + name + "' does not exist."));
return ServiceGroup(configObject);
}
}
if (object->IsLocal())
- throw invalid_argument("Replicated remote object is marked as local.");
+ throw_exception(invalid_argument("Replicated remote object is marked as local."));
if (object->GetSource().empty())
object->SetSource(sender->GetIdentity());
string filename;
if (!GetConfig()->GetProperty("configFilename", &filename))
- throw logic_error("Missing 'configFilename' property");
+ throw_exception(logic_error("Missing 'configFilename' property"));
vector<ConfigItem::Ptr> configItems = ConfigCompiler::CompileFile(filename);
CopyServiceAttributes(host, service, builder);
} else {
- throw invalid_argument("Service description must be either a string or a dictionary.");
+ throw_exception(invalid_argument("Service description must be either a string or a dictionary."));
}
ConfigItem::Ptr serviceItem = builder->Compile();
if ( ! b )
YY_FATAL_ERROR( "bad buffer in yy_scan_bytes()" );
- /* It's okay to grow etc. this buffer, and we should throw it
+ /* It's okay to grow etc. this buffer, and we should throw_exception it
* away when we're done.
*/
b->yy_is_our_buffer = 1;
{
stringstream message;
message << *locp << ": " << err;
- throw runtime_error(message.str());
+ throw_exception(runtime_error(message.str()));
}
int yyparse(ConfigCompiler *context);
{
stringstream message;
message << *locp << ": " << err;
- throw runtime_error(message.str());
+ throw_exception(runtime_error(message.str()));
}
int yyparse(ConfigCompiler *context);
vector<ConfigItem::Ptr> ConfigCompiler::CompileFile(const string& path)
{
ifstream stream;
- stream.exceptions(ifstream::badbit);
stream.open(path.c_str(), ifstream::in);
if (!stream.good())
- throw invalid_argument("Could not open config file: " + path);
+ throw_exception(invalid_argument("Could not open config file: " + path));
Logger::Write(LogInformation, "dyn", "Compiling config file: " + path);
if (!parent) {
stringstream message;
message << "Parent object '" << name << "' does not exist (" << m_DebugInfo << ")";
- throw domain_error(message.str());
+ throw_exception(domain_error(message.str()));
}
parent->CalculateProperties(dictionary);
switch (m_Operator) {
case OperatorExecute:
if (!valueExprl)
- throw invalid_argument("Operand for OperatorExecute must be an ExpressionList.");
+ throw_exception(invalid_argument("Operand for OperatorExecute must be an ExpressionList."));
valueExprl->Execute(dictionary);
if (!oldValue.IsEmpty()) {
stringstream message;
message << "Wrong argument types for += (non-dictionary and dictionary) (" << m_DebugInfo << ")";
- throw domain_error(message.str());
+ throw_exception(domain_error(message.str()));
}
dict = boost::make_shared<Dictionary>();
} else {
stringstream message;
message << "+= only works for dictionaries (" << m_DebugInfo << ")";
- throw domain_error(message.str());
+ throw_exception(domain_error(message.str()));
}
break;
default:
- throw runtime_error("Not yet implemented.");
+ throw_exception(runtime_error("Not yet implemented."));
}
dictionary->Set(m_Key, newValue);
void EndpointManager::AddListener(string service)
{
if (!GetSSLContext())
- throw logic_error("SSL context is required for AddListener()");
+ throw_exception(logic_error("SSL context is required for AddListener()"));
stringstream s;
s << "Adding new listener: port " << service;
{
string method;
if (!message.GetMethod(&method))
- throw invalid_argument("Message is missing the 'method' property.");
+ throw_exception(invalid_argument("Message is missing the 'method' property."));
vector<Endpoint::Ptr> candidates;
Endpoint::Ptr endpoint;
{
string id;
if (message.GetID(&id))
- throw invalid_argument("Multicast requests must not have an ID.");
+ throw_exception(invalid_argument("Multicast requests must not have an ID."));
string method;
if (!message.GetMethod(&method))
- throw invalid_argument("Message is missing the 'method' property.");
+ throw_exception(invalid_argument("Message is missing the 'method' property."));
Endpoint::Ptr recipient;
BOOST_FOREACH(tie(tuples::ignore, recipient), m_Endpoints) {
{
string id;
if (!message.GetID(&id))
- throw invalid_argument("Response message must have a message ID.");
+ throw_exception(invalid_argument("Response message must have a message ID."));
map<string, PendingRequest>::iterator it;
it = m_Requests.find(id);
continue;
} else if (arg == "-L") {
if (it + 1 == args.end())
- throw invalid_argument("Option -L requires a parameter");
+ throw_exception(invalid_argument("Option -L requires a parameter"));
StreamLogger::Ptr fileLogger = boost::make_shared<StreamLogger>(LogInformation);
fileLogger->OpenFile(*(it + 1));
daemonize = true;
continue;
} else {
- throw invalid_argument("Unknown option: " + arg);
+ throw_exception(invalid_argument("Unknown option: " + arg));
}
}
configFile = arg;
if (it + 1 != args.end())
- throw invalid_argument("Trailing command line arguments after config filename.");
+ throw_exception(invalid_argument("Trailing command line arguments after config filename."));
}
if (configFile.empty())
- throw invalid_argument("No config file was specified on the command line.");
+ throw_exception(invalid_argument("No config file was specified on the command line."));
if (enableSyslog) {
#ifndef _WIN32
SyslogLogger::Ptr syslogLogger = boost::make_shared<SyslogLogger>(LogInformation);
Logger::RegisterLogger(syslogLogger);
#else /* _WIN32 */
- throw invalid_argument("Syslog is not supported on Windows.");
+ throw_exception(invalid_argument("Syslog is not supported on Windows."));
#endif /* _WIN32 */
}
ConfigObject::Ptr icingaConfig = ConfigObject::GetObject("application", "icinga");
if (!icingaConfig)
- throw runtime_error("Configuration must contain an 'application' object named 'icinga'.");
+ throw_exception(runtime_error("Configuration must contain an 'application' object named 'icinga'."));
if (!icingaConfig->IsLocal())
- throw runtime_error("'icinga' application object must be 'local'.");
+ throw_exception(runtime_error("'icinga' application object must be 'local'."));
icingaConfig->GetProperty("cert", &m_CertificateFile);
icingaConfig->GetProperty("ca", &m_CAFile);
{
/* don't allow replicated config objects */
if (!object->IsLocal())
- throw runtime_error("'component' objects must be 'local'");
+ throw_exception(runtime_error("'component' objects must be 'local'"));
string path;
if (!object->GetProperty("path", &path)) {
{
/* don't allow replicated config objects */
if (!object->IsLocal())
- throw runtime_error("'log' objects must be 'local'");
+ throw_exception(runtime_error("'log' objects must be 'local'"));
Logger::Ptr logger;
if (object->GetTag("logger", &logger))
string type;
if (!object->GetProperty("type", &type))
- throw invalid_argument("'log' object must have a 'type' property");
+ throw_exception(invalid_argument("'log' object must have a 'type' property"));
string strSeverity;
LogSeverity severity = LogInformation;
#ifndef _WIN32
logger = boost::make_shared<SyslogLogger>(severity);
#else /* _WIN32 */
- throw invalid_argument("Syslog is not supported on Windows.");
+ throw_exception(invalid_argument("Syslog is not supported on Windows."));
#endif /* _WIN32 */
} else if (type == "file") {
string path;
if (!object->GetProperty("path", &path))
- throw invalid_argument("'log' object of type 'file' must have a 'path' property");
+ throw_exception(invalid_argument("'log' object of type 'file' must have a 'path' property"));
StreamLogger::Ptr slogger = boost::make_shared<StreamLogger>(severity);
slogger->OpenFile(path);
} else if (type == "console") {
logger = boost::make_shared<StreamLogger>(&std::cout, severity);
} else {
- throw runtime_error("Unknown log type: " + type);
+ throw_exception(runtime_error("Unknown log type: " + type));
}
object->SetTag("logger", logger);
m_Client = client;
client->OnNewMessage.connect(boost::bind(&JsonRpcEndpoint::NewMessageHandler, this, _2));
client->OnClosed.connect(boost::bind(&JsonRpcEndpoint::ClientClosedHandler, this));
- client->OnError.connect(boost::bind(&JsonRpcEndpoint::ClientErrorHandler, this, _2));
client->OnCertificateValidated.connect(boost::bind(&JsonRpcEndpoint::CertificateValidatedHandler, this));
}
bool JsonRpcEndpoint::IsConnected(void) const
{
- return (bool)m_Client;
+ return (m_Client && m_Client->IsConnected());
}
void JsonRpcEndpoint::ProcessRequest(Endpoint::Ptr sender, const RequestMessage& message)
void JsonRpcEndpoint::ClientClosedHandler(void)
{
+ try {
+ m_Client->CheckException();
+ } catch (const exception& ex) {
+ stringstream message;
+ message << "Error occured for JSON-RPC socket: Message=" << ex.what();
+
+ Logger::Write(LogWarning, "jsonrpc", message.str());
+ }
+
Logger::Write(LogWarning, "jsonrpc", "Lost connection to endpoint: identity=" + GetIdentity());
// TODO: _only_ clear non-persistent publications/subscriptions
void JsonRpcEndpoint::ClientErrorHandler(const exception& ex)
{
- stringstream message;
- message << "Error occured for JSON-RPC socket: Message=" << ex.what();
-
- Logger::Write(LogWarning, "jsonrpc", message.str());
}
void JsonRpcEndpoint::CertificateValidatedHandler(void)
//m_TopicHandlers[method] -= callback;
//UnregisterMethodSubscription(method);
- throw NotImplementedException();
+ throw_exception(NotImplementedException());
}
void VirtualEndpoint::ProcessRequest(Endpoint::Ptr sender, const RequestMessage& request)
json_t *json = cJSON_Parse(jsonString.c_str());
if (!json)
- throw runtime_error("Invalid JSON string");
+ throw_exception(runtime_error("Invalid JSON string"));
m_Dictionary = GetDictionaryFromJson(json);
buffer_length = 16;
char *buffer = (char *)malloc(buffer_length);
+
+ if (buffer == NULL && buffer_length > 0)
+ throw_exception(bad_alloc());
+
queue->Peek(buffer, buffer_length);
/* no leading zeros allowed */
if (buffer[0] == '0' && isdigit(buffer[1])) {
free(buffer);
- throw invalid_argument("Invalid netstring (leading zero)");
+ throw_exception(invalid_argument("Invalid netstring (leading zero)"));
}
size_t len, i;
/* length specifier must have at most 9 characters */
if (i >= 9) {
free(buffer);
- throw invalid_argument("Length specifier must not exceed 9 characters");
+ throw_exception(invalid_argument("Length specifier must not exceed 9 characters"));
}
len = len * 10 + (buffer[i] - '0');
if (new_buffer == NULL) {
free(buffer);
- throw std::bad_alloc();
+ throw_exception(bad_alloc());
}
buffer = new_buffer;
/* check for the colon delimiter */
if (buffer[i] != ':') {
free(buffer);
- throw invalid_argument("Invalid Netstring (missing :)");
+ throw_exception(invalid_argument("Invalid Netstring (missing :)"));
}
/* check for the comma delimiter after the string */
if (buffer[i + 1 + len] != ',') {
free(buffer);
- throw invalid_argument("Invalid Netstring (missing ,)");
+ throw_exception(invalid_argument("Invalid Netstring (missing ,)"));
}
*str = string(&buffer[i + 1], &buffer[i + 1 + len]);