Skip to content

Commit

Permalink
handle invalid conversion of non-arithmetic types via custom exception
Browse files Browse the repository at this point in the history
  • Loading branch information
christianrauch committed Jun 12, 2024
1 parent 598a7e9 commit 121fd17
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/CameraNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,16 @@ CameraNode::declareParameters()
throw std::runtime_error("minimum and maximum parameter array sizes do not match");

// clamp default ControlValue to min/max range and cast ParameterValue
const rclcpp::ParameterValue value =
cv_to_pv(clamp(info.def(), info.min(), info.max()));
rclcpp::ParameterValue value;
try {
value = cv_to_pv(clamp(info.def(), info.min(), info.max()));
}
catch (const invalid_conversion &e) {
RCLCPP_ERROR_STREAM(get_logger(), "unsupported control '" << id->name() << "' (type: "
<< std::to_string(info.def().type())
<< "): " << e.what());
continue;
}

// get smallest bounds for minimum and maximum set
rcl_interfaces::msg::IntegerRange range_int;
Expand Down
2 changes: 1 addition & 1 deletion src/cv_to_pv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ template<typename T,
rclcpp::ParameterValue
cv_to_pv_array(const std::vector<T> & /*values*/)
{
throw std::runtime_error("ParameterValue only supported for arithmetic types");
throw invalid_conversion("ParameterValue only supported for arithmetic types");
}

template<typename T,
Expand Down
7 changes: 7 additions & 0 deletions src/cv_to_pv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
#include <rclcpp/parameter_value.hpp>


class invalid_conversion : public std::runtime_error
{
public:
explicit invalid_conversion(const std::string &msg) : std::runtime_error(msg) {};
};


rclcpp::ParameterValue
cv_to_pv(const libcamera::ControlValue &value);

Expand Down

0 comments on commit 121fd17

Please sign in to comment.