Generalize the "Async Response" pattern

Lots of endpoints have been using the AsyncResp structure, and some,
like the systems schemas, have created their own.  This is the start of
a series of patches to move to a more condensed async response object.

Tested by: ran GET on systems schema and observed no change to behavior.

Change-Id: I4c9afc583be3f75371b31cc76dcc02590ce5e9a7
diff --git a/redfish-core/include/node.hpp b/redfish-core/include/node.hpp
index 8dbb0c2..80e0744 100644
--- a/redfish-core/include/node.hpp
+++ b/redfish-core/include/node.hpp
@@ -19,6 +19,8 @@
 #include "token_authorization_middleware.hpp"
 #include "webserver_common.hpp"
 
+#include <error_messages.hpp>
+
 #include "crow.h"
 
 namespace redfish
@@ -37,6 +39,23 @@
 
     ~AsyncResp()
     {
+        if (res.result() != boost::beast::http::status::ok)
+        {
+            nlohmann::json::iterator error = res.jsonValue.find("error");
+
+            if (error == res.jsonValue.end())
+            {
+                // If an error value hasn't yet been set, assume that whatever
+                // content we have is garbage, and provide a worthless internal
+                // server error
+                res.jsonValue = {};
+            }
+            // Reset the json object to clear out any data that made it in
+            // before the error happened todo(ed) handle error condition with
+            // proper code
+            messages::addMessageToErrorJson(res.jsonValue,
+                                            messages::internalError());
+        }
         res.end();
     }