Skip to content

Commit

Permalink
Use InvalidServiceTypeError for unavailable service type in GenericCl…
Browse files Browse the repository at this point in the history
…ient (#2629)

Signed-off-by: Barry Xu <[email protected]>
  • Loading branch information
Barry-Xu-2018 authored Sep 16, 2024
1 parent 97c386c commit 918363d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
33 changes: 21 additions & 12 deletions rclcpp/src/rclcpp/generic_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,31 @@ GenericClient::GenericClient(
rcl_client_options_t & client_options)
: ClientBase(node_base, node_graph)
{
ts_lib_ = get_typesupport_library(
service_type, "rosidl_typesupport_cpp");

auto service_ts_ = get_service_typesupport_handle(
service_type, "rosidl_typesupport_cpp", *ts_lib_);

auto response_type_support_intro = get_message_typesupport_handle(
service_ts_->response_typesupport,
rosidl_typesupport_introspection_cpp::typesupport_identifier);
response_members_ = static_cast<const rosidl_typesupport_introspection_cpp::MessageMembers *>(
response_type_support_intro->data);
const rosidl_service_type_support_t * service_ts;
try {
ts_lib_ = get_typesupport_library(
service_type, "rosidl_typesupport_cpp");

service_ts = get_service_typesupport_handle(
service_type, "rosidl_typesupport_cpp", *ts_lib_);

auto response_type_support_intro = get_message_typesupport_handle(
service_ts->response_typesupport,
rosidl_typesupport_introspection_cpp::typesupport_identifier);
response_members_ = static_cast<const rosidl_typesupport_introspection_cpp::MessageMembers *>(
response_type_support_intro->data);
} catch (std::runtime_error & err) {
RCLCPP_ERROR(
rclcpp::get_node_logger(node_handle_.get()).get_child("rclcpp"),
"Invalid service type: %s",
err.what());
throw rclcpp::exceptions::InvalidServiceTypeError(err.what());
}

rcl_ret_t ret = rcl_client_init(
this->get_client_handle().get(),
this->get_rcl_node_handle(),
service_ts_,
service_ts,
service_name.c_str(),
&client_options);
if (ret != RCL_RET_OK) {
Expand Down
2 changes: 1 addition & 1 deletion rclcpp/test/rclcpp/test_generic_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ TEST_F(TestGenericClient, construction_and_destruction) {
ASSERT_THROW(
{
auto client = node->create_generic_client("test_service", "test_msgs/srv/InvalidType");
}, std::runtime_error);
}, rclcpp::exceptions::InvalidServiceTypeError);
}
}

Expand Down

0 comments on commit 918363d

Please sign in to comment.