From d174daef8208f65288fafaff102b6d488cfa5cf0 Mon Sep 17 00:00:00 2001 From: Ivan Santiago Paunovic Date: Thu, 3 Sep 2020 13:48:17 -0300 Subject: [PATCH 1/9] Add macros to handle java exceptions Signed-off-by: Ivan Santiago Paunovic --- .../include/rcljava_common/exceptions.hpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/rcljava_common/include/rcljava_common/exceptions.hpp b/rcljava_common/include/rcljava_common/exceptions.hpp index 8a148611..788d4ba3 100644 --- a/rcljava_common/include/rcljava_common/exceptions.hpp +++ b/rcljava_common/include/rcljava_common/exceptions.hpp @@ -19,6 +19,24 @@ #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_EXCEPTION_CHECK_X(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_EXCEPTION_CHECK(env) RCLJAVA_COMMON_EXCEPTION_CHECK_X(env, return ) + namespace rcljava_common { namespace exceptions From 8f99fb8785b785f2ba2154d0b0320d995e959d42 Mon Sep 17 00:00:00 2001 From: Ivan Santiago Paunovic Date: Thu, 3 Sep 2020 13:57:03 -0300 Subject: [PATCH 2/9] Add macros to throw a java exception from an rcl error Signed-off-by: Ivan Santiago Paunovic --- .../include/rcljava_common/exceptions.hpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/rcljava_common/include/rcljava_common/exceptions.hpp b/rcljava_common/include/rcljava_common/exceptions.hpp index 788d4ba3..2b7b15a4 100644 --- a/rcljava_common/include/rcljava_common/exceptions.hpp +++ b/rcljava_common/include/rcljava_common/exceptions.hpp @@ -37,6 +37,31 @@ */ #define RCLJAVA_COMMON_EXCEPTION_CHECK(env) RCLJAVA_COMMON_EXCEPTION_CHECK_X(env, return ) +/// Call \ref rcljava_throw_rclexception if \a ret is not RCL_RET_OK, +/// and execute \a error_statement in that case. +/** + * \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 message error message that will be passed to the thrown exception. + * \param error_statement statement executed if ret was not RCL_RET_OK. + */ +#define RCLJAVA_COMMON_THROW_FROM_RCL_X(env, ret, message, error_statement) \ + do { \ + if (RCL_RET_OK != ret) { \ + rcljava_common::exception::rcljava_throw_rclexception(env, ret, message); \ + 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 message error message that will be passed to the thrown exception. + */ +#define RCLJAVA_COMMON_THROW_FROM_RCL(env, ret, message) \ + RCLJAVA_COMMON_THROW_FROM_RCL_X(env, ret, message, return ) + namespace rcljava_common { namespace exceptions From ff655b137400246d73675828098fe6fb8b434011 Mon Sep 17 00:00:00 2001 From: Ivan Santiago Paunovic Date: Thu, 3 Sep 2020 14:52:27 -0300 Subject: [PATCH 3/9] Improve the macro Signed-off-by: Ivan Santiago Paunovic --- rcljava_common/include/rcljava_common/exceptions.hpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/rcljava_common/include/rcljava_common/exceptions.hpp b/rcljava_common/include/rcljava_common/exceptions.hpp index 2b7b15a4..307cc08a 100644 --- a/rcljava_common/include/rcljava_common/exceptions.hpp +++ b/rcljava_common/include/rcljava_common/exceptions.hpp @@ -40,15 +40,21 @@ /// 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 message error message that will be passed to the thrown exception. + * The complete error will be "${message}: ${rcl_error_string}". + * \a 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_X(env, ret, message, error_statement) \ +#define RCLJAVA_COMMON_THROW_FROM_RCL_X(env, ret, base_message, error_statement) \ do { \ if (RCL_RET_OK != ret) { \ - rcljava_common::exception::rcljava_throw_rclexception(env, ret, message); \ + rcljava_common::exception::rcljava_throw_rclexception( \ + env, ret, static_cast(message) + ": " + rcl_get_error_string().str); \ + rcl_reset_error(); error_statement; \ } \ } while (0) From df699c003c54fa6db1da41671796044289cffe6c Mon Sep 17 00:00:00 2001 From: Ivan Santiago Paunovic Date: Thu, 3 Sep 2020 15:02:47 -0300 Subject: [PATCH 4/9] fixup Signed-off-by: Ivan Santiago Paunovic --- rcljava_common/include/rcljava_common/exceptions.hpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/rcljava_common/include/rcljava_common/exceptions.hpp b/rcljava_common/include/rcljava_common/exceptions.hpp index 307cc08a..b61ba668 100644 --- a/rcljava_common/include/rcljava_common/exceptions.hpp +++ b/rcljava_common/include/rcljava_common/exceptions.hpp @@ -41,7 +41,7 @@ /// 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 message error message that will be passed to the thrown exception. @@ -52,9 +52,9 @@ #define RCLJAVA_COMMON_THROW_FROM_RCL_X(env, ret, base_message, error_statement) \ do { \ if (RCL_RET_OK != ret) { \ - rcljava_common::exception::rcljava_throw_rclexception( \ - env, ret, static_cast(message) + ": " + rcl_get_error_string().str); \ - rcl_reset_error(); + rcljava_common::exceptions::rcljava_throw_rclexception( \ + env, ret, static_cast(base_message) + ": " + rcl_get_error_string().str); \ + rcl_reset_error(); \ error_statement; \ } \ } while (0) @@ -65,8 +65,8 @@ * \param ret rcl_ret_t error that will be checked. * \param message error message that will be passed to the thrown exception. */ -#define RCLJAVA_COMMON_THROW_FROM_RCL(env, ret, message) \ - RCLJAVA_COMMON_THROW_FROM_RCL_X(env, ret, message, return ) +#define RCLJAVA_COMMON_THROW_FROM_RCL(env, ret, base_message) \ + RCLJAVA_COMMON_THROW_FROM_RCL_X(env, ret, base_message, return ) namespace rcljava_common { From 2eee0c1f2711142e73f431d407a727fb4ef3afe2 Mon Sep 17 00:00:00 2001 From: Ivan Santiago Paunovic Date: Tue, 8 Sep 2020 16:26:59 -0300 Subject: [PATCH 5/9] Always use `base_message` instead of `message` in docblocks Signed-off-by: Ivan Santiago Paunovic Co-authored-by: Jacob Perron --- rcljava_common/include/rcljava_common/exceptions.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rcljava_common/include/rcljava_common/exceptions.hpp b/rcljava_common/include/rcljava_common/exceptions.hpp index b61ba668..2d795dbc 100644 --- a/rcljava_common/include/rcljava_common/exceptions.hpp +++ b/rcljava_common/include/rcljava_common/exceptions.hpp @@ -44,9 +44,9 @@ * * \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 message error message that will be passed to the thrown exception. - * The complete error will be "${message}: ${rcl_error_string}". - * \a message can be either a `const char *` or as `std::string`. + * \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_X(env, ret, base_message, error_statement) \ From a9292674b8843f7393b4d67c3618e99fe7c8048a Mon Sep 17 00:00:00 2001 From: Ivan Santiago Paunovic Date: Tue, 8 Sep 2020 16:27:31 -0300 Subject: [PATCH 6/9] Always use `base_message` instead of `message` in docblocks Signed-off-by: Ivan Santiago Paunovic Co-authored-by: Jacob Perron --- rcljava_common/include/rcljava_common/exceptions.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rcljava_common/include/rcljava_common/exceptions.hpp b/rcljava_common/include/rcljava_common/exceptions.hpp index 2d795dbc..73d4c03b 100644 --- a/rcljava_common/include/rcljava_common/exceptions.hpp +++ b/rcljava_common/include/rcljava_common/exceptions.hpp @@ -63,7 +63,7 @@ /** * \param env a JNIEnv pointer, used to check for exceptions. * \param ret rcl_ret_t error that will be checked. - * \param message error message that will be passed to the thrown exception. + * \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_X(env, ret, base_message, return ) From e78003d7fd41b3512711daf444990352703d8eea Mon Sep 17 00:00:00 2001 From: Ivan Santiago Paunovic Date: Tue, 8 Sep 2020 16:41:41 -0300 Subject: [PATCH 7/9] rename macros Signed-off-by: Ivan Santiago Paunovic --- rcljava_common/include/rcljava_common/exceptions.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rcljava_common/include/rcljava_common/exceptions.hpp b/rcljava_common/include/rcljava_common/exceptions.hpp index b61ba668..8ff63abe 100644 --- a/rcljava_common/include/rcljava_common/exceptions.hpp +++ b/rcljava_common/include/rcljava_common/exceptions.hpp @@ -24,7 +24,7 @@ * \param env a JNIEnv pointer, used to check for exceptions. * \param error_statement statement executed if an exception has happened. */ -#define RCLJAVA_COMMON_EXCEPTION_CHECK_X(env, error_statement) \ +#define RCLJAVA_COMMON_CHECK_FOR_EXCEPTION_WITH_ERROR_STATEMENT(env, error_statement) \ do { \ if (env->ExceptionCheck()) { \ error_statement; \ @@ -35,7 +35,7 @@ /** * \param env a JNIEnv pointer, used to check for exceptions. */ -#define RCLJAVA_COMMON_EXCEPTION_CHECK(env) RCLJAVA_COMMON_EXCEPTION_CHECK_X(env, return ) +#define RCLJAVA_COMMON_CHECK_FOR_EXCEPTION(env) RCLJAVA_COMMON_CHECK_FOR_EXCEPTION_WITH_STATEMENT(env, return ) /// Call \ref rcljava_throw_rclexception if \a ret is not RCL_RET_OK, /// and execute \a error_statement in that case. @@ -49,7 +49,7 @@ * \a 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_X(env, ret, base_message, error_statement) \ +#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( \ @@ -66,7 +66,7 @@ * \param 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_X(env, ret, base_message, return ) + RCLJAVA_COMMON_THROW_FROM_RCL_WITH_ERROR_STATEMENT(env, ret, base_message, return ) namespace rcljava_common { From 54445853028106ad70706bdd362f1066134b5ec4 Mon Sep 17 00:00:00 2001 From: Ivan Santiago Paunovic Date: Tue, 8 Sep 2020 17:58:37 -0300 Subject: [PATCH 8/9] Please linters Signed-off-by: Ivan Santiago Paunovic --- rcljava_common/include/rcljava_common/exceptions.hpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/rcljava_common/include/rcljava_common/exceptions.hpp b/rcljava_common/include/rcljava_common/exceptions.hpp index 992a509d..11b426aa 100644 --- a/rcljava_common/include/rcljava_common/exceptions.hpp +++ b/rcljava_common/include/rcljava_common/exceptions.hpp @@ -35,7 +35,8 @@ /** * \param env a JNIEnv pointer, used to check for exceptions. */ -#define RCLJAVA_COMMON_CHECK_FOR_EXCEPTION(env) RCLJAVA_COMMON_CHECK_FOR_EXCEPTION_WITH_STATEMENT(env, return ) +#define RCLJAVA_COMMON_CHECK_FOR_EXCEPTION(env) \ + RCLJAVA_COMMON_CHECK_FOR_EXCEPTION_WITH_STATEMENT(env, return ) /// Call \ref rcljava_throw_rclexception if \a ret is not RCL_RET_OK, /// and execute \a error_statement in that case. @@ -49,7 +50,8 @@ * \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) \ +#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( \ From d7fbce044cdca99e0a4a0dd0954f4c4692ba5dc4 Mon Sep 17 00:00:00 2001 From: Ivan Santiago Paunovic Date: Tue, 8 Sep 2020 18:06:26 -0300 Subject: [PATCH 9/9] fix after using the macros Signed-off-by: Ivan Santiago Paunovic --- rcljava_common/include/rcljava_common/exceptions.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rcljava_common/include/rcljava_common/exceptions.hpp b/rcljava_common/include/rcljava_common/exceptions.hpp index 11b426aa..b7f2d3c5 100644 --- a/rcljava_common/include/rcljava_common/exceptions.hpp +++ b/rcljava_common/include/rcljava_common/exceptions.hpp @@ -36,7 +36,7 @@ * \param env a JNIEnv pointer, used to check for exceptions. */ #define RCLJAVA_COMMON_CHECK_FOR_EXCEPTION(env) \ - RCLJAVA_COMMON_CHECK_FOR_EXCEPTION_WITH_STATEMENT(env, return ) + 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.