Skip to content

Commit

Permalink
Better error handling.
Browse files Browse the repository at this point in the history
Signed-off-by: Jaison Titus <[email protected]>
  • Loading branch information
jaisontj committed Nov 27, 2019
1 parent fea85a6 commit 1a4d9fa
Showing 1 changed file with 12 additions and 31 deletions.
43 changes: 12 additions & 31 deletions rmw_fastrtps_shared_cpp/src/rmw_get_topic_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,14 @@ _get_topic_fqdns(const char * topic_name, bool no_mangle)
return topic_fqdns;
}

typedef rmw_ret_t (* rmw_topic_info_fini_func_t)(
rmw_topic_info_t * topic_info,
rcutils_allocator_t * allocator);

void
_handle_topic_info_member_fini(
_handle_topic_info_fini(
rmw_topic_info_t * topic_info,
std::string err_msg_title,
rcutils_allocator_t * allocator,
rmw_topic_info_fini_func_t topic_info_fini_member_func)
rcutils_allocator_t * allocator)
{
std::string error_string = err_msg_title + "\n" + rmw_get_error_string().str;
std::string error_string = rmw_get_error_string().str;
rmw_reset_error();
rmw_ret_t ret = topic_info_fini_member_func(topic_info, allocator);
rmw_ret_t ret = rmw_topic_info_fini(topic_info, allocator);
if (ret != RMW_RET_OK) {
error_string += rmw_get_error_string().str;
rmw_reset_error();
Expand Down Expand Up @@ -145,15 +139,10 @@ _set_rmw_topic_info(
if (topic_data.participant_guid == participant_guid) {
ret = rmw_topic_info_set_node_name(topic_info, node_name, allocator);
if (ret != RMW_RET_OK) {
_handle_topic_info_member_fini(topic_info, "Failed to set node_name", allocator,
rmw_topic_info_fini_topic_type);
return ret;
}
ret = rmw_topic_info_set_node_namespace(topic_info, node_namespace, allocator);
if (ret != RMW_RET_OK) {
_handle_topic_info_member_fini(topic_info, "Failed to set node_namespace", allocator,
rmw_topic_info_fini_node_name);
_handle_topic_info_member_fini(topic_info, "", allocator, rmw_topic_info_fini_topic_type);
return ret;
}
return ret;
Expand All @@ -169,8 +158,6 @@ _set_rmw_topic_info(
ret = rmw_topic_info_set_node_name(topic_info, "_NODE_NAME_UNKNOWN_", allocator);
}
if (ret != RMW_RET_OK) {
_handle_topic_info_member_fini(topic_info, "Failed to set node_name", allocator,
rmw_topic_info_fini_topic_type);
return ret;
}
// set node namespace
Expand All @@ -181,11 +168,6 @@ _set_rmw_topic_info(
} else {
ret = rmw_topic_info_set_node_namespace(topic_info, "_NODE_NAMESPACE_UNKNOWN_", allocator);
}
if (ret != RMW_RET_OK) {
_handle_topic_info_member_fini(topic_info, "Failed to set node_namespace", allocator,
rmw_topic_info_fini_node_name);
_handle_topic_info_member_fini(topic_info, "", allocator, rmw_topic_info_fini_topic_type);
}
return ret;
}

Expand Down Expand Up @@ -227,7 +209,7 @@ _get_info_by_topic(
const auto & it = topic_name_to_data.find(topic_name);
if (it != topic_name_to_data.end()) {
for (const auto & data : it->second) {
rmw_topic_info_t topic_info;
rmw_topic_info_t topic_info = rmw_get_zero_initialized_topic_info();
rmw_ret_t ret = _set_rmw_topic_info(
&topic_info,
participant_guid,
Expand All @@ -237,17 +219,12 @@ _get_info_by_topic(
slave_target,
allocator);
if (ret != RMW_RET_OK) {
// Free topic_info
_handle_topic_info_fini(&topic_info, allocator);
// Free all the space allocated to the previous topic_infos
for (auto & tinfo : topic_info_vector) {
_handle_topic_info_member_fini(&tinfo, "", allocator,
rmw_topic_info_fini_node_namespace);
_handle_topic_info_member_fini(&tinfo, "", allocator, rmw_topic_info_fini_node_name);
_handle_topic_info_member_fini(&tinfo, "", allocator, rmw_topic_info_fini_topic_type);
_handle_topic_info_fini(&tinfo, allocator);
}
std::string error_msg = "Failed to create set_rmw_topic_info: ";
error_msg += rmw_get_error_string().str;
rmw_reset_error();
RMW_SET_ERROR_MSG(error_msg.c_str());
return ret;
}
// add rmw_topic_info_t to a vector
Expand All @@ -264,6 +241,10 @@ _get_info_by_topic(
rmw_reset_error();
RMW_SET_ERROR_MSG_WITH_FORMAT_STRING(
"rmw_topic_info_array_init_with_size failed to allocate memory: %s", error_message.str);
// Free all the space allocated to the previous topic_infos
for (auto & tinfo : topic_info_vector) {
_handle_topic_info_fini(&tinfo, allocator);
}
return RMW_RET_BAD_ALLOC;
}
for (auto i = 0u; i < count; i++) {
Expand Down

0 comments on commit 1a4d9fa

Please sign in to comment.