Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion include/behaviortree_cpp/contrib/expected.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ namespace nonstd {
#include <initializer_list>
#include <memory>
#include <new>
#include <string>
#include <system_error>
#include <type_traits>
#include <utility>
Expand Down Expand Up @@ -1607,9 +1608,10 @@ class bad_expected_access : public bad_expected_access< void >
: m_error( error )
{}

// BT.CPP patch (#1263): report the error message when the error is a string.
virtual char const * what() const noexcept override
{
return "bad_expected_access";
return what_of( m_error );
}

nsel_constexpr14 error_type & error() &
Expand Down Expand Up @@ -1637,6 +1639,17 @@ class bad_expected_access : public bad_expected_access< void >
#endif

private:
static char const * what_of( std::string const & error ) noexcept
{
return error.empty() ? "bad_expected_access" : error.c_str();
}

template< typename T >
static char const * what_of( T const & ) noexcept
{
return "bad_expected_access";
}

error_type m_error;
};

Expand Down
43 changes: 43 additions & 0 deletions tests/gtest_basic_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,49 @@ TEST(BasicTypes, Result_Error)
ASSERT_EQ(result.error(), "error message");
}

TEST(BasicTypes, Expected_BadAccessMessage)
{
auto bb = Blackboard::create();
try
{
(void)bb->getStamped<int>("missing_key").value();
FAIL() << "value() should throw";
}
catch(const std::exception& ex)
{
const std::string msg = ex.what();
EXPECT_NE(msg.find("Missing key [missing_key]"), std::string::npos) << msg;
}

Expected<int> res = nonstd::make_unexpected(std::string("error message"));
try
{
(void)res.value();
FAIL() << "value() should throw";
}
catch(const nonstd::bad_expected_access<std::string>& ex)
{
const auto copy = ex;
EXPECT_STREQ(copy.what(), "error message");
EXPECT_EQ(copy.error(), "error message");
}
}

TEST(BasicTypes, Expected_BadAccessMessageNotString)
{
nonstd::expected<int, int> res = nonstd::make_unexpected(42);
try
{
(void)res.value();
FAIL() << "value() should throw";
}
catch(const nonstd::bad_expected_access<int>& ex)
{
EXPECT_STREQ(ex.what(), "bad_expected_access");
EXPECT_EQ(ex.error(), 42);
}
}

// ============ StringView tests ============

TEST(BasicTypes, StringView_FromString)
Expand Down
Loading