Check for expression compile failures

If the expression doesn't compile, print the errors and throw an
exception to crash the program since there is no point in continuing.

This would be caused by a mistake in the JSON file.

Signed-off-by: Matt Spinler <spinler@us.ibm.com>
Change-Id: I0f648a32f1a1e5729f96289d59fcea1cea9da91f
diff --git a/meson.build b/meson.build
index 6de3ea0..de28513 100644
--- a/meson.build
+++ b/meson.build
@@ -28,6 +28,7 @@
         dependency('sdbusplus'),
         dependency('phosphor-dbus-interfaces'),
         dependency('sdeventplus'),
+        dependency('fmt'),
         exprtk,
     ],
     install: true,
diff --git a/virtualSensor.cpp b/virtualSensor.cpp
index 9f2516e..ce429f6 100644
--- a/virtualSensor.cpp
+++ b/virtualSensor.cpp
@@ -2,6 +2,8 @@
 
 #include "config.hpp"
 
+#include <fmt/format.h>
+
 #include <phosphor-logging/log.hpp>
 #include <sdeventplus/event.hpp>
 
@@ -156,7 +158,22 @@
 
     /* parser from exprtk */
     exprtk::parser<double> parser{};
-    parser.compile(exprStr, expression);
+    if (!parser.compile(exprStr, expression))
+    {
+        log<level::ERR>("Expression compilation failed");
+
+        for (std::size_t i = 0; i < parser.error_count(); ++i)
+        {
+            auto error = parser.get_error(i);
+
+            log<level::ERR>(
+                fmt::format(
+                    "Position: {} Type: {} Message: {}", error.token.position,
+                    exprtk::parser_error::to_str(error.mode), error.diagnostic)
+                    .c_str());
+        }
+        throw std::runtime_error("Expression compilation failed");
+    }
 
     /* Print all parameters for debug purpose only */
     if (DEBUG)