1#ifndef AWS_COMMON_ERROR_H
2#define AWS_COMMON_ERROR_H
3
4/*
5 * Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License").
8 * You may not use this file except in compliance with the License.
9 * A copy of the License is located at
10 *
11 * http://aws.amazon.com/apache2.0
12 *
13 * or in the "license" file accompanying this file. This file is distributed
14 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
15 * express or implied. See the License for the specific language governing
16 * permissions and limitations under the License.
17 */
18
19#include <aws/common/assert.h>
20#include <aws/common/exports.h>
21#include <aws/common/macros.h>
22#include <aws/common/stdint.h>
23
24#define AWS_OP_SUCCESS (0)
25#define AWS_OP_ERR (-1)
26
27struct aws_error_info {
28 int error_code;
29 const char *literal_name;
30 const char *error_str;
31 const char *lib_name;
32 const char *formatted_name;
33};
34
35struct aws_error_info_list {
36 const struct aws_error_info *error_list;
37 uint16_t count;
38};
39
40#define AWS_DEFINE_ERROR_INFO(C, ES, LN) \
41 { \
42 .literal_name = #C, .error_code = (C), .error_str = (ES), .lib_name = (LN), \
43 .formatted_name = LN ": " #C ", " ES, \
44 }
45
46typedef void(aws_error_handler_fn)(int err, void *ctx);
47
48AWS_EXTERN_C_BEGIN
49
50/*
51 * Returns the latest error code on the current thread, or 0 if none have
52 * occurred.
53 */
54AWS_COMMON_API
55int aws_last_error(void);
56
57/*
58 * Returns the error str corresponding to `err`.
59 */
60AWS_COMMON_API
61const char *aws_error_str(int err);
62
63/*
64 * Returns the enum name corresponding to `err`.
65 */
66AWS_COMMON_API
67const char *aws_error_name(int err);
68
69/*
70 * Returns the error lib name corresponding to `err`.
71 */
72AWS_COMMON_API
73const char *aws_error_lib_name(int err);
74
75/*
76 * Returns libname concatenated with error string.
77 */
78AWS_COMMON_API
79const char *aws_error_debug_str(int err);
80
81/*
82 * Internal implementation detail.
83 */
84AWS_COMMON_API
85void aws_raise_error_private(int err);
86
87/*
88 * Raises `err` to the installed callbacks, and sets the thread's error.
89 */
90AWS_STATIC_IMPL
91int aws_raise_error(int err);
92
93/*
94 * Resets the `err` back to defaults
95 */
96AWS_COMMON_API
97void aws_reset_error(void);
98/*
99 * Sets `err` to the latest error. Does not invoke callbacks.
100 */
101AWS_COMMON_API
102void aws_restore_error(int err);
103
104/*
105 * Sets an application wide error handler function. This will be overridden by
106 * the thread local handler. The previous handler is returned, this can be used
107 * for restoring an error handler if it needs to be overridden temporarily.
108 * Setting this to NULL will turn off this error callback after it has been
109 * enabled.
110 */
111AWS_COMMON_API
112aws_error_handler_fn *aws_set_global_error_handler_fn(aws_error_handler_fn *handler, void *ctx);
113
114/*
115 * Sets a thread-local error handler function. This will override the global
116 * handler. The previous handler is returned, this can be used for restoring an
117 * error handler if it needs to be overridden temporarily. Setting this to NULL
118 * will turn off this error callback after it has been enabled.
119 */
120AWS_COMMON_API
121aws_error_handler_fn *aws_set_thread_local_error_handler_fn(aws_error_handler_fn *handler, void *ctx);
122
123/** TODO: this needs to be a private function (wait till we have the cmake story
124 * better before moving it though). It should be external for the purpose of
125 * other libs we own, but customers should not be able to hit it without going
126 * out of their way to do so.
127 */
128AWS_COMMON_API
129void aws_register_error_info(const struct aws_error_info_list *error_info);
130
131AWS_COMMON_API
132void aws_unregister_error_info(const struct aws_error_info_list *error_info);
133
134/**
135 * Convert a c library io error into an aws error.
136 */
137AWS_COMMON_API
138int aws_translate_and_raise_io_error(int error_no);
139
140#ifndef AWS_NO_STATIC_IMPL
141# include <aws/common/error.inl>
142#endif /* AWS_NO_STATIC_IMPL */
143
144AWS_EXTERN_C_END
145
146enum aws_common_error {
147 AWS_ERROR_SUCCESS = 0,
148 AWS_ERROR_OOM,
149 AWS_ERROR_UNKNOWN,
150 AWS_ERROR_SHORT_BUFFER,
151 AWS_ERROR_OVERFLOW_DETECTED,
152 AWS_ERROR_UNSUPPORTED_OPERATION,
153 AWS_ERROR_INVALID_BUFFER_SIZE,
154 AWS_ERROR_INVALID_HEX_STR,
155 AWS_ERROR_INVALID_BASE64_STR,
156 AWS_ERROR_INVALID_INDEX,
157 AWS_ERROR_THREAD_INVALID_SETTINGS,
158 AWS_ERROR_THREAD_INSUFFICIENT_RESOURCE,
159 AWS_ERROR_THREAD_NO_PERMISSIONS,
160 AWS_ERROR_THREAD_NOT_JOINABLE,
161 AWS_ERROR_THREAD_NO_SUCH_THREAD_ID,
162 AWS_ERROR_THREAD_DEADLOCK_DETECTED,
163 AWS_ERROR_MUTEX_NOT_INIT,
164 AWS_ERROR_MUTEX_TIMEOUT,
165 AWS_ERROR_MUTEX_CALLER_NOT_OWNER,
166 AWS_ERROR_MUTEX_FAILED,
167 AWS_ERROR_COND_VARIABLE_INIT_FAILED,
168 AWS_ERROR_COND_VARIABLE_TIMED_OUT,
169 AWS_ERROR_COND_VARIABLE_ERROR_UNKNOWN,
170 AWS_ERROR_CLOCK_FAILURE,
171 AWS_ERROR_LIST_EMPTY,
172 AWS_ERROR_DEST_COPY_TOO_SMALL,
173 AWS_ERROR_LIST_EXCEEDS_MAX_SIZE,
174 AWS_ERROR_LIST_STATIC_MODE_CANT_SHRINK,
175 AWS_ERROR_PRIORITY_QUEUE_FULL,
176 AWS_ERROR_PRIORITY_QUEUE_EMPTY,
177 AWS_ERROR_PRIORITY_QUEUE_BAD_NODE,
178 AWS_ERROR_HASHTBL_ITEM_NOT_FOUND,
179 AWS_ERROR_INVALID_DATE_STR,
180 AWS_ERROR_INVALID_ARGUMENT,
181 AWS_ERROR_RANDOM_GEN_FAILED,
182 AWS_ERROR_MALFORMED_INPUT_STRING,
183 AWS_ERROR_UNIMPLEMENTED,
184 AWS_ERROR_INVALID_STATE,
185 AWS_ERROR_ENVIRONMENT_GET,
186 AWS_ERROR_ENVIRONMENT_SET,
187 AWS_ERROR_ENVIRONMENT_UNSET,
188 AWS_ERROR_STREAM_UNSEEKABLE,
189 AWS_ERROR_NO_PERMISSION,
190 AWS_ERROR_FILE_INVALID_PATH,
191 AWS_ERROR_MAX_FDS_EXCEEDED,
192 AWS_ERROR_SYS_CALL_FAILURE,
193 AWS_ERROR_C_STRING_BUFFER_NOT_NULL_TERMINATED,
194 AWS_ERROR_STRING_MATCH_NOT_FOUND,
195
196 AWS_ERROR_END_COMMON_RANGE = 0x03FF
197};
198
199#endif /* AWS_COMMON_ERROR_H */
200