[common] Add std::exception handler to ParsedInstance 19/251819/3
authorPawel Wasowski <p.wasowski2@samsung.com>
Mon, 18 Jan 2021 19:35:14 +0000 (20:35 +0100)
committerPiotr Kosko <p.kosko@samsung.com>
Tue, 19 Jan 2021 13:09:21 +0000 (13:09 +0000)
When an exception derived from std::exception and not PlatformException
is thrown in webapi-plugins code, only the following error message was
logged:

HandleException(393) > Exception: Unknown exception

As std::exception inheritance is a very common practice, especially in
standard library, we add logging std::exception::what() message to make
debugging easier.

[Verification] An exception thrown from std::string is caught and its
what() message is logged properly.

Change-Id: I879ebe80b609cf26a7e68be822a783aa9ef145eb
Signed-off-by: Pawel Wasowski <p.wasowski2@samsung.com>
src/common/extension.cc
src/common/extension.h

index 6a0a2c3..7a1be11 100644 (file)
@@ -383,6 +383,8 @@ void ParsedInstance::HandleMessage(const char* msg, bool is_sync) {
     return HandleException(e);
   } catch (const PlatformException* e) {
     return HandleException(*e);
+  } catch (const std::exception& e) {
+    return HandleException(e);
   } catch (...) {
     return HandleException(UnknownException("Unknown exception"));
   }
@@ -396,6 +398,15 @@ void ParsedInstance::HandleException(const PlatformException& ex) {
   SendSyncReply(result.serialize().c_str());
 }
 
+void ParsedInstance::HandleException(const std::exception& e) {
+  ScopeLogger();
+  LoggerE("std::exception: %s", e.what());
+  picojson::value result = picojson::value(picojson::object());
+  ReportError(common::PlatformResult{common::ErrorCode::ABORT_ERR, "An unknown internal error"},
+              &result.get<picojson::object>());
+  SendSyncReply(result.serialize().c_str());
+}
+
 void ParsedInstance::HandleError(const PlatformResult& e) {
   LoggerE("Error: %d", static_cast<int>(e.error_code()));
   picojson::value result = picojson::value(picojson::object());
index 404e3f7..99f459e 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <sys/types.h>
 
+#include <exception>
 #include <functional>
 #include <map>
 #include <mutex>
@@ -142,6 +143,7 @@ class ParsedInstance : public Instance {
 
   void HandleMessage(const char* msg, bool is_sync);
   void HandleException(const PlatformException& ex);
+  void HandleException(const std::exception& e);
   void HandleError(const PlatformResult& error);
 
   std::map<std::string, NativeHandler> handler_map_;