From 927cda45cfea4173ee414d3c3c744a4b7f3d67a3 Mon Sep 17 00:00:00 2001 From: Jay Rainey Date: Wed, 22 Jan 2014 11:38:21 +0000 Subject: [PATCH] Moved IDSError method to algorithm helper. Refs #8727. - As I will use the exact same logic when checking for an IDS error in `CatalogDownloadDataFiles`. - Updated IDSError method signature and logic to be more robust and usable across algorithms. --- .../inc/MantidICat/CatalogAlgorithmHelper.h | 2 ++ .../ICat/inc/MantidICat/CatalogPublish.h | 2 -- .../ICat/src/CatalogAlgorithmHelper.cpp | 31 +++++++++++++++++ .../Framework/ICat/src/CatalogPublish.cpp | 34 ++++--------------- 4 files changed, 39 insertions(+), 30 deletions(-) diff --git a/Code/Mantid/Framework/ICat/inc/MantidICat/CatalogAlgorithmHelper.h b/Code/Mantid/Framework/ICat/inc/MantidICat/CatalogAlgorithmHelper.h index 98e93e6eeed3..2fff8cce6620 100644 --- a/Code/Mantid/Framework/ICat/inc/MantidICat/CatalogAlgorithmHelper.h +++ b/Code/Mantid/Framework/ICat/inc/MantidICat/CatalogAlgorithmHelper.h @@ -15,6 +15,8 @@ namespace Mantid public: /// Create a catalog to use in the algorithms. API::ICatalog_sptr createCatalog(); + /// Obtain the error message returned by the IDS. + const std::string getIDSError(const std::string &HTTPStatus, std::istream& responseStream); }; } } diff --git a/Code/Mantid/Framework/ICat/inc/MantidICat/CatalogPublish.h b/Code/Mantid/Framework/ICat/inc/MantidICat/CatalogPublish.h index 7520d581850b..e99ffc1772c0 100644 --- a/Code/Mantid/Framework/ICat/inc/MantidICat/CatalogPublish.h +++ b/Code/Mantid/Framework/ICat/inc/MantidICat/CatalogPublish.h @@ -66,8 +66,6 @@ namespace Mantid void publishWorkspaceHistory(Mantid::API::ICatalog_sptr &catalog, Mantid::API::Workspace_sptr &workspace); /// Generate the history of a given workspace. const std::string generateWorkspaceHistory(Mantid::API::Workspace_sptr &workspace); - /// Obtain the error message returned by the IDS. - const std::string getIDSError(const std::string& jsonResponseData); }; } } diff --git a/Code/Mantid/Framework/ICat/src/CatalogAlgorithmHelper.cpp b/Code/Mantid/Framework/ICat/src/CatalogAlgorithmHelper.cpp index 3cfd6ee97952..0fcfb17bdd86 100644 --- a/Code/Mantid/Framework/ICat/src/CatalogAlgorithmHelper.cpp +++ b/Code/Mantid/Framework/ICat/src/CatalogAlgorithmHelper.cpp @@ -1,5 +1,8 @@ #include "MantidICat/CatalogAlgorithmHelper.h" +#include +#include + namespace Mantid { namespace ICat @@ -22,5 +25,33 @@ namespace Mantid } return catalog; } + + + /** + * Obtain the error message returned by the IDS. + * @param HTTPStatus :: The HTTPStatus returned by the IDS. + * @param responseStream :: The contents of the stream (a JSON stream) returned from the IDS. + * @returns An appropriate error message for the user if it exists. Otherwise an empty string. + */ + const std::string CatalogAlgorithmHelper::getIDSError(const std::string &HTTPStatus, std::istream& responseStream) + { + // Set containing all valid HTTPStatus'. + std::string tmp[] = {"200", "201", "202"}; + std::set successHTTPStatus(tmp, tmp + sizeof(tmp) / sizeof(tmp[0])); + + // Cancel the algorithm and output message if status returned + // from the server is not in our successHTTPStatus set. + if (successHTTPStatus.find(HTTPStatus) == successHTTPStatus.end()) + { + // Stores the contents of `jsonResponseData` as a json property tree. + boost::property_tree::ptree json; + // Convert the stream to a JSON tree. + boost::property_tree::read_json(responseStream, json); + // Return the message returned by the server. + return json.get("code") + ": " + json.get("message"); + } + // No error occurred, so return an empty string for verification. + return ""; + } } } diff --git a/Code/Mantid/Framework/ICat/src/CatalogPublish.cpp b/Code/Mantid/Framework/ICat/src/CatalogPublish.cpp index 2cdd822661b2..4aed1051e219 100644 --- a/Code/Mantid/Framework/ICat/src/CatalogPublish.cpp +++ b/Code/Mantid/Framework/ICat/src/CatalogPublish.cpp @@ -30,8 +30,6 @@ datafiles or workspaces to investigations of which they are an investigator. #include #include -#include -#include namespace Mantid { @@ -144,7 +142,6 @@ namespace Mantid // Sets the encoding type of the request. This enables us to stream data to the server. request.setChunkedTransferEncoding(true); std::ostream& os = session.sendRequest(request); - // Copy data from the input stream to the server (request) output stream. Poco::StreamCopier::copyStream(fileContents, os); @@ -152,23 +149,20 @@ namespace Mantid Poco::Net::HTTPResponse response; // Store the response for use IF an error occurs (e.g. 404). std::istream& responseStream = session.receiveResponse(response); + // Obtain the status returned by the server to verify if it was a success. std::string HTTPStatus = boost::lexical_cast(response.getStatus()); - - // Throw an error if publishing was not successful. - // (Note: The IDS does not currently return any meta-data related to the errors caused.) - if (HTTPStatus.find("20") == std::string::npos) + // The error message returned by the IDS (if one exists). + std::string IDSError = CatalogAlgorithmHelper().getIDSError(HTTPStatus, responseStream); + // Cancel the algorithm and display the message if it exists. + if(!IDSError.empty()) { - std::string jsonResponseData; - // Copy the input stream to a string. - Poco::StreamCopier::copyToString(responseStream, jsonResponseData); - // As an error occurred we must cancel the algorithm. // We cannot throw an exception here otherwise it is caught below as Poco::Exception catches runtimes, // and then the I/O error is thrown as it is generated above first. this->cancel(); // Output an appropriate error message from the JSON object returned by the IDS. - g_log.error(getIDSError(jsonResponseData)); + g_log.error(IDSError); } } catch(Poco::Net::SSLException& error) @@ -239,21 +233,5 @@ namespace Mantid return wsHistory->getPropertyValue("ScriptText"); } - - /** - * Obtain the error message returned by the IDS. - * @param jsonResponseData :: The contents of the JSON object returned from the IDS. - * @returns An appropriate error message for the user. - */ - const std::string CatalogPublish::getIDSError(const std::string& jsonResponseData) - { - std::istringstream is(jsonResponseData); - // Stores the contents of `jsonResponseData` as a json property tree. - boost::property_tree::ptree json; - // Convert the stream to a JSON tree. - boost::property_tree::read_json(is, json); - // Return the message returned by the server. - return json.get("code") + ": " + json.get("message"); - } } }