Improve error handling for exceptions and asserts
The phosphor-psu-code-manager application currently exits abnormally due
to the following conditions:
* Uncaught exception
* False assert() statement
An abnormal exit can result in a core dump and/or a BMC dump. It also
causes the service to be restarted. If the failure condition remains,
the restarts will fail repeatedly, and systemd will stop trying to start
the service.
Improve error handling for exceptions in the following ways:
* Add try/catch blocks to the following locations:
* Code that calls functions that throw and needs to handle exceptions.
* For example, code looping over PSU objects may need to handle an
exception for one PSU and then continue to the remaining PSUs.
* D-Bus PropertiesChanged and InterfacesAdded event handlers.
* Do not allow exceptions to escape to the sdbusplus stack frames.
* main()
* Last line of defense; catching avoids a core dump.
* Write exception error message to the journal if appropriate
Replace assert statements with exceptions or error messages to the
journal.
Tested:
* Tested all modified functions/methods.
* Verified that all exceptions were caught and logged to the journal if
appropriate.
* Verified that asserts were replaced by exceptions and logging.
* See complete test plan at
https://gist.github.com/smccarney/b4bf568639fedd269c9737234fa2803d
Change-Id: I933386e94f43a915b301d6aef7d91691816a0548
Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
diff --git a/src/runtime_warning.hpp b/src/runtime_warning.hpp
new file mode 100644
index 0000000..78db37c
--- /dev/null
+++ b/src/runtime_warning.hpp
@@ -0,0 +1,45 @@
+#pragma once
+
+#include <exception>
+#include <string>
+
+namespace phosphor::software::updater
+{
+
+/**
+ * @class RuntimeWarning
+ *
+ * Exception class to report a runtime warning condition.
+ */
+class RuntimeWarning : public std::exception
+{
+ public:
+ // Specify which compiler-generated methods we want
+ RuntimeWarning() = delete;
+ RuntimeWarning(const RuntimeWarning&) = default;
+ RuntimeWarning(RuntimeWarning&&) = default;
+ RuntimeWarning& operator=(const RuntimeWarning&) = default;
+ RuntimeWarning& operator=(RuntimeWarning&&) = default;
+ ~RuntimeWarning() override = default;
+
+ /** @brief Constructor.
+ *
+ * @param error error message
+ */
+ explicit RuntimeWarning(const std::string& error) : error{error} {}
+
+ /** @brief Returns the description of this error.
+ *
+ * @return error description
+ */
+ const char* what() const noexcept override
+ {
+ return error.c_str();
+ }
+
+ private:
+ /** @brief Error message */
+ std::string error;
+};
+
+} // namespace phosphor::software::updater