From 774d2288a89177b62375f336505a5f9f425b2c7f Mon Sep 17 00:00:00 2001
From: Sergey Yakubov <sergey.yakubov@desy.de>
Date: Thu, 6 May 2021 14:16:20 +0200
Subject: [PATCH] consumer add request api to error message

---
 common/cpp/include/asapo/common/error.h |  5 ++++
 consumer/api/cpp/src/consumer_impl.cpp  | 32 ++++++++++++++++---------
 consumer/api/cpp/src/consumer_impl.h    |  2 +-
 3 files changed, 27 insertions(+), 12 deletions(-)

diff --git a/common/cpp/include/asapo/common/error.h b/common/cpp/include/asapo/common/error.h
index 24d78d539..d99b3aead 100644
--- a/common/cpp/include/asapo/common/error.h
+++ b/common/cpp/include/asapo/common/error.h
@@ -38,6 +38,7 @@ class ErrorInterface {
   public:
     virtual std::string Explain() const noexcept = 0;
     virtual void Append(const std::string& value) noexcept = 0;
+    virtual void Prepend(const std::string& value) noexcept = 0;
     virtual ErrorType GetErrorType() const noexcept = 0;
     virtual CustomErrorData* GetCustomData() = 0;
     virtual void SetCustomData(std::unique_ptr<CustomErrorData> data) = 0;
@@ -112,6 +113,10 @@ class SimpleError: public ErrorInterface {
         error_ += ": " + value;
     }
 
+    void Prepend(const std::string& value) noexcept override {
+        error_ = value+": "+error_;
+    }
+
     std::string Explain() const noexcept override  {
         return error_;
     }
diff --git a/consumer/api/cpp/src/consumer_impl.cpp b/consumer/api/cpp/src/consumer_impl.cpp
index 60848dfa3..b6eb00b86 100644
--- a/consumer/api/cpp/src/consumer_impl.cpp
+++ b/consumer/api/cpp/src/consumer_impl.cpp
@@ -93,19 +93,29 @@ Error ConsumerErrorFromHttpCode(const RequestOutput* response, const HttpCode &c
 }
 Error ConsumerErrorFromServerError(const Error &server_err) {
     if (server_err == HttpErrorTemplates::kTransferError) {
-        return ConsumerErrorTemplates::kInterruptedTransaction.Generate(
-            "error processing request: " + server_err->Explain());
+        return ConsumerErrorTemplates::kInterruptedTransaction.Generate(server_err->Explain());
     } else {
-        return ConsumerErrorTemplates::kUnavailableService.Generate(
-            "error processing request: " + server_err->Explain());
+        return ConsumerErrorTemplates::kUnavailableService.Generate(server_err->Explain());
     }
 }
 
-Error ProcessRequestResponce(const Error &server_err, const RequestOutput* response, const HttpCode &code) {
+Error ProcessRequestResponce(const RequestInfo &request,
+                             const Error &server_err,
+                             const RequestOutput* response,
+                             const HttpCode &code) {
+    Error err;
     if (server_err != nullptr) {
-        return ConsumerErrorFromServerError(server_err);
+        err =  ConsumerErrorFromServerError(server_err);
+    } else {
+        err =  ConsumerErrorFromHttpCode(response, code);
+    }
+
+    if (err!=nullptr) {
+        std::string prefix = "Error processing request" + request.api;
+        err->Prepend(prefix);
     }
-    return ConsumerErrorFromHttpCode(response, code);
+    return err;
+
 }
 
 ConsumerImpl::ConsumerImpl(std::string server_uri,
@@ -179,7 +189,7 @@ Error ConsumerImpl::ProcessRequest(RequestOutput* response, const RequestInfo &r
     if (err && service_uri) {
         service_uri->clear();
     }
-    return ProcessRequestResponce(err, response, code);
+    return ProcessRequestResponce(request, err, response, code);
 }
 
 RequestInfo ConsumerImpl::GetDiscoveryRequest(const std::string &service_name) const {
@@ -939,7 +949,7 @@ Error ConsumerImpl::GetServerVersionInfo(std::string* server_info, bool* support
     if (err) {
         return err;
     }
-    return ExtractVersionFromResponse(output.string_output,"consumer",server_info,supported);
+    return ExtractVersionFromResponse(output.string_output, "consumer", server_info, supported);
 }
 
 Error ConsumerImpl::GetVersionInfo(std::string* client_info, std::string* server_info, bool* supported) {
@@ -952,7 +962,7 @@ Error ConsumerImpl::GetVersionInfo(std::string* client_info, std::string* server
     }
 
     if (server_info != nullptr || supported != nullptr) {
-        return GetServerVersionInfo(server_info,supported);
+        return GetServerVersionInfo(server_info, supported);
     }
 
     return nullptr;
@@ -970,7 +980,7 @@ RequestInfo ConsumerImpl::GetDeleteStreamRequest(std::string stream, DeleteStrea
 }
 
 Error ConsumerImpl::DeleteStream(std::string stream, DeleteStreamOptions options) {
-    auto ri = GetDeleteStreamRequest(std::move(stream),options);
+    auto ri = GetDeleteStreamRequest(std::move(stream), options);
     Error err;
     BrokerRequestWithTimeout(ri, &err);
     return err;
diff --git a/consumer/api/cpp/src/consumer_impl.h b/consumer/api/cpp/src/consumer_impl.h
index 9fe29bdf8..7f7411c99 100644
--- a/consumer/api/cpp/src/consumer_impl.h
+++ b/consumer/api/cpp/src/consumer_impl.h
@@ -46,7 +46,7 @@ struct RequestOutput {
     }
 };
 
-Error ProcessRequestResponce(const Error& server_err, const RequestOutput* response, const HttpCode& code);
+Error ProcessRequestResponce(const RequestInfo& request, const Error& server_err, const RequestOutput* response, const HttpCode& code);
 Error ConsumerErrorFromNoDataResponse(const std::string& response);
 Error ConsumerErrorFromPartialDataResponse(const std::string& response);
 DataSet DecodeDatasetFromResponse(std::string response, Error* err);
-- 
GitLab