diff --git a/rclcpp/src/rclcpp/generic_client.cpp b/rclcpp/src/rclcpp/generic_client.cpp index fdcfc70aab..987975d803 100644 --- a/rclcpp/src/rclcpp/generic_client.cpp +++ b/rclcpp/src/rclcpp/generic_client.cpp @@ -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( - 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( + 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) { diff --git a/rclcpp/test/rclcpp/test_generic_client.cpp b/rclcpp/test/rclcpp/test_generic_client.cpp index be65ea1f53..496b21ab63 100644 --- a/rclcpp/test/rclcpp/test_generic_client.cpp +++ b/rclcpp/test/rclcpp/test_generic_client.cpp @@ -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); } }