From 713dd0c18411623ea82af1255f583a00c3c5970b Mon Sep 17 00:00:00 2001 From: jhdcs <48914066+jhdcs@users.noreply.github.com> Date: Tue, 16 Apr 2019 14:25:25 -0400 Subject: [PATCH] [WIP] Exception Generator function for implementing "from_rcl_error" (#678) * Created function to generate exception objects Signed-off-by: Jacob Hassold * Created function to generate exception objects Signed-off-by: Jacob Hassold * Fixed typo Signed-off-by: Jacob Hassold * Fixed typo Signed-off-by: Jacob Hassold * Throw exceptions not created by ret Signed-off-by: Jacob Hassold * Throw exceptions not created by ret Signed-off-by: Jacob Hassold * convert throw_from_rcl_error to use from_rcl_error Mostly just a convenience function Signed-off-by: Jacob Hassold * Updated .gitignore Please ignore Signed-off-by: Jacob Hassold * Re-ordered functions to allow compilation Signed-off-by: Jacob Hassold * Revert "Updated .gitignore" This reverts commit bee0ee13ce687bc56bdc7ad1e8382506d9aef428. Signed-off-by: Jacob Hassold * restore .gitignore to original state Signed-off-by: Shane Loretz * oops, actually restore .gitignore Signed-off-by: Shane Loretz --- rclcpp/src/rclcpp/exceptions.cpp | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/rclcpp/src/rclcpp/exceptions.cpp b/rclcpp/src/rclcpp/exceptions.cpp index 84469b5..6f5c40f 100644 --- a/rclcpp/src/rclcpp/exceptions.cpp +++ b/rclcpp/src/rclcpp/exceptions.cpp @@ -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, @@ -55,9 +55,9 @@ 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) { @@ -65,14 +65,28 @@ throw_from_rcl_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)