Commit error and exit the application on error scenarios

Typically sd_event* calls can not go wrong and if they do, better
would be to terminate the application so we get a coredump.

Change-Id: I899177c369332cabf6d9bc22daaa142b50d758d0
Signed-off-by: Vishwanatha Subbanna <vishwa@linux.vnet.ibm.com>
diff --git a/Makefile.am b/Makefile.am
index 2e9a4f1..806a0aa 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -18,5 +18,4 @@
                              ${PHOSPHOR_LOGGING_CFLAGS} \
                              ${SDBUSPLUS_CFLAGS} \
                              ${PHOSPHOR_DBUS_INTERFACES_CFLAGS}
-
 SUBDIRS = test
diff --git a/mainapp.cpp b/mainapp.cpp
index 7751aa9..43328c3 100644
--- a/mainapp.cpp
+++ b/mainapp.cpp
@@ -16,6 +16,9 @@
 
 #include <iostream>
 #include <phosphor-logging/log.hpp>
+#include <phosphor-logging/elog.hpp>
+#include <phosphor-logging/elog-errors.hpp>
+#include <xyz/openbmc_project/Common/error.hpp>
 #include "argument.hpp"
 #include "watchdog.hpp"
 
@@ -29,7 +32,8 @@
 int main(int argc, char** argv)
 {
     using namespace phosphor::logging;
-
+    using InternalFailure = sdbusplus::xyz::openbmc_project::Common::
+                                Error::InternalFailure;
     // Read arguments.
     auto options = phosphor::watchdog::ArgumentParser(argc, argv);
 
@@ -71,23 +75,32 @@
     // Attach the bus to sd_event to service user requests
     bus.attach_event(eventP.get(), SD_EVENT_PRIORITY_NORMAL);
 
-    // Create a watchdog object
-    phosphor::watchdog::Watchdog watchdog(bus, path.c_str(),
-                                          eventP, std::move(target));
-
-    // Claim the bus
-    bus.request_name(service.c_str());
-
-    // Wait until the timer has expired
-    while(!watchdog.timerExpired())
+    try
     {
-        // -1 denotes wait for ever
-        r = sd_event_run(eventP.get(), (uint64_t)-1);
-        if (r < 0)
+        // Create a watchdog object
+        phosphor::watchdog::Watchdog watchdog(bus, path.c_str(),
+                                              eventP, std::move(target));
+        // Claim the bus
+        bus.request_name(service.c_str());
+
+        // Wait until the timer has expired
+        while(!watchdog.timerExpired())
         {
-            log<level::ERR>("Error waiting for events");
-            return -1;
+            // -1 denotes wait for ever
+            r = sd_event_run(eventP.get(), (uint64_t)-1);
+            if (r < 0)
+            {
+                log<level::ERR>("Error waiting for events");
+                elog<InternalFailure>();
+            }
         }
     }
+    catch(InternalFailure& e)
+    {
+        phosphor::logging::commit<InternalFailure>();
+
+        // Need a coredump in the error cases.
+        std::terminate();
+    }
     return 0;
 }
diff --git a/test/Makefile.am b/test/Makefile.am
index 654f0f1..259e357 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -34,6 +34,7 @@
 timer_test_SOURCES = timer_test.cpp
 watchdog_test_SOURCES = watchdog_test.cpp
 
-timer_test_LDADD = $(top_builddir)/timer.o
-watchdog_test_LDADD = $(top_builddir)/timer.o \
-                      $(top_builddir)/watchdog.o
+timer_test_LDADD =  $(top_builddir)/timer.o
+
+watchdog_test_LDADD = $(top_builddir)/watchdog.o \
+                      $(top_builddir)/timer.o
diff --git a/timer.cpp b/timer.cpp
index c98dcb0..2825855 100644
--- a/timer.cpp
+++ b/timer.cpp
@@ -1,20 +1,28 @@
 #include <chrono>
 #include <systemd/sd-event.h>
 #include <phosphor-logging/log.hpp>
+#include <phosphor-logging/elog.hpp>
+#include <phosphor-logging/elog-errors.hpp>
+#include <xyz/openbmc_project/Common/error.hpp>
 #include "timer.hpp"
 namespace phosphor
 {
 namespace watchdog
 {
 
+// For throwing exception
+using namespace phosphor::logging;
+using InternalFailure = sdbusplus::xyz::openbmc_project::Common::
+                            Error::InternalFailure;
+
 // Initializes the timer object
 void Timer::initialize()
 {
     // This can not be called more than once.
     if (eventSource.get())
     {
-        // TODO: Need to throw elog exception stating its already added.
-        throw std::runtime_error("Timer already initialized");
+        log<level::ERR>("Timer already initialized");
+        elog<InternalFailure>();
     }
 
     // Add infinite expiration time
@@ -30,8 +38,8 @@
 
     if (r < 0)
     {
-        // TODO: throw elog exception
-        throw std::runtime_error("Timer initialization failed");
+        log<level::ERR>("Timer initialization failed");
+        elog<InternalFailure>();
     }
 
     // Disable the timer for now
@@ -68,6 +76,8 @@
 // Sets the expiration time and arms the timer
 void Timer::start(std::chrono::microseconds usec)
 {
+    using namespace std::chrono;
+
     // Get the current MONOTONIC time and add the delta
     auto expireTime = getCurrentTime() + usec;
 
@@ -76,8 +86,9 @@
                                       expireTime.count());
     if (r < 0)
     {
-        // TODO throw elog exception
-        throw std::runtime_error("Error setting the expiration time");
+        log<level::ERR>("Error setting the expiration time",
+                entry("MSEC=%llu",duration_cast<milliseconds>(usec).count()));
+        elog<InternalFailure>();
     }
 }
 
@@ -88,8 +99,8 @@
     auto r = sd_event_source_get_enabled(eventSource.get(), &enabled);
     if (r < 0)
     {
-        // TODO: Need to throw elog exception
-        throw std::runtime_error("Error geting current time enablement state");
+        log<level::ERR>("Error geting current timer type enablement state");
+        elog<InternalFailure>();
     }
     return enabled;
 }
@@ -100,8 +111,9 @@
     auto r = sd_event_source_set_enabled(eventSource.get(), type);
     if (r < 0)
     {
-        // TODO: Need to throw elog exception
-        throw std::runtime_error("Error altering enabled property");
+        log<level::ERR>("Error setting the timer type",
+                entry("TYPE=%d",type));
+        elog<InternalFailure>();
     }
 }
 
@@ -112,8 +124,8 @@
     auto r = sd_event_source_get_time(eventSource.get(), &next);
     if (r < 0)
     {
-        // TODO: Need to throw elog exception
-        throw std::runtime_error("Error altering enabled property");
+        log<level::ERR>("Error fetching remaining time to expire");
+        elog<InternalFailure>();
     }
     return std::chrono::microseconds(next);
 }