|
| 1 | +/* Copyright (C) 2018-2025 Davide Faconti, Eurecat - All Rights Reserved |
| 2 | +* |
| 3 | +* Permission is hereby granted, free of charge, to any person obtaining a copy of this |
| 4 | +* software and associated documentation files (the "Software"), to deal in the Software |
| 5 | +* without restriction, including without limitation the rights to use, copy, modify, |
| 6 | +* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to |
| 7 | +* permit persons to whom the Software is furnished to do so, subject to the following |
| 8 | +* conditions: The above copyright notice and this permission notice shall be included in |
| 9 | +* all copies or substantial portions of the Software. |
| 10 | +* |
| 11 | +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 12 | +* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 13 | +* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 14 | +* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 15 | +* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE |
| 16 | +* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 17 | +*/ |
| 18 | + |
| 19 | +#include "behaviortree_cpp/bt_factory.h" |
| 20 | + |
| 21 | +#include <gtest/gtest.h> |
| 22 | + |
| 23 | +using namespace BT; |
| 24 | + |
| 25 | +// Test node that throws an exception |
| 26 | +class ThrowingAction : public SyncActionNode |
| 27 | +{ |
| 28 | +public: |
| 29 | + ThrowingAction(const std::string& name, const NodeConfig& config) |
| 30 | + : SyncActionNode(name, config) |
| 31 | + {} |
| 32 | + |
| 33 | + NodeStatus tick() override |
| 34 | + { |
| 35 | + throw std::runtime_error("Test exception from ThrowingAction"); |
| 36 | + } |
| 37 | + |
| 38 | + static PortsList providedPorts() |
| 39 | + { |
| 40 | + return {}; |
| 41 | + } |
| 42 | +}; |
| 43 | + |
| 44 | +// Test node that succeeds |
| 45 | +class SucceedingAction : public SyncActionNode |
| 46 | +{ |
| 47 | +public: |
| 48 | + SucceedingAction(const std::string& name, const NodeConfig& config) |
| 49 | + : SyncActionNode(name, config) |
| 50 | + {} |
| 51 | + |
| 52 | + NodeStatus tick() override |
| 53 | + { |
| 54 | + return NodeStatus::SUCCESS; |
| 55 | + } |
| 56 | + |
| 57 | + static PortsList providedPorts() |
| 58 | + { |
| 59 | + return {}; |
| 60 | + } |
| 61 | +}; |
| 62 | + |
| 63 | +TEST(ExceptionTracking, BasicExceptionCapture) |
| 64 | +{ |
| 65 | + // Simple tree: Sequence -> ThrowingAction |
| 66 | + const char* xml = R"( |
| 67 | + <root BTCPP_format="4"> |
| 68 | + <BehaviorTree ID="MainTree"> |
| 69 | + <ThrowingAction name="thrower"/> |
| 70 | + </BehaviorTree> |
| 71 | + </root> |
| 72 | + )"; |
| 73 | + |
| 74 | + BehaviorTreeFactory factory; |
| 75 | + factory.registerNodeType<ThrowingAction>("ThrowingAction"); |
| 76 | + |
| 77 | + auto tree = factory.createTreeFromText(xml); |
| 78 | + |
| 79 | + try |
| 80 | + { |
| 81 | + tree.tickOnce(); |
| 82 | + FAIL() << "Expected NodeExecutionError to be thrown"; |
| 83 | + } |
| 84 | + catch(const NodeExecutionError& e) |
| 85 | + { |
| 86 | + // Verify the failed node info |
| 87 | + EXPECT_EQ(e.failedNode().node_name, "thrower"); |
| 88 | + EXPECT_EQ(e.failedNode().registration_name, "ThrowingAction"); |
| 89 | + EXPECT_EQ(e.originalMessage(), "Test exception from ThrowingAction"); |
| 90 | + |
| 91 | + // Verify backtrace has the node |
| 92 | + ASSERT_GE(e.backtrace().size(), 1u); |
| 93 | + EXPECT_EQ(e.backtrace().back().node_name, "thrower"); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +TEST(ExceptionTracking, NestedExceptionBacktrace) |
| 98 | +{ |
| 99 | + // Tree: Sequence -> RetryNode -> ThrowingAction |
| 100 | + // This tests that the backtrace shows the full path |
| 101 | + const char* xml = R"( |
| 102 | + <root BTCPP_format="4"> |
| 103 | + <BehaviorTree ID="MainTree"> |
| 104 | + <Sequence name="main_seq"> |
| 105 | + <SucceedingAction name="first"/> |
| 106 | + <RetryUntilSuccessful num_attempts="1" name="retry"> |
| 107 | + <ThrowingAction name="nested_thrower"/> |
| 108 | + </RetryUntilSuccessful> |
| 109 | + </Sequence> |
| 110 | + </BehaviorTree> |
| 111 | + </root> |
| 112 | + )"; |
| 113 | + |
| 114 | + BehaviorTreeFactory factory; |
| 115 | + factory.registerNodeType<ThrowingAction>("ThrowingAction"); |
| 116 | + factory.registerNodeType<SucceedingAction>("SucceedingAction"); |
| 117 | + |
| 118 | + auto tree = factory.createTreeFromText(xml); |
| 119 | + |
| 120 | + try |
| 121 | + { |
| 122 | + tree.tickOnce(); |
| 123 | + FAIL() << "Expected NodeExecutionError to be thrown"; |
| 124 | + } |
| 125 | + catch(const NodeExecutionError& e) |
| 126 | + { |
| 127 | + // Verify the failed node is the innermost throwing node |
| 128 | + EXPECT_EQ(e.failedNode().node_name, "nested_thrower"); |
| 129 | + |
| 130 | + // Verify backtrace shows the full path (at least 3 nodes: Sequence, Retry, Thrower) |
| 131 | + ASSERT_GE(e.backtrace().size(), 3u); |
| 132 | + |
| 133 | + // Check the what() message contains backtrace info |
| 134 | + std::string what_msg = e.what(); |
| 135 | + EXPECT_NE(what_msg.find("nested_thrower"), std::string::npos); |
| 136 | + EXPECT_NE(what_msg.find("Tick backtrace"), std::string::npos); |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +TEST(ExceptionTracking, SubtreeExceptionBacktrace) |
| 141 | +{ |
| 142 | + // Tree with subtree: MainTree -> Subtree -> ThrowingAction |
| 143 | + const char* xml = R"( |
| 144 | + <root BTCPP_format="4" main_tree_to_execute="MainTree"> |
| 145 | + <BehaviorTree ID="MainTree"> |
| 146 | + <Sequence name="outer_seq"> |
| 147 | + <SubTree ID="InnerTree" name="subtree_call"/> |
| 148 | + </Sequence> |
| 149 | + </BehaviorTree> |
| 150 | + <BehaviorTree ID="InnerTree"> |
| 151 | + <Sequence name="inner_seq"> |
| 152 | + <ThrowingAction name="subtree_thrower"/> |
| 153 | + </Sequence> |
| 154 | + </BehaviorTree> |
| 155 | + </root> |
| 156 | + )"; |
| 157 | + |
| 158 | + BehaviorTreeFactory factory; |
| 159 | + factory.registerNodeType<ThrowingAction>("ThrowingAction"); |
| 160 | + |
| 161 | + auto tree = factory.createTreeFromText(xml); |
| 162 | + |
| 163 | + try |
| 164 | + { |
| 165 | + tree.tickOnce(); |
| 166 | + FAIL() << "Expected NodeExecutionError to be thrown"; |
| 167 | + } |
| 168 | + catch(const NodeExecutionError& e) |
| 169 | + { |
| 170 | + // Verify the failed node is the one in the subtree |
| 171 | + EXPECT_EQ(e.failedNode().node_name, "subtree_thrower"); |
| 172 | + |
| 173 | + // Verify fullPath includes the subtree hierarchy |
| 174 | + std::string full_path = e.failedNode().node_path; |
| 175 | + EXPECT_NE(full_path.find("subtree_thrower"), std::string::npos); |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +TEST(ExceptionTracking, NoExceptionNoWrapping) |
| 180 | +{ |
| 181 | + // Verify that trees that don't throw work normally |
| 182 | + const char* xml = R"( |
| 183 | + <root BTCPP_format="4"> |
| 184 | + <BehaviorTree ID="MainTree"> |
| 185 | + <Sequence> |
| 186 | + <SucceedingAction name="a"/> |
| 187 | + <SucceedingAction name="b"/> |
| 188 | + </Sequence> |
| 189 | + </BehaviorTree> |
| 190 | + </root> |
| 191 | + )"; |
| 192 | + |
| 193 | + BehaviorTreeFactory factory; |
| 194 | + factory.registerNodeType<SucceedingAction>("SucceedingAction"); |
| 195 | + |
| 196 | + auto tree = factory.createTreeFromText(xml); |
| 197 | + |
| 198 | + // Should not throw |
| 199 | + EXPECT_NO_THROW({ |
| 200 | + auto status = tree.tickOnce(); |
| 201 | + EXPECT_EQ(status, NodeStatus::SUCCESS); |
| 202 | + }); |
| 203 | +} |
| 204 | + |
| 205 | +TEST(ExceptionTracking, BacktraceEntryContents) |
| 206 | +{ |
| 207 | + // Test that TickBacktraceEntry contains all expected fields |
| 208 | + const char* xml = R"( |
| 209 | + <root BTCPP_format="4"> |
| 210 | + <BehaviorTree ID="MainTree"> |
| 211 | + <ThrowingAction name="my_action"/> |
| 212 | + </BehaviorTree> |
| 213 | + </root> |
| 214 | + )"; |
| 215 | + |
| 216 | + BehaviorTreeFactory factory; |
| 217 | + factory.registerNodeType<ThrowingAction>("ThrowingAction"); |
| 218 | + |
| 219 | + auto tree = factory.createTreeFromText(xml); |
| 220 | + |
| 221 | + try |
| 222 | + { |
| 223 | + tree.tickOnce(); |
| 224 | + FAIL() << "Expected exception"; |
| 225 | + } |
| 226 | + catch(const NodeExecutionError& e) |
| 227 | + { |
| 228 | + const auto& entry = e.failedNode(); |
| 229 | + // Check all fields are populated |
| 230 | + EXPECT_FALSE(entry.node_name.empty()); |
| 231 | + EXPECT_FALSE(entry.node_path.empty()); |
| 232 | + EXPECT_FALSE(entry.registration_name.empty()); |
| 233 | + |
| 234 | + EXPECT_EQ(entry.node_name, "my_action"); |
| 235 | + EXPECT_EQ(entry.registration_name, "ThrowingAction"); |
| 236 | + } |
| 237 | +} |
0 commit comments