Skip to content

Commit 2c65b2c

Browse files
handle invalid conversion of non-arithmetic types via custom exception
1 parent aa175c8 commit 2c65b2c

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

src/CameraNode.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,17 @@ CameraNode::declareParameters()
462462
throw std::runtime_error("minimum and maximum parameter array sizes do not match");
463463

464464
// clamp default ControlValue to min/max range and cast ParameterValue
465-
const rclcpp::ParameterValue value = cv_to_pv(clamp(info.def(), info.min(), info.max()));
465+
rclcpp::ParameterValue value;
466+
try {
467+
value = cv_to_pv(clamp(info.def(), info.min(), info.max()));
468+
}
469+
catch (const invalid_conversion &e) {
470+
RCLCPP_ERROR_STREAM(get_logger(), "unsupported control '"
471+
<< id->name()
472+
<< "' (type: " << std::to_string(info.def().type()) << "): "
473+
<< e.what());
474+
continue;
475+
}
466476

467477
// get smallest bounds for minimum and maximum set
468478
rcl_interfaces::msg::IntegerRange range_int;

src/cv_to_pv.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ template<typename T,
4848
rclcpp::ParameterValue
4949
cv_to_pv_array(const std::vector<T> & /*values*/)
5050
{
51-
throw std::runtime_error("ParameterValue only supported for arithmetic types");
51+
throw invalid_conversion("ParameterValue only supported for arithmetic types");
5252
}
5353

5454
template<

src/cv_to_pv.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
#include <rclcpp/parameter_value.hpp>
55

66

7+
class invalid_conversion : public std::runtime_error
8+
{
9+
public:
10+
explicit invalid_conversion(const std::string &msg) : std::runtime_error(msg) {};
11+
};
12+
13+
714
rclcpp::ParameterValue
815
cv_to_pv(const libcamera::ControlValue &value);
916

0 commit comments

Comments
 (0)