Skip to content

Commit c63d806

Browse files
ivanpaunojacobperron
authored andcommitted
Add convenient error handling macros (osrf#30)
Add macros to handle java exceptions Add macros to throw a java exception from an rcl error Always use `base_message` instead of `message` in docblocks Signed-off-by: Ivan Santiago Paunovic <ivanpauno@ekumenlabs.com>
1 parent ae53ab7 commit c63d806

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

rcljava_common/include/rcljava_common/exceptions.hpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,57 @@
1919

2020
#include "rcljava_common/visibility_control.hpp"
2121

22+
/// Execute \a error_statement if an exception has happened.
23+
/**
24+
* \param env a JNIEnv pointer, used to check for exceptions.
25+
* \param error_statement statement executed if an exception has happened.
26+
*/
27+
#define RCLJAVA_COMMON_CHECK_FOR_EXCEPTION_WITH_ERROR_STATEMENT(env, error_statement) \
28+
do { \
29+
if (env->ExceptionCheck()) { \
30+
error_statement; \
31+
} \
32+
} while (0)
33+
34+
/// Return from the current function if a java exception has happened.
35+
/**
36+
* \param env a JNIEnv pointer, used to check for exceptions.
37+
*/
38+
#define RCLJAVA_COMMON_CHECK_FOR_EXCEPTION(env) \
39+
RCLJAVA_COMMON_CHECK_FOR_EXCEPTION_WITH_ERROR_STATEMENT(env, return )
40+
41+
/// Call \ref rcljava_throw_rclexception if \a ret is not RCL_RET_OK,
42+
/// and execute \a error_statement in that case.
43+
/**
44+
* The rcl error message will be added to \a base_message, and the rcl error state will be reset.
45+
*
46+
* \param env a JNIEnv pointer, used to throw a java exception from the rcl error.
47+
* \param ret rcl_ret_t error that will be checked.
48+
* \param base_message error message that will be passed to the thrown exception.
49+
* The complete error will be "${base_message}: ${rcl_error_string}".
50+
* \a base_message can be either a `const char *` or as `std::string`.
51+
* \param error_statement statement executed if ret was not RCL_RET_OK.
52+
*/
53+
#define RCLJAVA_COMMON_THROW_FROM_RCL_WITH_ERROR_STATEMENT( \
54+
env, ret, base_message, error_statement) \
55+
do { \
56+
if (RCL_RET_OK != ret) { \
57+
rcljava_common::exceptions::rcljava_throw_rclexception( \
58+
env, ret, static_cast<std::string>(base_message) + ": " + rcl_get_error_string().str); \
59+
rcl_reset_error(); \
60+
error_statement; \
61+
} \
62+
} while (0)
63+
64+
/// Call \ref rcljava_throw_rclexception if \a ret is not RCL_RET_OK and return.
65+
/**
66+
* \param env a JNIEnv pointer, used to check for exceptions.
67+
* \param ret rcl_ret_t error that will be checked.
68+
* \param base_message error message that will be passed to the thrown exception.
69+
*/
70+
#define RCLJAVA_COMMON_THROW_FROM_RCL(env, ret, base_message) \
71+
RCLJAVA_COMMON_THROW_FROM_RCL_WITH_ERROR_STATEMENT(env, ret, base_message, return )
72+
2273
namespace rcljava_common
2374
{
2475
namespace exceptions

0 commit comments

Comments
 (0)