Skip to content
28 changes: 21 additions & 7 deletions rclcpp/src/rclcpp/exceptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ NameValidationError::format_error(
return msg;
}

void
throw_from_rcl_error(
std::exception_ptr
from_rcl_error(
rcl_ret_t ret,
const std::string & prefix,
const rcl_error_state_t * error_state,
Expand All @@ -55,24 +55,38 @@ throw_from_rcl_error(
if (!error_state) {
throw std::runtime_error("rcl error state is not set");
}
std::string formated_prefix = prefix;
std::string formatted_prefix = prefix;
if (!prefix.empty()) {
formated_prefix += ": ";
formatted_prefix += ": ";
}
RCLErrorBase base_exc(ret, error_state);
if (reset_error) {
reset_error();
}
switch (ret) {
case RCL_RET_BAD_ALLOC:
throw RCLBadAlloc(base_exc);
return std::make_exception_ptr(RCLBadAlloc(base_exc));
case RCL_RET_INVALID_ARGUMENT:
throw RCLInvalidArgument(base_exc, formated_prefix);
return std::make_exception_ptr(RCLInvalidArgument(base_exc, formatted_prefix));
default:
throw RCLError(base_exc, formated_prefix);
return std::make_exception_ptr(RCLError(base_exc, formatted_prefix));
}
}

void
throw_from_rcl_error(
rcl_ret_t ret,
const std::string & prefix,
const rcl_error_state_t * error_state,
void (* reset_error)())
{
// We expect this to either throw a standard error,
// or to generate an error pointer (which is caught
// in err, and immediately thrown)
auto err = from_rcl_error(ret, prefix, error_state, reset_error);
std::rethrow_exception(err);
}

RCLErrorBase::RCLErrorBase(rcl_ret_t ret, const rcl_error_state_t * error_state)
: ret(ret), message(error_state->message), file(error_state->file), line(error_state->line_number),
formatted_message(rcl_get_error_string().str)
Expand Down