Skip to content

Commit

Permalink
Moved IDSError method to algorithm helper. Refs #8727.
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
jawrainey committed Jan 22, 2014
1 parent 750af37 commit 927cda4
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 30 deletions.
Expand Up @@ -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);
};
}
}
Expand Down
2 changes: 0 additions & 2 deletions Code/Mantid/Framework/ICat/inc/MantidICat/CatalogPublish.h
Expand Up @@ -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);
};
}
}
Expand Down
31 changes: 31 additions & 0 deletions Code/Mantid/Framework/ICat/src/CatalogAlgorithmHelper.cpp
@@ -1,5 +1,8 @@
#include "MantidICat/CatalogAlgorithmHelper.h"

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>

namespace Mantid
{
namespace ICat
Expand All @@ -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<std::string> 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<std::string>("code") + ": " + json.get<std::string>("message");
}
// No error occurred, so return an empty string for verification.
return "";
}
}
}
34 changes: 6 additions & 28 deletions Code/Mantid/Framework/ICat/src/CatalogPublish.cpp
Expand Up @@ -30,8 +30,6 @@ datafiles or workspaces to investigations of which they are an investigator.

#include <fstream>
#include <boost/regex.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>

namespace Mantid
{
Expand Down Expand Up @@ -144,31 +142,27 @@ 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);

// Close the request by requesting a response.
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<std::string>(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)
Expand Down Expand Up @@ -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<std::string>("code") + ": " + json.get<std::string>("message");
}
}
}

0 comments on commit 927cda4

Please sign in to comment.