Skip to content

Commit

Permalink
Include field name when diagnosing field not found:
Browse files Browse the repository at this point in the history
Closes #3263
  • Loading branch information
thejohnfreeman authored and carlhua committed Mar 5, 2020
1 parent f76a5a3 commit 053b6d9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
18 changes: 11 additions & 7 deletions src/ripple/protocol/STObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ namespace ripple {

class STArray;

//------------------------------------------------------------------------------
inline void
throwFieldNotFound(SField const& field)
{
Throw<std::runtime_error>("Field not found: " + field.getName());
}

class STObject
: public STBase
Expand Down Expand Up @@ -492,7 +496,7 @@ class STObject
STBase* rf = getPField (field, true);

if (! rf)
Throw<std::runtime_error> ("Field not found");
throwFieldNotFound(field);

if (rf->getSType () == STI_NOTPRESENT)
rf = makeFieldPresent (field);
Expand Down Expand Up @@ -552,7 +556,7 @@ class STObject
const STBase* rf = peekAtPField (field);

if (! rf)
Throw<std::runtime_error> ("Field not found");
throwFieldNotFound(field);

SerializedTypeID id = rf->getSType ();

Expand All @@ -578,7 +582,7 @@ class STObject
const STBase* rf = peekAtPField (field);

if (! rf)
Throw<std::runtime_error> ("Field not found");
throwFieldNotFound(field);

SerializedTypeID id = rf->getSType ();

Expand All @@ -602,7 +606,7 @@ class STObject
STBase* rf = getPField (field, true);

if (! rf)
Throw<std::runtime_error> ("Field not found");
throwFieldNotFound(field);

if (rf->getSType () == STI_NOTPRESENT)
rf = makeFieldPresent (field);
Expand All @@ -622,7 +626,7 @@ class STObject
STBase* rf = getPField (field, true);

if (! rf)
Throw<std::runtime_error> ("Field not found");
throwFieldNotFound(field);

if (rf->getSType () == STI_NOTPRESENT)
rf = makeFieldPresent (field);
Expand All @@ -642,7 +646,7 @@ class STObject
STBase* rf = getPField (field, true);

if (! rf)
Throw<std::runtime_error> ("Field not found");
throwFieldNotFound(field);

if (rf->getSType () == STI_NOTPRESENT)
rf = makeFieldPresent (field);
Expand Down
8 changes: 4 additions & 4 deletions src/ripple/protocol/impl/STObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ const STBase& STObject::peekAtField (SField const& field) const
int index = getFieldIndex (field);

if (index == -1)
Throw<std::runtime_error> ("Field not found");
throwFieldNotFound(field);

return peekAtIndex (index);
}
Expand All @@ -353,7 +353,7 @@ STBase& STObject::getField (SField const& field)
int index = getFieldIndex (field);

if (index == -1)
Throw<std::runtime_error> ("Field not found");
throwFieldNotFound(field);

return getIndex (index);
}
Expand Down Expand Up @@ -453,7 +453,7 @@ STBase* STObject::makeFieldPresent (SField const& field)
if (index == -1)
{
if (!isFree ())
Throw<std::runtime_error> ("Field not found");
throwFieldNotFound(field);

return getPIndex (emplace_back(detail::nonPresentObject, field));
}
Expand All @@ -473,7 +473,7 @@ void STObject::makeFieldAbsent (SField const& field)
int index = getFieldIndex (field);

if (index == -1)
Throw<std::runtime_error> ("Field not found");
throwFieldNotFound(field);

const STBase& f = peekAtIndex (index);

Expand Down
19 changes: 15 additions & 4 deletions src/test/protocol/STTx_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,22 @@
#include <ripple/protocol/messages.h>

#include <memory>
#include <regex>

namespace ripple {

/**
* Return true if the string loosely matches the regex.
*
* Meant for testing human-readable strings that may change over time.
*/
inline bool
matches(char const* string, char const* regex)
{
return std::regex_search(
string, std::basic_regex<char>(regex, std::regex_constants::icase));
}

class STTx_test : public beast::unit_test::suite
{
public:
Expand Down Expand Up @@ -1283,8 +1296,7 @@ class STTx_test : public beast::unit_test::suite
}
catch (std::runtime_error const& ex)
{
BEAST_EXPECT (
std::strcmp (ex.what(), "Field not found") == 0);
BEAST_EXPECT(matches(ex.what(), "field not found"));
}
}

Expand Down Expand Up @@ -1348,8 +1360,7 @@ class STTx_test : public beast::unit_test::suite
}
catch (std::runtime_error const& ex)
{
BEAST_EXPECT (
std::strcmp (ex.what(), "Field not found") == 0);
BEAST_EXPECT(matches(ex.what(), "field not found"));
}
}

Expand Down

0 comments on commit 053b6d9

Please sign in to comment.