Skip to content

Commit

Permalink
Improved exceptions in type support loading.
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanFabian committed Oct 23, 2024
1 parent 01bcf56 commit 619f5af
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 29 deletions.
20 changes: 20 additions & 0 deletions ros_babel_fish/include/ros_babel_fish/idl/exceptions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// Created by stefan on 22.10.24.
//

#ifndef ROS_BABEL_FISH_IDL_EXCEPTIONS_HPP
#define ROS_BABEL_FISH_IDL_EXCEPTIONS_HPP

#include "ros_babel_fish/exceptions/babel_fish_exception.hpp"

namespace ros_babel_fish
{

class TypeSupportException : public BabelFishException
{
public:
using BabelFishException::BabelFishException;
};
} // namespace ros_babel_fish

#endif // ROS_BABEL_FISH_IDL_EXCEPTIONS_HPP
60 changes: 31 additions & 29 deletions ros_babel_fish/src/idl/providers/local_type_support_provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#include "ros_babel_fish/idl/providers/local_type_support_provider.hpp"
#include "ros_babel_fish/exceptions/babel_fish_exception.hpp"
#include "ros_babel_fish/idl/exceptions.hpp"

#include <ament_index_cpp/get_package_prefix.hpp>
#include <ament_index_cpp/get_resources.hpp>
#include <rcpputils/shared_library.hpp>
#include <rcutils/error_handling.h>
#include <rosidl_typesupport_c/identifier.h>
#include <rosidl_typesupport_cpp/identifier.hpp>
#include <rosidl_typesupport_cpp/service_type_support.hpp>
#include <rosidl_typesupport_introspection_cpp/identifier.hpp>
#include <rosidl_typesupport_introspection_cpp/service_introspection.hpp>

Expand Down Expand Up @@ -57,8 +54,8 @@ std::string get_typesupport_library_path( const std::string &package_name,
std::string package_prefix;
try {
package_prefix = ament_index_cpp::get_package_prefix( package_name );
} catch ( ament_index_cpp::PackageNotFoundError &e ) {
throw BabelFishException( e.what() );
} catch ( std::runtime_error &e ) {
throw TypeSupportException( e.what() );
}

auto library_path = package_prefix + dynamic_library_folder + filename_prefix + package_name +
Expand All @@ -73,12 +70,12 @@ std::tuple<std::string, std::string, std::string> extract_type_identifier( const
auto sep_position_front = full_type.find_first_of( type_separator );
if ( sep_position_back == std::string::npos || sep_position_back == 0 ||
sep_position_back == full_type.length() - 1 ) {
throw BabelFishException( "Message type '" + full_type +
"' is not of the form package/type and cannot be processed" );
throw TypeSupportException( "Message type '" + full_type +
"' is not of the form package/type and cannot be processed" );
}

std::string package_name = full_type.substr( 0, sep_position_front );
std::string middle_module = "";
std::string middle_module;
if ( sep_position_back - sep_position_front > 0 ) {
middle_module =
full_type.substr( sep_position_front + 1, sep_position_back - sep_position_front - 1 );
Expand All @@ -91,17 +88,22 @@ std::tuple<std::string, std::string, std::string> extract_type_identifier( const
std::shared_ptr<rcpputils::SharedLibrary>
get_typesupport_library( const std::string &type, const std::string &typesupport_identifier )
{
auto package_name = std::get<0>( extract_type_identifier( type ) );
auto library_path = get_typesupport_library_path( package_name, typesupport_identifier );
return std::make_shared<rcpputils::SharedLibrary>( library_path );
try {
auto package_name = std::get<0>( extract_type_identifier( type ) );
auto library_path = get_typesupport_library_path( package_name, typesupport_identifier );
return std::make_shared<rcpputils::SharedLibrary>( library_path );
} catch ( TypeSupportException &e ) {
throw TypeSupportException( "Failed to get typesupport library for message type '" + type +
"': " + e.what() );
}
}

const rosidl_message_type_support_t *
get_typesupport_handle( const std::string &type, const std::string &typesupport_identifier,
const std::shared_ptr<rcpputils::SharedLibrary> &library )
{
if ( nullptr == library ) {
throw BabelFishException(
throw TypeSupportException(
"rcpputils::SharedLibrary not initialized. Call get_typesupport_library first." );
}

Expand All @@ -122,24 +124,24 @@ get_typesupport_handle( const std::string &type, const std::string &typesupport_
package_name + "__" + middle_module + "__" + type_name;

if ( !library->has_symbol( symbol_name ) ) {
throw std::runtime_error(
throw TypeSupportException(
std::string(
" Could not find symbol for message type support handle getter. rcutils error: " ) +
rcutils_get_error_string().str +
". This is probably due to https://github.com/ros2/rosidl_typesupport/pull/114 not being "
"merged yet." );
}

const rosidl_message_type_support_t *( *get_ts )() = nullptr;
const rosidl_message_type_support_t *( *get_ts )();
get_ts = (decltype( get_ts ))library->get_symbol( symbol_name );

if ( !get_ts ) {
throw std::runtime_error{ " Symbol of wrong type." };
throw TypeSupportException{ " Symbol of wrong type." };
}
auto type_support = get_ts();
return type_support;
} catch ( std::runtime_error &e ) {
throw BabelFishException( rcutils_dynamic_loading_error.str() + e.what() );
} catch ( TypeSupportException &e ) {
throw TypeSupportException( rcutils_dynamic_loading_error.str() + e.what() );
}
}
// ==== End of block ===
Expand All @@ -149,7 +151,7 @@ get_service_typesupport_handle( const std::string &type, const std::string &type
const std::shared_ptr<rcpputils::SharedLibrary> &library )
{
if ( nullptr == library ) {
throw BabelFishException(
throw TypeSupportException(
"rcpputils::SharedLibrary not initialized. Call get_typesupport_library first." );
}

Expand All @@ -170,24 +172,24 @@ get_service_typesupport_handle( const std::string &type, const std::string &type
auto symbol_name = typesupport_identifier + "__get_service_type_support_handle__" + package_name +
"__" + ( middle_module.empty() ? "srv" : middle_module ) + "__" + type_name;
if ( !library->has_symbol( symbol_name ) ) {
throw std::runtime_error(
throw TypeSupportException(
std::string(
" Could not find symbol for message type support handle getter. rcutils error: " ) +
rcutils_get_error_string().str +
". This is probably due to https://github.com/ros2/rosidl_typesupport/pull/114 not being "
"merged yet." );
}

const rosidl_service_type_support_t *( *get_ts )() = nullptr;
const rosidl_service_type_support_t *( *get_ts )();
get_ts = (decltype( get_ts ))library->get_symbol( symbol_name );

if ( !get_ts ) {
throw std::runtime_error{ " Symbol of wrong type." };
throw TypeSupportException{ " Symbol of wrong type." };
}
auto type_support = get_ts();
return type_support;
} catch ( std::runtime_error &e ) {
throw BabelFishException( rcutils_dynamic_loading_error.str() + e.what() );
} catch ( TypeSupportException &e ) {
throw TypeSupportException( rcutils_dynamic_loading_error.str() + e.what() );
}
}

Expand All @@ -196,7 +198,7 @@ get_action_typesupport_handle( const std::string &type, const std::string &types
const std::shared_ptr<rcpputils::SharedLibrary> &library )
{
if ( nullptr == library ) {
throw std::runtime_error(
throw TypeSupportException(
"rcpputils::SharedLibrary not initialized. Call get_typesupport_library first." );
}

Expand All @@ -219,24 +221,24 @@ get_action_typesupport_handle( const std::string &type, const std::string &types
"__" + type_name;

if ( !library->has_symbol( symbol_name ) ) {
throw std::runtime_error(
throw TypeSupportException(
std::string(
" Could not find symbol for message type support handle getter. rcutils error: " ) +
rcutils_get_error_string().str +
". This is probably due to https://github.com/ros2/rosidl_typesupport/pull/114 not being "
"merged yet." );
}

const rosidl_action_type_support_t *( *get_ts )() = nullptr;
const rosidl_action_type_support_t *( *get_ts )();
get_ts = (decltype( get_ts ))library->get_symbol( symbol_name );

if ( !get_ts ) {
throw std::runtime_error{ " Symbol of wrong type." };
throw TypeSupportException{ " Symbol of wrong type." };
}
auto type_support = get_ts();
return type_support;
} catch ( std::runtime_error &e ) {
throw BabelFishException( rcutils_dynamic_loading_error.str() + e.what() );
throw TypeSupportException( rcutils_dynamic_loading_error.str() + e.what() );
}
}
} // namespace
Expand Down

0 comments on commit 619f5af

Please sign in to comment.