]> granicus.if.org Git - icinga2/commitdiff
Implemented rudimentary error handling.
authorGunnar Beutner <gunnar.beutner@netways.de>
Tue, 3 Apr 2012 11:01:00 +0000 (13:01 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Tue, 3 Apr 2012 11:04:57 +0000 (13:04 +0200)
12 files changed:
base/Makefile.am
base/application.cpp
base/application.h
base/base.vcxproj
base/exception.cpp [new file with mode: 0644]
base/exception.h [new file with mode: 0644]
base/i2-base.h
base/memory.cpp
base/memory.h
base/object.h
configfilecomponent/configfilecomponent.cpp
configfilecomponent/configfilecomponent.h

index c8f50b60281b1b78a3a34506111d11bc0a13a5fb..3113fbc8558b795e8e6aed3bb2845d8f79acbf24 100644 (file)
@@ -17,6 +17,8 @@ libbase_la_SOURCES =  \
        configobject.h \
        delegate.h \
        event.h \
+       exception.cpp \
+       exception.h \
        fifo.cpp \
        fifo.h \
        i2-base.h \
index 099fe0440105fcc9fd56f3c9be8ae6e8a113ffb5..df4949ab066bdf706df0bff7c4fa2921dbcc3ec3 100644 (file)
@@ -196,7 +196,7 @@ Component::Ptr Application::LoadComponent(const string& path, const ConfigObject
 #endif /* _WIN32 */
 
        if (hModule == NULL)
-               throw exception(/*"Could not load module"*/);
+               throw ComponentLoadException("Could not load module");
 
 #ifdef _WIN32
        pCreateComponent = (Component *(*)())GetProcAddress(hModule, "CreateComponent");
@@ -205,7 +205,7 @@ Component::Ptr Application::LoadComponent(const string& path, const ConfigObject
 #endif /* _WIN32 */
 
        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->SetApplication(static_pointer_cast<Application>(shared_from_this()));
@@ -292,10 +292,7 @@ const string& Application::GetExeDirectory(void)
                PathEnv = getenv("PATH");
 
                if (PathEnv != NULL) {
-                       PathEnv = strdup(PathEnv);
-
-                       if (PathEnv == NULL)
-                               throw exception(/*"strdup() failed"*/);
+                       PathEnv = Memory::StrDup(PathEnv);
 
                        FoundPath = false;
 
index 99248cb88c8f21f0b941b124223f7b8b606d6b92..a27afbb01ad148970111926e66e02cc2cb853254 100644 (file)
@@ -5,6 +5,8 @@ namespace icinga {
 
 class Component;
 
+DEFINE_EXCEPTION_CLASS(ComponentLoadException);
+
 class Application : public Object {
 private:
        bool m_ShuttingDown;
@@ -56,7 +58,19 @@ int application_main(int argc, char **argv)
 
        Application::Instance->SetArguments(args);
 
-       result = Application::Instance->Main(args);
+#ifndef _DEBUG
+       try {
+#endif /* !_DEBUG */
+               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();
 
index 3802c4fa3ce858223d4f23666a697b8a004d258e..9b176e50856d3b637adf76bd937b11a22112e192 100644 (file)
@@ -16,6 +16,7 @@
     <ClCompile Include="condvar.cpp" />
     <ClCompile Include="confighive.cpp" />
     <ClCompile Include="configobject.cpp" />
+    <ClCompile Include="exception.cpp" />
     <ClCompile Include="fifo.cpp" />
     <ClCompile Include="memory.cpp" />
     <ClCompile Include="mutex.cpp" />
@@ -37,6 +38,7 @@
     <ClInclude Include="configobject.h" />
     <ClInclude Include="delegate.h" />
     <ClInclude Include="event.h" />
+    <ClInclude Include="exception.h" />
     <ClInclude Include="fifo.h" />
     <ClInclude Include="i2-base.h" />
     <ClInclude Include="memory.h" />
diff --git a/base/exception.cpp b/base/exception.cpp
new file mode 100644 (file)
index 0000000..f120c5f
--- /dev/null
@@ -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;
+}
diff --git a/base/exception.h b/base/exception.h
new file mode 100644 (file)
index 0000000..4853dde
--- /dev/null
@@ -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 */
index 47fae22e30353e64e4805e03e710faae11db3370..cfccd7b81d547c266a5143774fdd5933014eebb5 100644 (file)
@@ -47,6 +47,7 @@ using namespace std::tr1::placeholders;
 #include "condvar.h"
 #include "thread.h"
 #include "object.h"
+#include "exception.h"
 #include "memory.h"
 #include "delegate.h"
 #include "event.h"
index 426825c318a729cb5af192f66da0cf90facaee60..d41d8a5a778f5e90d3241dd8418a9995beb8b0a5 100644 (file)
@@ -2,6 +2,10 @@
 
 using namespace icinga;
 
+Memory::Memory(void)
+{
+}
+
 void *Memory::Allocate(size_t size)
 {
        void *ptr = malloc(size);
@@ -22,6 +26,16 @@ void *Memory::Reallocate(void *ptr, size_t size)
        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)
 {
        if (ptr != NULL)
index 1126c8089eba62b0ad405d24fe39d9d8d5d5d468..9abd0d28c3cef31dadf353635711e54b7db31ee6 100644 (file)
@@ -4,16 +4,17 @@
 namespace icinga
 {
 
-class OutOfMemoryException : public exception { };
+DEFINE_EXCEPTION_CLASS(OutOfMemoryException);
 
 class Memory
 {
 private:
-       Memory(void) { }
+       Memory(void);
 
 public:
        static void *Allocate(size_t size);
        static void *Reallocate(void *ptr, size_t size);
+       static char *StrDup(const char *str);
        static void Free(void *ptr);
 };
 
index 8fa3f48269438d5efa68fda64464321b63bf89d5..00096adcd392b47e86bd9959b91a165df402fa8c 100644 (file)
@@ -44,6 +44,22 @@ shared_ptr<T> new_object(void)
        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;
 
 template<class T>
index e99e8fdcc2e508df25ed9d40e772ab8b8769d639..688b88878f5860b63749ef5a594bf3353b1fa115 100644 (file)
@@ -17,18 +17,20 @@ void ConfigFileComponent::Start(void)
 
        string filename;
        if (!GetConfig()->GetProperty("configFilename", &filename))
-               throw exception(/*"Missing configFilename property"*/);
+               throw ConfigParserException("Missing configFilename property");
 
        fp.open(filename.c_str(), ifstream::in);
        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()) {
                size_t bufferSize = 1024;
                char *buffer = (char *)fifo->GetWriteBuffer(&bufferSize);
                fp.read(buffer, bufferSize);
                if (fp.bad())
-                       throw exception(/*"Could not read from config file"*/);
+                       throw ConfigParserException("Could not read from config file");
                fifo->Write(NULL, fp.gcount());
        }
 
@@ -41,7 +43,7 @@ void ConfigFileComponent::Start(void)
        fifo->Read(NULL, fifo->GetSize());
 
        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) {
                string type = typeobj->string;
index d681b530fae3430eeb0cf65cf9bb4f12bf8a814c..8d195f6abbe72e7b9757d237cfec1bdae08adfd5 100644 (file)
@@ -4,6 +4,8 @@
 namespace icinga
 {
 
+DEFINE_EXCEPTION_CLASS(ConfigParserException);
+
 class ConfigFileComponent : public Component
 {
 public: