rclcpp  master
C++ ROS Client Library API
exceptions.hpp
Go to the documentation of this file.
1 // Copyright 2016 Open Source Robotics Foundation, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef RCLCPP__EXCEPTIONS_HPP_
16 #define RCLCPP__EXCEPTIONS_HPP_
17 
18 #include <stdexcept>
19 #include <string>
20 #include <vector>
21 
22 #include "rcl/error_handling.h"
23 #include "rcl/types.h"
25 
26 #include "rcpputils/join.hpp"
27 
28 namespace rclcpp
29 {
30 namespace exceptions
31 {
32 
35 {
36 public:
38  : std::runtime_error("node is invalid") {}
39 };
40 
43 {
44 public:
46  const char * name_type_,
47  const char * name_,
48  const char * error_msg_,
49  size_t invalid_index_)
50  : std::invalid_argument(format_error(name_type_, name_, error_msg_, invalid_index_)),
51  name_type(name_type_), name(name_), error_msg(error_msg_), invalid_index(invalid_index_)
52  {}
53 
54  static std::string
55  format_error(
56  const char * name_type,
57  const char * name,
58  const char * error_msg,
59  size_t invalid_index);
60 
64  const size_t invalid_index;
65 };
66 
69 {
70 public:
71  InvalidNodeNameError(const char * node_name, const char * error_msg, size_t invalid_index)
72  : NameValidationError("node name", node_name, error_msg, invalid_index)
73  {}
74 };
75 
78 {
79 public:
80  InvalidNamespaceError(const char * namespace_, const char * error_msg, size_t invalid_index)
81  : NameValidationError("namespace", namespace_, error_msg, invalid_index)
82  {}
83 };
84 
87 {
88 public:
89  InvalidTopicNameError(const char * namespace_, const char * error_msg, size_t invalid_index)
90  : NameValidationError("topic name", namespace_, error_msg, invalid_index)
91  {}
92 };
93 
96 {
97 public:
98  InvalidServiceNameError(const char * namespace_, const char * error_msg, size_t invalid_index)
99  : NameValidationError("service name", namespace_, error_msg, invalid_index)
100  {}
101 };
102 
104 
116 /* *INDENT-OFF* */ // Uncrustify cannot yet understand [[noreturn]] properly
118 void
119 throw_from_rcl_error [[noreturn]] (
120  rcl_ret_t ret,
121  const std::string & prefix = "",
122  const rcl_error_state_t * error_state = nullptr,
123  void (* reset_error)() = rcl_reset_error);
124 /* *INDENT-ON* */
125 
127 {
128 public:
130  RCLErrorBase(rcl_ret_t ret, const rcl_error_state_t * error_state);
131  virtual ~RCLErrorBase() {}
132 
136  size_t line;
138 };
139 
142 {
143 public:
145  RCLError(rcl_ret_t ret, const rcl_error_state_t * error_state, const std::string & prefix);
147  RCLError(const RCLErrorBase & base_exc, const std::string & prefix);
148 };
149 
152 {
153 public:
155  RCLBadAlloc(rcl_ret_t ret, const rcl_error_state_t * error_state);
157  explicit RCLBadAlloc(const RCLErrorBase & base_exc);
158 };
159 
162 {
163 public:
166  rcl_ret_t ret,
167  const rcl_error_state_t * error_state,
168  const std::string & prefix);
170  RCLInvalidArgument(const RCLErrorBase & base_exc, const std::string & prefix);
171 };
172 
175 {
176 public:
179  rcl_ret_t ret, const rcl_error_state_t * error_state, const std::string & prefix);
181  RCLInvalidROSArgsError(const RCLErrorBase & base_exc, const std::string & prefix);
182 };
183 
186 {
187 public:
188  explicit UnknownROSArgsError(std::vector<std::string> && unknown_ros_args_in)
189  : std::runtime_error(
190  "found unknown ROS arguments: '" + rcpputils::join(unknown_ros_args_in, "', '") + "'"),
191  unknown_ros_args(unknown_ros_args_in)
192  {
193  }
194 
196 };
197 
200 {
201 public:
203  : std::runtime_error("event is invalid") {}
204 };
205 
208 {
209 public:
211  : std::runtime_error("event already registered") {}
212 };
213 
216 {
217 public:
218  // Inherit constructors from runtime_error.
220 };
221 
224 {
225  // Inherit constructors from runtime_error.
227 };
228 
231 {
232  // Inherit constructors from runtime_error.
234 };
235 
238 {
239  // Inherit constructors from runtime_error.
241 };
242 
245 {
246  // Inherit constructors from runtime_error.
248 };
249 
252 {
253  // Inherit constructors from runtime_error.
255 };
256 
257 } // namespace exceptions
258 } // namespace rclcpp
259 
260 #endif // RCLCPP__EXCEPTIONS_HPP_
Created when the return code does not match one of the other specialized exceptions.
Definition: exceptions.hpp:141
Created when the ret is RCL_RET_INVALID_ARGUMENT.
Definition: exceptions.hpp:161
InvalidNodeError()
Definition: exceptions.hpp:37
Thrown when a any kind of name (node, namespace, topic, etc.) is invalid.
Definition: exceptions.hpp:42
#define rcl_reset_error
rmw_ret_t rcl_ret_t
Thrown if parameter is already declared.
Definition: exceptions.hpp:230
Thrown if passed parameters are inconsistent or invalid.
Definition: exceptions.hpp:215
Thrown when a topic name is invalid.
Definition: exceptions.hpp:86
EventNotRegisteredError()
Definition: exceptions.hpp:210
Thrown when an unregistered rclcpp::Event is encountered where a registered one was expected...
Definition: exceptions.hpp:207
Thrown when an invalid rclcpp::Event object or SharedPtr is encountered.
Definition: exceptions.hpp:199
Thrown when a service name is invalid.
Definition: exceptions.hpp:95
InvalidEventError()
Definition: exceptions.hpp:202
This header provides the get_node_base_interface() template function.
Definition: allocator_common.hpp:24
std::string file
Definition: exceptions.hpp:135
InvalidNodeNameError(const char *node_name, const char *error_msg, size_t invalid_index)
Definition: exceptions.hpp:71
Created when the ret is RCL_RET_INVALID_ROS_ARGS.
Definition: exceptions.hpp:174
Thrown when a method is trying to use a node, but it is invalid.
Definition: exceptions.hpp:34
rcl_ret_t ret
Definition: exceptions.hpp:133
void throw_from_rcl_error(rcl_ret_t ret, const std::string &prefix="", const rcl_error_state_t *error_state=nullptr, void(*reset_error)()=rcl_reset_error)
Throw a C++ std::exception which was created based on an rcl error.
InvalidNamespaceError(const char *namespace_, const char *error_msg, size_t invalid_index)
Definition: exceptions.hpp:80
Definition: exceptions.hpp:126
const std::string name_type
Definition: exceptions.hpp:61
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
UnknownROSArgsError(std::vector< std::string > &&unknown_ros_args_in)
Definition: exceptions.hpp:188
const size_t invalid_index
Definition: exceptions.hpp:64
Thrown if parameter is not declared, e.g. either set or get was called without first declaring...
Definition: exceptions.hpp:237
const std::string error_msg
Definition: exceptions.hpp:63
InvalidTopicNameError(const char *namespace_, const char *error_msg, size_t invalid_index)
Definition: exceptions.hpp:89
Thrown when unparsed ROS specific arguments are found.
Definition: exceptions.hpp:185
std::string message
Definition: exceptions.hpp:134
const std::vector< std::string > unknown_ros_args
Definition: exceptions.hpp:195
Created when the ret is RCL_RET_BAD_ALLOC.
Definition: exceptions.hpp:151
Thrown when a node namespace is invalid.
Definition: exceptions.hpp:77
Thrown if parameter is immutable and therefore cannot be undeclared.
Definition: exceptions.hpp:244
NameValidationError(const char *name_type_, const char *name_, const char *error_msg_, size_t invalid_index_)
Definition: exceptions.hpp:45
InvalidServiceNameError(const char *namespace_, const char *error_msg, size_t invalid_index)
Definition: exceptions.hpp:98
size_t line
Definition: exceptions.hpp:136
Thrown if parameter is modified while in a set callback.
Definition: exceptions.hpp:251
std::string formatted_message
Definition: exceptions.hpp:137
const std::string name
Definition: exceptions.hpp:62
virtual ~RCLErrorBase()
Definition: exceptions.hpp:131
Thrown if passed parameter value is invalid.
Definition: exceptions.hpp:223
Thrown when a node name is invalid.
Definition: exceptions.hpp:68