BehaviorTree
Core Library to create and execute Behavior Trees
Loading...
Searching...
No Matches
exceptions.h
1/* Copyright (C) 2015-2018 Michele Colledanchise - All Rights Reserved
2 * Copyright (C) 2018-2025 Davide Faconti, Eurecat - All Rights Reserved
3*
4* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
5* to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
6* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
7* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8*
9* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
10* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
11* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12*/
13
14#ifndef BT_EXCEPTIONS_H
15#define BT_EXCEPTIONS_H
16
17#include <stdexcept>
18#include <string>
19#include <vector>
20
21#include "utils/strcat.hpp"
22
23namespace BT
24{
25class BehaviorTreeException : public std::exception
26{
27public:
28 BehaviorTreeException(std::string_view message)
29 : message_(static_cast<std::string>(message))
30 {}
31
32 template <typename... SV>
33 BehaviorTreeException(const SV&... args) : message_(StrCat(args...))
34 {}
35
36 const char* what() const noexcept
37 {
38 return message_.c_str();
39 }
40
41private:
42 std::string message_;
43};
44
45// This errors are usually related to problems which "probably" require code refactoring
46// to be fixed.
48{
49public:
50 LogicError(std::string_view message) : BehaviorTreeException(message)
51 {}
52
53 template <typename... SV>
54 LogicError(const SV&... args) : BehaviorTreeException(args...)
55 {}
56};
57
58// This errors are usually related to problems that are relted to data or conditions
59// that happen only at run-time
61{
62public:
63 RuntimeError(std::string_view message) : BehaviorTreeException(message)
64 {}
65
66 template <typename... SV>
67 RuntimeError(const SV&... args) : BehaviorTreeException(args...)
68 {}
69};
70
71/// Information about a node in the tick backtrace.
73{
74 std::string node_name;
75 std::string node_path;
76 std::string registration_name;
77};
78
79/// Exception thrown when a node's tick() method throws an exception.
80/// Contains information about the node where the exception originated.
82{
83public:
84 NodeExecutionError(TickBacktraceEntry failed_node, const std::string& original_message)
85 : RuntimeError(formatMessage(failed_node, original_message))
86 , failed_node_(std::move(failed_node))
87 , original_message_(original_message)
88 {}
89
90 /// The node that threw the exception
91 [[nodiscard]] const TickBacktraceEntry& failedNode() const
92 {
93 return failed_node_;
94 }
95
96 [[nodiscard]] const std::string& originalMessage() const
97 {
98 return original_message_;
99 }
100
101private:
102 TickBacktraceEntry failed_node_;
103 std::string original_message_;
104
105 static std::string formatMessage(const TickBacktraceEntry& node,
106 const std::string& original_msg)
107 {
108 return StrCat("Exception in node '", node.node_path, "' [", node.registration_name,
109 "]: ", original_msg);
110 }
111};
112
113} // namespace BT
114
115#endif
Definition: exceptions.h:26
Definition: exceptions.h:48
Definition: exceptions.h:82
const TickBacktraceEntry & failedNode() const
The node that threw the exception.
Definition: exceptions.h:91
Definition: exceptions.h:61
Definition: action_node.h:24
Information about a node in the tick backtrace.
Definition: exceptions.h:73