ROS 2 rclcpp + rcl - rolling  rolling-a919a6e5
ROS 2 C++ Client Library with ROS Client Library
exceptions.hpp
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__EXCEPTIONS_HPP_
16 #define RCLCPP__EXCEPTIONS__EXCEPTIONS_HPP_
17 
18 #include <stdexcept>
19 #include <string>
20 #include <vector>
21 
22 #include "rcl/error_handling.h"
23 #include "rcl/types.h"
24 #include "rclcpp/visibility_control.hpp"
25 
26 #include "rcpputils/join.hpp"
27 
28 namespace rclcpp
29 {
30 namespace exceptions
31 {
32 
34 class InvalidNodeError : public std::runtime_error
35 {
36 public:
38  : std::runtime_error("node is invalid") {}
39 };
40 
42 class NameValidationError : public std::invalid_argument
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 
61  const std::string name_type;
62  const std::string name;
63  const std::string error_msg;
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 
103 class InvalidServiceTypeError : public std::runtime_error
104 {
105 public:
107  : std::runtime_error("Service type is invalid.") {}
108  explicit InvalidServiceTypeError(const std::string & msg)
109  : std::runtime_error(msg) {}
110 };
111 
112 class UnimplementedError : public std::runtime_error
113 {
114 public:
116  : std::runtime_error("This code is unimplemented.") {}
117  explicit UnimplementedError(const std::string & msg)
118  : std::runtime_error(msg) {}
119 };
120 
121 typedef void (* reset_error_function_t)();
122 
124 
136 /* *INDENT-OFF* */ // Uncrustify cannot yet understand [[noreturn]] properly
137 RCLCPP_PUBLIC
138 void
139 throw_from_rcl_error [[noreturn]] (
140  rcl_ret_t ret,
141  const std::string & prefix = "",
142  const rcl_error_state_t * error_state = nullptr,
143  reset_error_function_t reset_error = rcl_reset_error);
144 /* *INDENT-ON* */
145 
147 {
148 public:
149  RCLCPP_PUBLIC
150  RCLErrorBase(rcl_ret_t ret, const rcl_error_state_t * error_state);
151  virtual ~RCLErrorBase() {}
152 
153  rcl_ret_t ret;
154  std::string message;
155  std::string file;
156  size_t line;
157  std::string formatted_message;
158 };
159 
161 class RCLError : public RCLErrorBase, public std::runtime_error
162 {
163 public:
164  RCLCPP_PUBLIC
165  RCLError(rcl_ret_t ret, const rcl_error_state_t * error_state, const std::string & prefix);
166  RCLCPP_PUBLIC
167  RCLError(const RCLErrorBase & base_exc, const std::string & prefix);
168 };
169 
171 class RCLBadAlloc : public RCLErrorBase, public std::bad_alloc
172 {
173 public:
174  RCLCPP_PUBLIC
175  RCLBadAlloc(rcl_ret_t ret, const rcl_error_state_t * error_state);
176  RCLCPP_PUBLIC
177  explicit RCLBadAlloc(const RCLErrorBase & base_exc);
178 };
179 
181 class RCLInvalidArgument : public RCLErrorBase, public std::invalid_argument
182 {
183 public:
184  RCLCPP_PUBLIC
186  rcl_ret_t ret,
187  const rcl_error_state_t * error_state,
188  const std::string & prefix);
189  RCLCPP_PUBLIC
190  RCLInvalidArgument(const RCLErrorBase & base_exc, const std::string & prefix);
191 };
192 
194 class RCLInvalidROSArgsError : public RCLErrorBase, public std::runtime_error
195 {
196 public:
197  RCLCPP_PUBLIC
199  rcl_ret_t ret, const rcl_error_state_t * error_state, const std::string & prefix);
200  RCLCPP_PUBLIC
201  RCLInvalidROSArgsError(const RCLErrorBase & base_exc, const std::string & prefix);
202 };
203 
205 class UnknownROSArgsError : public std::runtime_error
206 {
207 public:
208  explicit UnknownROSArgsError(std::vector<std::string> && unknown_ros_args_in)
209  : std::runtime_error(
210  "found unknown ROS arguments: '" + rcpputils::join(unknown_ros_args_in, "', '") + "'"),
211  unknown_ros_args(unknown_ros_args_in)
212  {
213  }
214 
215  const std::vector<std::string> unknown_ros_args;
216 };
217 
219 class UnknownTypeError : public std::runtime_error
220 {
221 public:
222  explicit UnknownTypeError(const std::string & type)
223  : std::runtime_error("Unknown type: " + type) {}
224 };
225 
227 class InvalidEventError : public std::runtime_error
228 {
229 public:
231  : std::runtime_error("event is invalid") {}
232 };
233 
235 class EventNotRegisteredError : public std::runtime_error
236 {
237 public:
239  : std::runtime_error("event already registered") {}
240 };
241 
243 class MissingGroupNodeException : public std::runtime_error
244 {
245 public:
246  explicit MissingGroupNodeException(const std::string & obj_type)
247  : std::runtime_error("cannot create: " + obj_type + " , callback group not in node") {}
248 };
249 
251 class InvalidParametersException : public std::runtime_error
252 {
253 public:
254  // Inherit constructors from runtime_error.
255  using std::runtime_error::runtime_error;
256 };
257 
259 class InvalidParameterValueException : public std::runtime_error
260 {
261  // Inherit constructors from runtime_error.
262  using std::runtime_error::runtime_error;
263 };
264 
266 
270 class InvalidParameterTypeException : public std::runtime_error
271 {
272 public:
274 
278  RCLCPP_PUBLIC
279  InvalidParameterTypeException(const std::string & name, const std::string message)
280  : std::runtime_error("parameter '" + name + "' has invalid type: " + message)
281  {}
282 };
283 
285 
288 class UninitializedStaticallyTypedParameterException : public std::runtime_error
289 {
290 public:
292 
295  RCLCPP_PUBLIC
296  explicit UninitializedStaticallyTypedParameterException(const std::string & name)
297  : std::runtime_error("Statically typed parameter '" + name + "' must be initialized.")
298  {}
299 };
300 
302 class ParameterAlreadyDeclaredException : public std::runtime_error
303 {
304  // Inherit constructors from runtime_error.
305  using std::runtime_error::runtime_error;
306 };
307 
309 class ParameterNotDeclaredException : public std::runtime_error
310 {
311  // Inherit constructors from runtime_error.
312  using std::runtime_error::runtime_error;
313 };
314 
316 class ParameterImmutableException : public std::runtime_error
317 {
318  // Inherit constructors from runtime_error.
319  using std::runtime_error::runtime_error;
320 };
321 
323 class ParameterModifiedInCallbackException : public std::runtime_error
324 {
325  // Inherit constructors from runtime_error.
326  using std::runtime_error::runtime_error;
327 };
328 
330 class ParameterUninitializedException : public std::runtime_error
331 {
332 public:
334 
337  explicit ParameterUninitializedException(const std::string & name)
338  : std::runtime_error("parameter '" + name + "' is not initialized")
339  {}
340 };
341 
343 class InvalidQosOverridesException : public std::runtime_error
344 {
345  // Inherit constructors from runtime_error.
346  using std::runtime_error::runtime_error;
347 };
348 
350 class QoSCheckCompatibleException : public std::runtime_error
351 {
352  // Inherit constructors from runtime_error.
353  using std::runtime_error::runtime_error;
354 };
355 
356 } // namespace exceptions
357 } // namespace rclcpp
358 
359 #endif // RCLCPP__EXCEPTIONS__EXCEPTIONS_HPP_
Thrown when an unregistered rclcpp::Event is encountered where a registered one was expected.
Definition: exceptions.hpp:236
Thrown when an invalid rclcpp::Event object or SharedPtr is encountered.
Definition: exceptions.hpp:228
Thrown when a node namespace is invalid.
Definition: exceptions.hpp:78
Thrown when a method is trying to use a node, but it is invalid.
Definition: exceptions.hpp:35
Thrown when a node name is invalid.
Definition: exceptions.hpp:69
Thrown if requested parameter type is invalid.
Definition: exceptions.hpp:271
RCLCPP_PUBLIC InvalidParameterTypeException(const std::string &name, const std::string message)
Construct an instance.
Definition: exceptions.hpp:279
Thrown if passed parameter value is invalid.
Definition: exceptions.hpp:260
Thrown if passed parameters are inconsistent or invalid.
Definition: exceptions.hpp:252
Thrown if the QoS overrides provided aren't valid.
Definition: exceptions.hpp:344
Thrown when a service name is invalid.
Definition: exceptions.hpp:96
Thrown when a topic name is invalid.
Definition: exceptions.hpp:87
Thrown when a callback group is missing from the node, when it wants to utilize the group.
Definition: exceptions.hpp:244
Thrown when a any kind of name (node, namespace, topic, etc.) is invalid.
Definition: exceptions.hpp:43
Thrown if parameter is already declared.
Definition: exceptions.hpp:303
Thrown if parameter is immutable and therefore cannot be undeclared.
Definition: exceptions.hpp:317
Thrown if parameter is modified while in a set callback.
Definition: exceptions.hpp:324
Thrown if parameter is not declared, e.g. either set or get was called without first declaring.
Definition: exceptions.hpp:310
Thrown when an uninitialized parameter is accessed.
Definition: exceptions.hpp:331
ParameterUninitializedException(const std::string &name)
Construct an instance.
Definition: exceptions.hpp:337
Thrown if a QoS compatibility check fails.
Definition: exceptions.hpp:351
Created when the ret is RCL_RET_BAD_ALLOC.
Definition: exceptions.hpp:172
Created when the return code does not match one of the other specialized exceptions.
Definition: exceptions.hpp:162
Created when the ret is RCL_RET_INVALID_ARGUMENT.
Definition: exceptions.hpp:182
Created when the ret is RCL_RET_INVALID_ROS_ARGS.
Definition: exceptions.hpp:195
Thrown if user attempts to create an uninitialized statically typed parameter.
Definition: exceptions.hpp:289
RCLCPP_PUBLIC UninitializedStaticallyTypedParameterException(const std::string &name)
Construct an instance.
Definition: exceptions.hpp:296
Thrown when unparsed ROS specific arguments are found.
Definition: exceptions.hpp:206
Thrown when an unknown type is passed.
Definition: exceptions.hpp:220
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.
Definition: types.h:24