diff --git a/rcljava_common/include/rcljava_common/exceptions.hpp b/rcljava_common/include/rcljava_common/exceptions.hpp index 8a148611..b7f2d3c5 100644 --- a/rcljava_common/include/rcljava_common/exceptions.hpp +++ b/rcljava_common/include/rcljava_common/exceptions.hpp @@ -19,6 +19,57 @@ #include "rcljava_common/visibility_control.hpp" +/// Execute \a error_statement if an exception has happened. +/** + * \param env a JNIEnv pointer, used to check for exceptions. + * \param error_statement statement executed if an exception has happened. + */ +#define RCLJAVA_COMMON_CHECK_FOR_EXCEPTION_WITH_ERROR_STATEMENT(env, error_statement) \ + do { \ + if (env->ExceptionCheck()) { \ + error_statement; \ + } \ + } while (0) + +/// Return from the current function if a java exception has happened. +/** + * \param env a JNIEnv pointer, used to check for exceptions. + */ +#define RCLJAVA_COMMON_CHECK_FOR_EXCEPTION(env) \ + RCLJAVA_COMMON_CHECK_FOR_EXCEPTION_WITH_ERROR_STATEMENT(env, return ) + +/// Call \ref rcljava_throw_rclexception if \a ret is not RCL_RET_OK, +/// and execute \a error_statement in that case. +/** + * The rcl error message will be added to \a base_message, and the rcl error state will be reset. + * + * \param env a JNIEnv pointer, used to throw a java exception from the rcl error. + * \param ret rcl_ret_t error that will be checked. + * \param base_message error message that will be passed to the thrown exception. + * The complete error will be "${base_message}: ${rcl_error_string}". + * \a base_message can be either a `const char *` or as `std::string`. + * \param error_statement statement executed if ret was not RCL_RET_OK. + */ +#define RCLJAVA_COMMON_THROW_FROM_RCL_WITH_ERROR_STATEMENT( \ + env, ret, base_message, error_statement) \ + do { \ + if (RCL_RET_OK != ret) { \ + rcljava_common::exceptions::rcljava_throw_rclexception( \ + env, ret, static_cast(base_message) + ": " + rcl_get_error_string().str); \ + rcl_reset_error(); \ + error_statement; \ + } \ + } while (0) + +/// Call \ref rcljava_throw_rclexception if \a ret is not RCL_RET_OK and return. +/** + * \param env a JNIEnv pointer, used to check for exceptions. + * \param ret rcl_ret_t error that will be checked. + * \param base_message error message that will be passed to the thrown exception. + */ +#define RCLJAVA_COMMON_THROW_FROM_RCL(env, ret, base_message) \ + RCLJAVA_COMMON_THROW_FROM_RCL_WITH_ERROR_STATEMENT(env, ret, base_message, return ) + namespace rcljava_common { namespace exceptions