Skip to content

Commit e5cce95

Browse files
authored
Add convenience error handling macros (#421)
* Add convenience error handling macros Signed-off-by: methylDragon <[email protected]> * Refine error handling macros Signed-off-by: methylDragon <[email protected]> * Remove conditional in error handling macros Signed-off-by: methylDragon <[email protected]> --------- Signed-off-by: methylDragon <[email protected]>
1 parent dbd3ff4 commit e5cce95

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

include/rcutils/error_handling.h

+69
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,75 @@ RCUTILS_PUBLIC
314314
void
315315
rcutils_reset_error(void);
316316

317+
/// Set the error message using RCUTILS_SET_ERROR_MSG and append the previous error.
318+
/**
319+
* If there is no previous error, has same behavior as RCUTILS_SET_ERROR_MSG.
320+
* \param[in] msg The error message to be set.
321+
*/
322+
#define RCUTILS_SET_ERROR_MSG_AND_APPEND_PREV_ERROR(msg) \
323+
do { \
324+
rcutils_error_string_t error_string = rcutils_get_error_string(); \
325+
rcutils_reset_error(); \
326+
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING( \
327+
RCUTILS_EXPAND(msg ": %s"), error_string.str); \
328+
} while (0)
329+
330+
/// Set the error message with RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING and append the previous
331+
/// error.
332+
/**
333+
* This function sets the error message using the given format string, and appends and resets the
334+
* latest error string.
335+
* The resulting formatted string is silently truncated at RCUTILS_ERROR_MESSAGE_MAX_LENGTH.
336+
*
337+
* If there is no previous error, has same behavior as RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING.
338+
*
339+
* \param[in] format_string The string to be used as the format of the error message.
340+
* \param[in] ... Arguments for the format string.
341+
*/
342+
#define RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING_AND_APPEND_PREV_ERROR(format_string, ...) \
343+
do { \
344+
rcutils_error_string_t error_string = rcutils_get_error_string(); \
345+
rcutils_reset_error(); \
346+
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING( \
347+
RCUTILS_EXPAND(format_string ": %s"), __VA_ARGS__, error_string.str); \
348+
} while (0)
349+
350+
/// Write the given msg out to stderr, limiting the buffer size in the `fwrite`, appending the
351+
/// previous error.
352+
/**
353+
* This will reset the previous error, if it exists.
354+
* If there is no previous error, has same behavior as RCUTILS_SAFE_FWRITE_TO_STDERR.
355+
*/
356+
#define RCUTILS_SAFE_FWRITE_TO_STDERR_AND_APPEND_PREV_ERROR(msg) \
357+
do { \
358+
rcutils_error_string_t error_string = rcutils_get_error_string(); \
359+
rcutils_reset_error(); \
360+
RCUTILS_SAFE_FWRITE_TO_STDERR(msg); \
361+
RCUTILS_SAFE_FWRITE_TO_STDERR_WITH_FORMAT_STRING(": %s", error_string.str); \
362+
} while (0)
363+
364+
/// Set the error message to stderr using a format string and format arguments, appending the
365+
/// previous error.
366+
/**
367+
* This function sets the error message to stderr using the given format string, appending and
368+
* resetting the previous error.
369+
* The resulting formatted string is silently truncated at RCUTILS_ERROR_MESSAGE_MAX_LENGTH.
370+
*
371+
* This will reset the previous error, if it exists.
372+
* If there is no previous error, has same behavior as
373+
* RCUTILS_SAFE_FWRITE_TO_STDERR_WITH_FORMAT_STRING.
374+
*
375+
* \param[in] format_string The string to be used as the format of the error message.
376+
* \param[in] ... Arguments for the format string.
377+
*/
378+
#define RCUTILS_SAFE_FWRITE_TO_STDERR_WITH_FORMAT_STRING_AND_APPEND_PREV_ERROR(format_string, ...) \
379+
do { \
380+
rcutils_error_string_t error_string = rcutils_get_error_string(); \
381+
rcutils_reset_error(); \
382+
RCUTILS_SAFE_FWRITE_TO_STDERR_WITH_FORMAT_STRING(format_string, __VA_ARGS__); \
383+
RCUTILS_SAFE_FWRITE_TO_STDERR_WITH_FORMAT_STRING(": %s", error_string.str); \
384+
} while (0)
385+
317386
#ifdef __cplusplus
318387
}
319388
#endif

include/rcutils/macros.h

+2
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ extern "C"
6464
#define RCUTILS_THREAD_LOCAL _Thread_local
6565
#endif
6666

67+
// Helper macros for nested macro expansion
68+
#define RCUTILS_EXPAND(x) x
6769
#define RCUTILS_STRINGIFY_IMPL(x) #x
6870
#define RCUTILS_STRINGIFY(x) RCUTILS_STRINGIFY_IMPL(x)
6971

0 commit comments

Comments
 (0)