Example #1
0
const char *
rmw_get_error_string_safe()
{
  if (!rmw_error_is_set()) {
    return "error not set";
  }
  return rmw_get_error_string();
}
Example #2
0
void
rmw_set_error_state(const char * error_string, const char * file, size_t line_number)
{
  if (rmw_error_is_set()) {
#if RMW_REPORT_ERROR_HANDLING_ERRORS
    fprintf(
      stderr,
      "[rmw|error_handling.c:" RMW_STRINGIFY(__LINE__) "] error string being overwritten: %s\n",
      rmw_get_error_string_safe());
#endif
    rmw_reset_error();
  }
  __rmw_error_state = (rmw_error_state_t *)rmw_allocate(sizeof(rmw_error_state_t));
  if (!__rmw_error_state) {
#if RMW_REPORT_ERROR_HANDLING_ERRORS
    // rmw_allocate failed, but fwrite might work?
    SAFE_FWRITE_TO_STDERR(
      "[rmw|error_handling.c:" RMW_STRINGIFY(__LINE__)
      "] failed to allocate memory for the error state struct\n");
#endif
    return;
  }
  __rmw_error_state->message = (char *)malloc(strlen(error_string));
  if (!__rmw_error_state->message) {
#if RMW_REPORT_ERROR_HANDLING_ERRORS
    // rmw_allocate failed, but fwrite might work?
    SAFE_FWRITE_TO_STDERR(
      "[rmw|error_handling.c:" RMW_STRINGIFY(__LINE__)
      "] failed to allocate memory for the error message in the error state struct\n");
#endif
    rmw_reset_error();  // This will free any allocations done so far.
    return;
  }
  // Cast the const away to set ->message initially.
  strcpy((char *)__rmw_error_state->message, error_string);
  __rmw_error_state->file = file;
  __rmw_error_state->line_number = line_number;
}