Commit 8e19cece authored by Anthony Lichnewsky's avatar Anthony Lichnewsky
Browse files

WIP: additional diagnostics for failure to construct a dataset object from...

WIP: additional diagnostics for failure to construct a dataset object from json returned by sdms service
parent 4421482f
Pipeline #52238 passed with stages
in 8 minutes and 6 seconds
......@@ -613,6 +613,7 @@ namespace seismicdrive
return dirlist;
}
// this assumes already that the response is accepted and correctly formatted.
Dataset SeismicStore::createDataset(const HTTPManager &http, const std::string &tag) const
{
auto headers = http.response_headers();
......@@ -636,7 +637,36 @@ namespace seismicdrive
throw SDExpectedJsonException(ss.str());
}
auto cloudProvider = headers["service-provider"];
return Dataset(JsonUtils::tojson(http.get_response(), tag), cloudProvider);
// potential failure modes:
// response is not json, fails to parse. a proxy did something, the service implementation failed, etc..
// response is json ( or empty ) and fails to find all fields required by Dataset ctor from JSON.
try {
return Dataset(JsonUtils::tojson(http.get_response(), tag), cloudProvider);
} catch (...) {
std::ostringstream oss;
oss << "failed to build a Dataset out of HTTP response :\n";
static const std::string sep = "-----------------------------------------------------------------------\n";
oss << sep;
// TODO: expose url query ...
oss << "[request] " << http.get_http_method_as_string() << " " << http.get_url() << "\n";
oss << sep;
for (auto const& header : http.response_headers())
{
oss << "[header] " << header.first << ": " << header.second << '\n';
}
oss << sep;
oss << http.get_response() << '\n';
oss<< sep;
std::cout << oss.str() << std::flush;
throw;
}
}
std::string SeismicStore::escapeString(const std::string &str) const
......
......@@ -40,7 +40,7 @@ namespace seismicdrive
{
if (shoudlthrow)
{
throw SDExceptionValueError(sdmex::value_error::JsonParsing(variable));
throw SDExceptionValueError(sdmex::value_error::JsonParsing(variable, root.toStyledString()));
}
else
{
......@@ -55,7 +55,7 @@ namespace seismicdrive
const auto my_var = root.get(variable, Json::Value::null);
if (my_var == Json::Value::null)
{
throw SDExceptionValueError(sdmex::value_error::JsonParsing(variable));
throw SDExceptionValueError(sdmex::value_error::JsonParsing(variable, root.toStyledString()));
}
return std::stoll(my_var.asString());
}
......@@ -65,7 +65,7 @@ namespace seismicdrive
const auto my_var = root.get(variable, Json::Value::null);
if (my_var == Json::Value::null)
{
throw SDExceptionValueError(sdmex::value_error::JsonParsing(variable));
throw SDExceptionValueError(sdmex::value_error::JsonParsing(variable, root.toStyledString()));
}
return std::stoull(my_var.asString());
}
......@@ -75,7 +75,7 @@ namespace seismicdrive
const auto my_var = root.get(variable, Json::Value::null);
if (my_var == Json::Value::null)
{
throw SDExceptionValueError(sdmex::value_error::JsonParsing(variable));
throw SDExceptionValueError(sdmex::value_error::JsonParsing(variable, root.toStyledString()));
}
std::string res(sdutils::toLower(my_var.asString()));
return res == "true";
......@@ -113,7 +113,7 @@ namespace seismicdrive
{
if (shoudlthrow)
{
throw SDExceptionValueError(sdmex::value_error::JsonParsing(variable));
throw SDExceptionValueError(sdmex::value_error::JsonParsing(variable, root.toStyledString()));
}
return {};
}
......
......@@ -324,9 +324,9 @@ namespace seismicdrive
namespace value_error
{
inline const std::string JsonParsing(std::string &var)
inline const std::string JsonParsing(std::string &var, const std::string & response = "")
{
return "The " + var + " field was expected in the JSON response but it was not found.";
return "The " + var + " field was expected in the JSON response " + response + " but it was not found.";
}
inline const std::string ResponseParsing(const std::string &datastr, const std::string &tag)
......
......@@ -16,6 +16,7 @@
#include "gtest/gtest.h"
#include "gmock/gmock-matchers.h"
#include "Dataset.h"
#include "DatasetPath.h"
......@@ -43,6 +44,52 @@ TEST(DatasetTest, dataset_test_ctor) {
}
TEST(DatasetTest, dataset_test_ctor_misformed_json) {
Json::Value ds01{Json::Value::null};
//ds01["name"] = "dsx01";
ds01["path"] = "/a/b/c/d";
ds01["user"] = "user";
ds01["time"] = "time";
ds01["gcsurl"] = "gcsurl";
ds01["tenant"] = "tnx01";
ds01["subproject"] = "spx01";
ds01["created_by"] = "me";
ds01["created_date"] = "123";
ds01["status"] = true;
ds01["last_modified_date"] = "123";
ds01["ctag"] = "1234123412341234abc";
EXPECT_THAT([&ds01]() { Dataset x3(ds01); },
testing::Throws<SDExceptionValueError>(
testing::Property(&SDExceptionValueError::what, testing::HasSubstr("The name field was expected in the JSON response "))));
}
TEST(DatasetTest, dataset_test_ctor_null_json) {
Json::Value ds01{Json::Value::null};
#if 1
Dataset x1(ds01);
// uh? this is possible?
ASSERT_TRUE(true);
// surely such a dataset is going to be problematic... nothign is initialized.
#else
// why not ?
EXPECT_THAT([&ds01]() { Dataset x3(ds01); },
testing::Throws<SDExceptionValueError>(
testing::Property(&SDExceptionValueError::what, testing::HasSubstr("The name field was expected in the JSON response "))));
#endif
}
TEST(DatasetTest, dataset_test_ctor_empty_string_to_json) {
EXPECT_THAT([]() {
Json::Value ds01 = JsonUtils::tojson("");
Dataset x1(ds01);
},
testing::Throws<SDExceptionValueError>(
testing::Property(&SDExceptionValueError::what, testing::HasSubstr("an error occurred while trying to convert a string data into a json object"))));
}
TEST(DatasetPathTest, dataset_path_test_ctor) {
DatasetPath ds1("sd://tnx01/spx01/a/b/c", true);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment