Skip to content

Commit

Permalink
use new error handling API (#153)
Browse files Browse the repository at this point in the history
* use new error handling API

Signed-off-by: William Woodall <william@osrfoundation.org>

* use semicolons after macros

Signed-off-by: William Woodall <william@osrfoundation.org>
  • Loading branch information
wjwwood authored Nov 2, 2018
1 parent 3b9ea0f commit dd2c47d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 57 deletions.
1 change: 0 additions & 1 deletion rmw/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ include_directories(include)
set(rmw_sources
"src/allocators.c"
"src/convert_rcutils_ret_to_rmw_ret.c"
"src/error_handling.c"
"src/names_and_types.c"
"src/sanity_checks.c"
"src/node_security_options.c"
Expand Down
41 changes: 14 additions & 27 deletions rmw/include/rmw/error_handling.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,48 +20,35 @@ extern "C"
{
#endif

#include <stdbool.h>
#include <stddef.h>

#include <rcutils/error_handling.h>

#include "rmw/visibility_control.h"

typedef rcutils_error_string_t rmw_error_string_t;
typedef rcutils_error_state_t rmw_error_state_t;

#define rmw_error_state_copy rcutils_error_state_copy
#define RMW_SAFE_FWRITE_TO_STDERR RCUTILS_SAFE_FWRITE_TO_STDERR

#define rmw_initialize_error_handling_thread_local_storage \
rcutils_initialize_error_handling_thread_local_storage

#define rmw_error_state_fini rcutils_error_state_fini
#define rmw_set_error_state rcutils_set_error_state

// TODO(wjwwood): replace this completely with rcutils_set_error_state()
// once the rmw APIs take an allocator that can be passed
// by the rmw implementations on to the error functions
/// Set the error state, implicitly uses rcutils_get_default_allocator().
/**
* \see rcutils_get_default_allocator()
* \see rcutils_set_error_state()
*/
RMW_PUBLIC
void
rmw_set_error_state(const char * error_msg, const char * file, size_t line_number);
#define RMW_CHECK_ARGUMENT_FOR_NULL(argument, error_return_type) \
RCUTILS_CHECK_ARGUMENT_FOR_NULL(argument, error_return_type)

/// Set the error message, as well as append the current file and line number.
/**
* \see RCUTILS_SET_ERROR_MSG
*/
#define RMW_SET_ERROR_MSG(msg) rmw_set_error_state(msg, __FILE__, __LINE__);
#define RMW_CHECK_FOR_NULL_WITH_MSG(value, msg, error_statement) \
RCUTILS_CHECK_FOR_NULL_WITH_MSG(value, msg, error_statement)

#define RMW_SET_ERROR_MSG_ALLOC(msg, allocator) \
rcutils_set_error_state(msg, __FILE__, __LINE__, allocator);
#define RMW_SET_ERROR_MSG(msg) RCUTILS_SET_ERROR_MSG(msg)

#define RMW_SET_ERROR_MSG_WITH_FORMAT_STRING(format_string, ...) \
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(format_string, __VA_ARGS__)

#define rmw_error_is_set rcutils_error_is_set

#define rmw_get_error_state rcutils_get_error_state

#define rmw_get_error_string rcutils_get_error_string

#define rmw_get_error_string_safe rcutils_get_error_string_safe

#define rmw_reset_error rcutils_reset_error

#ifdef __cplusplus
Expand Down
21 changes: 0 additions & 21 deletions rmw/src/error_handling.c

This file was deleted.

16 changes: 8 additions & 8 deletions rmw/src/names_and_types.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,26 @@ rmw_names_and_types_init(
rcutils_allocator_t * allocator)
{
if (!allocator) {
RMW_SET_ERROR_MSG("allocator is null")
RMW_SET_ERROR_MSG("allocator is null");
return RMW_RET_INVALID_ARGUMENT;
}
if (!names_and_types) {
RMW_SET_ERROR_MSG_ALLOC("names_and_types is null", *allocator)
RMW_SET_ERROR_MSG("names_and_types is null");
return RMW_RET_INVALID_ARGUMENT;
}
rcutils_ret_t rcutils_ret = rcutils_string_array_init(&names_and_types->names, size, allocator);
if (rcutils_ret != RCUTILS_RET_OK) {
RMW_SET_ERROR_MSG(rcutils_get_error_string_safe())
RMW_SET_ERROR_MSG(rcutils_get_error_string().str);
return rmw_convert_rcutils_ret_to_rmw_ret(rcutils_ret);
}
names_and_types->types =
allocator->zero_allocate(size, sizeof(rcutils_string_array_t), allocator->state);
if (!names_and_types->types) {
rcutils_ret = rcutils_string_array_fini(&names_and_types->names);
if (rcutils_ret != RCUTILS_RET_OK) {
RCUTILS_LOG_ERROR("error while reporting error: %s", rcutils_get_error_string_safe());
RCUTILS_LOG_ERROR("error while reporting error: %s", rcutils_get_error_string().str);
}
RMW_SET_ERROR_MSG_ALLOC("failed to allocate memory for types", *allocator)
RMW_SET_ERROR_MSG("failed to allocate memory for types");
return RMW_RET_BAD_ALLOC;
}
return RMW_RET_OK;
Expand All @@ -89,7 +89,7 @@ rmw_names_and_types_fini(rmw_names_and_types_t * names_and_types)
return RMW_RET_INVALID_ARGUMENT;
}
if (names_and_types->names.size && !names_and_types->types) {
RMW_SET_ERROR_MSG("invalid names_and_types")
RMW_SET_ERROR_MSG("invalid names_and_types");
return RMW_RET_INVALID_ARGUMENT;
}
rcutils_ret_t rcutils_ret;
Expand All @@ -101,7 +101,7 @@ rmw_names_and_types_fini(rmw_names_and_types_t * names_and_types)
}
rcutils_ret = rcutils_string_array_fini(&names_and_types->types[i]);
if (rcutils_ret != RCUTILS_RET_OK) {
RMW_SET_ERROR_MSG(rcutils_get_error_string_safe())
RMW_SET_ERROR_MSG(rcutils_get_error_string().str);
return rmw_convert_rcutils_ret_to_rmw_ret(rcutils_ret);
}
}
Expand All @@ -115,7 +115,7 @@ rmw_names_and_types_fini(rmw_names_and_types_t * names_and_types)
// Cleanup names string array
rcutils_ret = rcutils_string_array_fini(&names_and_types->names);
if (rcutils_ret != RCUTILS_RET_OK) {
RMW_SET_ERROR_MSG(rcutils_get_error_string_safe())
RMW_SET_ERROR_MSG(rcutils_get_error_string().str);
return rmw_convert_rcutils_ret_to_rmw_ret(rcutils_ret);
}
return RMW_RET_OK;
Expand Down

0 comments on commit dd2c47d

Please sign in to comment.