Mantid
Loading...
Searching...
No Matches
CatalogAlgorithmHelper.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
8
9#include <json/reader.h>
10#include <json/value.h>
11#include <set>
12
13using Poco::Net::HTTPResponse;
14
15namespace Mantid::ICat {
16
25const std::string CatalogAlgorithmHelper::getIDSError(const HTTPResponse::HTTPStatus &HTTPStatus,
26 std::istream &responseStream) {
27 std::set<HTTPResponse::HTTPStatus> successHTTPStatus = {HTTPResponse::HTTP_OK, HTTPResponse::HTTP_CREATED,
28 HTTPResponse::HTTP_ACCEPTED};
29
30 // HTTP Status is not one of the positive statuses
31 if (successHTTPStatus.find(HTTPStatus) == successHTTPStatus.end()) {
32 // Attempt to parse response as json stream
33 Json::Value json;
34 ::Json::CharReaderBuilder readerBuilder;
35 std::string errors;
36
37 // Error messages from IDS are returned as json
38 if (Json::parseFromStream(readerBuilder, responseStream, &json, &errors)) {
39 return json.get("code", "UNKNOWN").asString() + ": " + json.get("message", "Unknown Error").asString();
40 } else {
41 // Sometimes the HTTP server can throw an error (which is plain HTML)
42 return "HTTP Error: " + std::to_string(HTTPStatus);
43 }
44 }
45
46 // No error occurred, so return an empty string for verification.
47 return "";
48}
49
50} // namespace Mantid::ICat
const std::string getIDSError(const Poco::Net::HTTPResponse::HTTPStatus &HTTPStatus, std::istream &responseStream)
Obtain the error message returned by the IDS.
std::string to_string(const wide_integer< Bits, Signed > &n)