fix: Print out error details of web context (#2351)

This commit is contained in:
emrearıyürek
2025-09-22 14:45:04 +02:00
committed by GitHub
parent 245a808e4b
commit d2de240389
3 changed files with 112 additions and 2 deletions

View File

@@ -39,6 +39,39 @@ using namespace std;
namespace rpc {
std::ostream&
operator<<(std::ostream& stream, Status const& status)
{
std::visit(
util::OverloadSet{
[&stream, &status](RippledError err) {
stream << "Code: " << static_cast<std::underlying_type_t<RippledError>>(err);
if (!status.error.empty())
stream << ", Error: " << status.error;
if (!status.message.empty())
stream << ", Message: " << status.message;
else
stream << ", Message: " << ripple::RPC::get_error_info(err).message;
},
[&stream, &status](ClioError err) {
stream << "Code: " << static_cast<std::underlying_type_t<ClioError>>(err);
if (!status.error.empty())
stream << ", Error: " << status.error;
if (!status.message.empty())
stream << ", Message: " << status.message;
else
stream << ", Message: " << getErrorInfo(err).message;
}
},
status.code
);
if (status.extraInfo.has_value())
stream << ", Extra Info: " << *status.extraInfo;
return stream;
}
WarningInfo const&
getWarningInfo(WarningCode code)
{

View File

@@ -182,6 +182,16 @@ struct Status {
return false;
}
/**
* @brief Custom output stream for Status
*
* @param stream The output stream
* @param status The Status
* @return The same ostream we were given
*/
friend std::ostream&
operator<<(std::ostream& stream, Status const& status);
};
/** @brief Warning codes that can be returned by clio. */

View File

@@ -18,6 +18,7 @@
//==============================================================================
#include "rpc/Errors.hpp"
#include "util/NameGenerator.hpp"
#include <boost/json/fwd.hpp>
#include <boost/json/object.hpp>
@@ -139,7 +140,7 @@ TEST(RPCErrorsTest, InvalidClioErrorToJSON)
}
struct WarningCodeTestBundle {
std::string name;
std::string testName;
WarningCode code;
std::string message;
};
@@ -166,7 +167,7 @@ INSTANTIATE_TEST_SUITE_P(
"https://xrpl.org/docs/references/http-websocket-apis/ and update your request."
}
),
[](testing::TestParamInfo<WarningCodeTestBundle> const& info) { return info.param.name; }
tests::util::kNAME_GENERATOR
);
TEST_P(WarningCodeTest, WarningToJSON)
@@ -189,3 +190,69 @@ TEST(RPCErrorsTest, InvalidWarningToJSON)
};
EXPECT_ANY_THROW((void)notSanitizedMakeWarning());
}
struct StatusStreamTestBundle {
std::string testName;
rpc::Status status;
std::string expectedOutput;
};
struct RPCErrorsStatusStreamTest : public ::testing::TestWithParam<StatusStreamTestBundle> {
protected:
std::ostringstream oss;
};
TEST_P(RPCErrorsStatusStreamTest, StatusStreamOperator)
{
auto const param = GetParam();
oss << param.status;
EXPECT_EQ(oss.str(), param.expectedOutput);
}
INSTANTIATE_TEST_SUITE_P(
RPCErrorsTest,
RPCErrorsStatusStreamTest,
testing::Values(
StatusStreamTestBundle{
.testName = "EmptyStatus",
.status = Status{},
.expectedOutput = "Code: 0, Message: An unknown error code."
},
StatusStreamTestBundle{
.testName = "StatusWithRippledError",
.status = Status{RippledError::rpcSUCCESS},
.expectedOutput = "Code: 0, Message: An unknown error code."
},
StatusStreamTestBundle{
.testName = "StatusWithClioError",
.status = Status{ClioError::RpcParamsUnparsable},
.expectedOutput = "Code: 6004, Message: Params must be an array holding exactly one object."
},
StatusStreamTestBundle{
.testName = "StatusWithCodeAndExtraInfo",
.status = Status{ClioError::EtlConnectionError, boost::json::object{}},
.expectedOutput = "Code: 7000, Message: Couldn't connect to rippled., Extra Info: {}"
},
StatusStreamTestBundle{
.testName = "StatusWithMessage",
.status = Status{"test message."},
.expectedOutput = "Code: -1, Message: test message."
},
StatusStreamTestBundle{
.testName = "StatusWithRippledErrorAndMessage",
.status = Status{RippledError::rpcSUCCESS, "test message."},
.expectedOutput = "Code: 0, Message: test message."
},
StatusStreamTestBundle{
.testName = "StatusWithClioErrorAndMessage",
.status = Status{ClioError::RpcParamsUnparsable, "Missing params array."},
.expectedOutput = "Code: 6004, Message: Missing params array."
},
StatusStreamTestBundle{
.testName = "StatusWithCodeErrorMessage",
.status = Status{ClioError::EtlInvalidResponse, "invalidResponse", "Rippled returned an invalid response."},
.expectedOutput = "Code: 7003, Error: invalidResponse, Message: Rippled returned an invalid response."
}
),
tests::util::kNAME_GENERATOR
);