Skip to content

Commit 64cac39

Browse files
azerbenalifacontidavideclaude
authored
fix: bad_expected_access::what() returns the error message (#1269)
* fix: bad_expected_access::what() returns the error message (#1263) what() returned a fixed "bad_expected_access", so the actual error of a failed Expected<T> was lost. When the error type converts to std::string, the message is now returned instead. It is stored in a shared_ptr so that copying the exception does not allocate. * simplify bad_expected_access::what(): reuse m_error instead of a second copy Return m_error.c_str() through an overload on std::string, with a generic fallback for other error types. No extra member, so the class layout is unchanged, and copying the exception no longer copies the message twice. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Davide Faconti <davide.faconti@gmail.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 726c857 commit 64cac39

2 files changed

Lines changed: 57 additions & 1 deletion

File tree

‎include/behaviortree_cpp/contrib/expected.hpp‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ namespace nonstd {
258258
#include <initializer_list>
259259
#include <memory>
260260
#include <new>
261+
#include <string>
261262
#include <system_error>
262263
#include <type_traits>
263264
#include <utility>
@@ -1607,9 +1608,10 @@ class bad_expected_access : public bad_expected_access< void >
16071608
: m_error( error )
16081609
{}
16091610

1611+
// BT.CPP patch (#1263): report the error message when the error is a string.
16101612
virtual char const * what() const noexcept override
16111613
{
1612-
return "bad_expected_access";
1614+
return what_of( m_error );
16131615
}
16141616

16151617
nsel_constexpr14 error_type & error() &
@@ -1637,6 +1639,17 @@ class bad_expected_access : public bad_expected_access< void >
16371639
#endif
16381640

16391641
private:
1642+
static char const * what_of( std::string const & error ) noexcept
1643+
{
1644+
return error.empty() ? "bad_expected_access" : error.c_str();
1645+
}
1646+
1647+
template< typename T >
1648+
static char const * what_of( T const & ) noexcept
1649+
{
1650+
return "bad_expected_access";
1651+
}
1652+
16401653
error_type m_error;
16411654
};
16421655

‎tests/gtest_basic_types.cpp‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,49 @@ TEST(BasicTypes, Result_Error)
484484
ASSERT_EQ(result.error(), "error message");
485485
}
486486

487+
TEST(BasicTypes, Expected_BadAccessMessage)
488+
{
489+
auto bb = Blackboard::create();
490+
try
491+
{
492+
(void)bb->getStamped<int>("missing_key").value();
493+
FAIL() << "value() should throw";
494+
}
495+
catch(const std::exception& ex)
496+
{
497+
const std::string msg = ex.what();
498+
EXPECT_NE(msg.find("Missing key [missing_key]"), std::string::npos) << msg;
499+
}
500+
501+
Expected<int> res = nonstd::make_unexpected(std::string("error message"));
502+
try
503+
{
504+
(void)res.value();
505+
FAIL() << "value() should throw";
506+
}
507+
catch(const nonstd::bad_expected_access<std::string>& ex)
508+
{
509+
const auto copy = ex;
510+
EXPECT_STREQ(copy.what(), "error message");
511+
EXPECT_EQ(copy.error(), "error message");
512+
}
513+
}
514+
515+
TEST(BasicTypes, Expected_BadAccessMessageNotString)
516+
{
517+
nonstd::expected<int, int> res = nonstd::make_unexpected(42);
518+
try
519+
{
520+
(void)res.value();
521+
FAIL() << "value() should throw";
522+
}
523+
catch(const nonstd::bad_expected_access<int>& ex)
524+
{
525+
EXPECT_STREQ(ex.what(), "bad_expected_access");
526+
EXPECT_EQ(ex.error(), 42);
527+
}
528+
}
529+
487530
// ============ StringView tests ============
488531

489532
TEST(BasicTypes, StringView_FromString)

0 commit comments

Comments
 (0)