| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/common/error.h> |
| 7 |
|
| 8 |
#include <aws/common/common.h> |
| 9 |
|
| 10 |
#include <errno.h> |
| 11 |
#include <stdio.h> |
| 12 |
#include <stdlib.h> |
| 13 |
|
| 14 |
static AWS_THREAD_LOCAL int tl_last_error = 0; |
| 15 |
|
| 16 |
static aws_error_handler_fn *s_global_handler = NULL; |
| 17 |
static void *s_global_error_context = NULL; |
| 18 |
|
| 19 |
static AWS_THREAD_LOCAL aws_error_handler_fn *tl_thread_handler = NULL; |
| 20 |
AWS_THREAD_LOCAL void *tl_thread_handler_context = NULL; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#define SLOT_MASK (AWS_ERROR_ENUM_STRIDE - 1) |
| 27 |
|
| 28 |
static const int MAX_ERROR_CODE = AWS_ERROR_ENUM_STRIDE * AWS_PACKAGE_SLOTS; |
| 29 |
|
| 30 |
static const struct aws_error_info_list *volatile ERROR_SLOTS[AWS_PACKAGE_SLOTS] = {0}; |
| 31 |
|
| 32 |
int aws_last_error(void) { |
| 33 |
return tl_last_error; |
| 34 |
} |
| 35 |
|
| 36 |
static const struct aws_error_info *get_error_by_code(int err) { |
| 37 |
if (err >= MAX_ERROR_CODE || err < 0) { |
| 38 |
return NULL; |
| 39 |
} |
| 40 |
|
| 41 |
uint32_t slot_index = (uint32_t)err >> AWS_ERROR_ENUM_STRIDE_BITS; |
| 42 |
uint32_t error_index = (uint32_t)err & SLOT_MASK; |
| 43 |
|
| 44 |
const struct aws_error_info_list *error_slot = ERROR_SLOTS[slot_index]; |
| 45 |
|
| 46 |
if (!error_slot || error_index >= error_slot->count) { |
| 47 |
return NULL; |
| 48 |
} |
| 49 |
|
| 50 |
return &error_slot->error_list[error_index]; |
| 51 |
} |
| 52 |
|
| 53 |
const char *aws_error_str(int err) { |
| 54 |
const struct aws_error_info *error_info = get_error_by_code(err); |
| 55 |
|
| 56 |
if (error_info) { |
| 57 |
return error_info->error_str; |
| 58 |
} |
| 59 |
|
| 60 |
return "Unknown Error Code"; |
| 61 |
} |
| 62 |
|
| 63 |
const char *aws_error_name(int err) { |
| 64 |
const struct aws_error_info *error_info = get_error_by_code(err); |
| 65 |
|
| 66 |
if (error_info) { |
| 67 |
return error_info->literal_name; |
| 68 |
} |
| 69 |
|
| 70 |
return "Unknown Error Code"; |
| 71 |
} |
| 72 |
|
| 73 |
const char *aws_error_lib_name(int err) { |
| 74 |
const struct aws_error_info *error_info = get_error_by_code(err); |
| 75 |
|
| 76 |
if (error_info) { |
| 77 |
return error_info->lib_name; |
| 78 |
} |
| 79 |
|
| 80 |
return "Unknown Error Code"; |
| 81 |
} |
| 82 |
|
| 83 |
const char *aws_error_debug_str(int err) { |
| 84 |
const struct aws_error_info *error_info = get_error_by_code(err); |
| 85 |
|
| 86 |
if (error_info) { |
| 87 |
return error_info->formatted_name; |
| 88 |
} |
| 89 |
|
| 90 |
return "Unknown Error Code"; |
| 91 |
} |
| 92 |
|
| 93 |
void aws_raise_error_private(int err) { |
| 94 |
tl_last_error = err; |
| 95 |
|
| 96 |
if (tl_thread_handler) { |
| 97 |
tl_thread_handler(tl_last_error, tl_thread_handler_context); |
| 98 |
} else if (s_global_handler) { |
| 99 |
s_global_handler(tl_last_error, s_global_error_context); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
void aws_reset_error(void) { |
| 104 |
tl_last_error = 0; |
| 105 |
} |
| 106 |
|
| 107 |
void aws_restore_error(int err) { |
| 108 |
tl_last_error = err; |
| 109 |
} |
| 110 |
|
| 111 |
aws_error_handler_fn *aws_set_global_error_handler_fn(aws_error_handler_fn *handler, void *ctx) { |
| 112 |
aws_error_handler_fn *old_handler = s_global_handler; |
| 113 |
s_global_handler = handler; |
| 114 |
s_global_error_context = ctx; |
| 115 |
|
| 116 |
return old_handler; |
| 117 |
} |
| 118 |
|
| 119 |
aws_error_handler_fn *aws_set_thread_local_error_handler_fn(aws_error_handler_fn *handler, void *ctx) { |
| 120 |
aws_error_handler_fn *old_handler = tl_thread_handler; |
| 121 |
tl_thread_handler = handler; |
| 122 |
tl_thread_handler_context = ctx; |
| 123 |
|
| 124 |
return old_handler; |
| 125 |
} |
| 126 |
|
| 127 |
void aws_register_error_info(const struct aws_error_info_list *error_info) { |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
AWS_FATAL_ASSERT(error_info); |
| 134 |
AWS_FATAL_ASSERT(error_info->error_list); |
| 135 |
AWS_FATAL_ASSERT(error_info->count); |
| 136 |
|
| 137 |
const int min_range = error_info->error_list[0].error_code; |
| 138 |
const int slot_index = min_range >> AWS_ERROR_ENUM_STRIDE_BITS; |
| 139 |
|
| 140 |
if (slot_index >= AWS_PACKAGE_SLOTS || slot_index < 0) { |
| 141 |
|
| 142 |
|
| 143 |
fprintf(stderr, "Bad error slot index %d\n", slot_index); |
| 144 |
AWS_FATAL_ASSERT(false); |
| 145 |
} |
| 146 |
|
| 147 |
#if DEBUG_BUILD |
| 148 |
|
| 149 |
const int expected_first_code = slot_index << AWS_ERROR_ENUM_STRIDE_BITS; |
| 150 |
if (error_info->error_list[0].error_code != expected_first_code) { |
| 151 |
fprintf( |
| 152 |
stderr, |
| 153 |
"Missing info: First error in list should be %d, not %d (%s)\n", |
| 154 |
expected_first_code, |
| 155 |
error_info->error_list[0].error_code, |
| 156 |
error_info->error_list[0].literal_name); |
| 157 |
AWS_FATAL_ASSERT(0); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
for (int i = 0; i < error_info->count; ++i) { |
| 162 |
const int expected_code = min_range + i; |
| 163 |
const struct aws_error_info *info = &error_info->error_list[i]; |
| 164 |
if (info->error_code != expected_code) { |
| 165 |
if (info->error_code) { |
| 166 |
fprintf(stderr, "Error %s is at wrong index of error info list.\n", info->literal_name); |
| 167 |
} else { |
| 168 |
fprintf(stderr, "Error %d is missing from error info list.\n", expected_code); |
| 169 |
} |
| 170 |
AWS_FATAL_ASSERT(0); |
| 171 |
} |
| 172 |
} |
| 173 |
#endif |
| 174 |
|
| 175 |
ERROR_SLOTS[slot_index] = error_info; |
| 176 |
} |
| 177 |
|
| 178 |
void aws_unregister_error_info(const struct aws_error_info_list *error_info) { |
| 179 |
AWS_FATAL_ASSERT(error_info); |
| 180 |
AWS_FATAL_ASSERT(error_info->error_list); |
| 181 |
AWS_FATAL_ASSERT(error_info->count); |
| 182 |
|
| 183 |
const int min_range = error_info->error_list[0].error_code; |
| 184 |
const int slot_index = min_range >> AWS_ERROR_ENUM_STRIDE_BITS; |
| 185 |
|
| 186 |
if (slot_index >= AWS_PACKAGE_SLOTS || slot_index < 0) { |
| 187 |
|
| 188 |
|
| 189 |
fprintf(stderr, "Bad error slot index %d\n", slot_index); |
| 190 |
AWS_FATAL_ASSERT(0); |
| 191 |
} |
| 192 |
|
| 193 |
ERROR_SLOTS[slot_index] = NULL; |
| 194 |
} |
| 195 |
|
| 196 |
int aws_translate_and_raise_io_error(int error_no) { |
| 197 |
return aws_translate_and_raise_io_error_or(error_no, AWS_ERROR_SYS_CALL_FAILURE); |
| 198 |
} |
| 199 |
|
| 200 |
int aws_translate_and_raise_io_error_or(int error_no, int fallback_aws_error_code) { |
| 201 |
switch (error_no) { |
| 202 |
case EINVAL: |
| 203 |
|
| 204 |
|
| 205 |
if (fallback_aws_error_code != AWS_ERROR_SYS_CALL_FAILURE) { |
| 206 |
return aws_raise_error(fallback_aws_error_code); |
| 207 |
} else { |
| 208 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 209 |
} |
| 210 |
case EPERM: |
| 211 |
case EACCES: |
| 212 |
return aws_raise_error(AWS_ERROR_NO_PERMISSION); |
| 213 |
case EISDIR: |
| 214 |
case ENAMETOOLONG: |
| 215 |
case ENOENT: |
| 216 |
case ENOTDIR: |
| 217 |
return aws_raise_error(AWS_ERROR_FILE_INVALID_PATH); |
| 218 |
case EMFILE: |
| 219 |
case ENFILE: |
| 220 |
return aws_raise_error(AWS_ERROR_MAX_FDS_EXCEEDED); |
| 221 |
case ENOMEM: |
| 222 |
return aws_raise_error(AWS_ERROR_OOM); |
| 223 |
case ENOSPC: |
| 224 |
return aws_raise_error(AWS_ERROR_NO_SPACE); |
| 225 |
case ENOTEMPTY: |
| 226 |
return aws_raise_error(AWS_ERROR_DIRECTORY_NOT_EMPTY); |
| 227 |
default: |
| 228 |
return aws_raise_error(fallback_aws_error_code); |
| 229 |
} |
| 230 |
} |