Skip to content

Commit 121fd17

Browse files
handle invalid conversion of non-arithmetic types via custom exception
1 parent 598a7e9 commit 121fd17

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

src/CameraNode.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,16 @@ CameraNode::declareParameters()
459459
throw std::runtime_error("minimum and maximum parameter array sizes do not match");
460460

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

465473
// get smallest bounds for minimum and maximum set
466474
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<typename T,

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)