Implemented rudimentary error handling.

This commit is contained in:
Gunnar Beutner 2012-04-03 13:01:00 +02:00
parent b30284c64c
commit 7ba6a4d921
12 changed files with 127 additions and 13 deletions

View File

@ -17,6 +17,8 @@ libbase_la_SOURCES = \
configobject.h \ configobject.h \
delegate.h \ delegate.h \
event.h \ event.h \
exception.cpp \
exception.h \
fifo.cpp \ fifo.cpp \
fifo.h \ fifo.h \
i2-base.h \ i2-base.h \

View File

@ -196,7 +196,7 @@ Component::Ptr Application::LoadComponent(const string& path, const ConfigObject
#endif /* _WIN32 */ #endif /* _WIN32 */
if (hModule == NULL) if (hModule == NULL)
throw exception(/*"Could not load module"*/); throw ComponentLoadException("Could not load module");
#ifdef _WIN32 #ifdef _WIN32
pCreateComponent = (Component *(*)())GetProcAddress(hModule, "CreateComponent"); pCreateComponent = (Component *(*)())GetProcAddress(hModule, "CreateComponent");
@ -205,7 +205,7 @@ Component::Ptr Application::LoadComponent(const string& path, const ConfigObject
#endif /* _WIN32 */ #endif /* _WIN32 */
if (pCreateComponent == NULL) if (pCreateComponent == NULL)
throw exception(/*"Module does not contain CreateComponent function"*/); throw ComponentLoadException("Loadable module does not contain CreateComponent function");
component = Component::Ptr(pCreateComponent()); component = Component::Ptr(pCreateComponent());
component->SetApplication(static_pointer_cast<Application>(shared_from_this())); component->SetApplication(static_pointer_cast<Application>(shared_from_this()));
@ -292,10 +292,7 @@ const string& Application::GetExeDirectory(void)
PathEnv = getenv("PATH"); PathEnv = getenv("PATH");
if (PathEnv != NULL) { if (PathEnv != NULL) {
PathEnv = strdup(PathEnv); PathEnv = Memory::StrDup(PathEnv);
if (PathEnv == NULL)
throw exception(/*"strdup() failed"*/);
FoundPath = false; FoundPath = false;

View File

@ -5,6 +5,8 @@ namespace icinga {
class Component; class Component;
DEFINE_EXCEPTION_CLASS(ComponentLoadException);
class Application : public Object { class Application : public Object {
private: private:
bool m_ShuttingDown; bool m_ShuttingDown;
@ -56,7 +58,19 @@ int application_main(int argc, char **argv)
Application::Instance->SetArguments(args); Application::Instance->SetArguments(args);
#ifndef _DEBUG
try {
#endif /* !_DEBUG */
result = Application::Instance->Main(args); result = Application::Instance->Main(args);
#ifndef _DEBUG
} catch (const Exception& ex) {
cout << "---" << endl;
cout << "Exception: " << typeid(ex).name() << endl;
cout << "Message: " << ex.GetMessage() << endl;
return EXIT_FAILURE;
}
#endif /* !_DEBUG */
Application::Instance.reset(); Application::Instance.reset();

View File

@ -16,6 +16,7 @@
<ClCompile Include="condvar.cpp" /> <ClCompile Include="condvar.cpp" />
<ClCompile Include="confighive.cpp" /> <ClCompile Include="confighive.cpp" />
<ClCompile Include="configobject.cpp" /> <ClCompile Include="configobject.cpp" />
<ClCompile Include="exception.cpp" />
<ClCompile Include="fifo.cpp" /> <ClCompile Include="fifo.cpp" />
<ClCompile Include="memory.cpp" /> <ClCompile Include="memory.cpp" />
<ClCompile Include="mutex.cpp" /> <ClCompile Include="mutex.cpp" />
@ -37,6 +38,7 @@
<ClInclude Include="configobject.h" /> <ClInclude Include="configobject.h" />
<ClInclude Include="delegate.h" /> <ClInclude Include="delegate.h" />
<ClInclude Include="event.h" /> <ClInclude Include="event.h" />
<ClInclude Include="exception.h" />
<ClInclude Include="fifo.h" /> <ClInclude Include="fifo.h" />
<ClInclude Include="i2-base.h" /> <ClInclude Include="i2-base.h" />
<ClInclude Include="memory.h" /> <ClInclude Include="memory.h" />

21
base/exception.cpp Normal file
View File

@ -0,0 +1,21 @@
#include "i2-base.h"
using namespace icinga;
Exception::Exception(void)
{
}
Exception::Exception(const string& message)
{
m_Message = message;
}
Exception::~Exception(void)
{
}
string Exception::GetMessage(void) const
{
return m_Message;
}

42
base/exception.h Normal file
View File

@ -0,0 +1,42 @@
#ifndef EXCEPTION_H
#define EXCEPTION_H
namespace icinga
{
class Exception
{
private:
string m_Message;
public:
typedef shared_ptr<Exception> Ptr;
typedef weak_ptr<Exception> WeakPtr;
Exception(void);
Exception(const string& message);
virtual ~Exception(void);
string GetMessage(void) const;
};
}
#define DEFINE_EXCEPTION_CLASS(klass) \
class klass : public Exception \
{ \
public: \
typedef shared_ptr<klass> Ptr; \
typedef weak_ptr<klass> WeakPtr; \
\
inline klass(void) : Exception() \
{ \
} \
\
inline klass(const string& message) : Exception(message) \
{ \
} \
};
#endif /* EXCEPTION_H */

View File

@ -47,6 +47,7 @@ using namespace std::tr1::placeholders;
#include "condvar.h" #include "condvar.h"
#include "thread.h" #include "thread.h"
#include "object.h" #include "object.h"
#include "exception.h"
#include "memory.h" #include "memory.h"
#include "delegate.h" #include "delegate.h"
#include "event.h" #include "event.h"

View File

@ -2,6 +2,10 @@
using namespace icinga; using namespace icinga;
Memory::Memory(void)
{
}
void *Memory::Allocate(size_t size) void *Memory::Allocate(size_t size)
{ {
void *ptr = malloc(size); void *ptr = malloc(size);
@ -22,6 +26,16 @@ void *Memory::Reallocate(void *ptr, size_t size)
return new_ptr; return new_ptr;
} }
char *Memory::StrDup(const char *str)
{
char *new_str = strdup(str);
if (str == NULL)
throw OutOfMemoryException();
return new_str;
}
void Memory::Free(void *ptr) void Memory::Free(void *ptr)
{ {
if (ptr != NULL) if (ptr != NULL)

View File

@ -4,16 +4,17 @@
namespace icinga namespace icinga
{ {
class OutOfMemoryException : public exception { }; DEFINE_EXCEPTION_CLASS(OutOfMemoryException);
class Memory class Memory
{ {
private: private:
Memory(void) { } Memory(void);
public: public:
static void *Allocate(size_t size); static void *Allocate(size_t size);
static void *Reallocate(void *ptr, size_t size); static void *Reallocate(void *ptr, size_t size);
static char *StrDup(const char *str);
static void Free(void *ptr); static void Free(void *ptr);
}; };

View File

@ -44,6 +44,22 @@ shared_ptr<T> new_object(void)
return shared_ptr<T>(instance); return shared_ptr<T>(instance);
} }
template<class T, class TArg1>
shared_ptr<T> new_object(const TArg1& arg1)
{
T *instance = new T(arg1);
return shared_ptr<T>(instance);
}
template<class T, class TArg1, class TArg2>
shared_ptr<T> new_object(const TArg1& arg1, const TArg2& arg2)
{
T *instance = new T(arg1, arg2);
return shared_ptr<T>(instance);
}
typedef function<Object::Ptr ()> factory_function; typedef function<Object::Ptr ()> factory_function;
template<class T> template<class T>

View File

@ -17,18 +17,20 @@ void ConfigFileComponent::Start(void)
string filename; string filename;
if (!GetConfig()->GetProperty("configFilename", &filename)) if (!GetConfig()->GetProperty("configFilename", &filename))
throw exception(/*"Missing configFilename property"*/); throw ConfigParserException("Missing configFilename property");
fp.open(filename.c_str(), ifstream::in); fp.open(filename.c_str(), ifstream::in);
if (fp.fail()) if (fp.fail())
throw exception(/*"Could not open config file"*/); throw ConfigParserException("Could not open config file");
GetApplication()->Log("Reading config file: %s", filename.c_str());
while (!fp.eof()) { while (!fp.eof()) {
size_t bufferSize = 1024; size_t bufferSize = 1024;
char *buffer = (char *)fifo->GetWriteBuffer(&bufferSize); char *buffer = (char *)fifo->GetWriteBuffer(&bufferSize);
fp.read(buffer, bufferSize); fp.read(buffer, bufferSize);
if (fp.bad()) if (fp.bad())
throw exception(/*"Could not read from config file"*/); throw ConfigParserException("Could not read from config file");
fifo->Write(NULL, fp.gcount()); fifo->Write(NULL, fp.gcount());
} }
@ -41,7 +43,7 @@ void ConfigFileComponent::Start(void)
fifo->Read(NULL, fifo->GetSize()); fifo->Read(NULL, fifo->GetSize());
if (jsonobj == NULL) if (jsonobj == NULL)
throw exception(/*"Could not parse config file."*/); throw ConfigParserException("Could not parse config file.");
for (cJSON *typeobj = jsonobj->child; typeobj != NULL; typeobj = typeobj->next) { for (cJSON *typeobj = jsonobj->child; typeobj != NULL; typeobj = typeobj->next) {
string type = typeobj->string; string type = typeobj->string;

View File

@ -4,6 +4,8 @@
namespace icinga namespace icinga
{ {
DEFINE_EXCEPTION_CLASS(ConfigParserException);
class ConfigFileComponent : public Component class ConfigFileComponent : public Component
{ {
public: public: