Loading...
Searching...
No Matches
as_error.h
Go to the documentation of this file.
1/*
2 * Copyright 2008-2024 Aerospike, Inc.
3 *
4 * Portions may be licensed to Aerospike, Inc. under one or more contributor
5 * license agreements.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
8 * use this file except in compliance with the License. You may obtain a copy of
9 * the License at http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations under
15 * the License.
16 */
17#pragma once
18
19#include <aerospike/as_std.h>
20#include <aerospike/as_status.h>
21#include <aerospike/as_string.h>
22
23#include <stdarg.h>
24#include <stdio.h>
25#include <string.h>
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
31//---------------------------------
32// Definitions
33//---------------------------------
34
35/**
36 * The size of as_error.message
37 */
38#define AS_ERROR_MESSAGE_MAX_SIZE 1024
39
40/**
41 * The maximum string length of as_error.message
42 */
43#define AS_ERROR_MESSAGE_MAX_LEN (AS_ERROR_MESSAGE_MAX_SIZE - 1)
44
45//---------------------------------
46// Types
47//---------------------------------
48
49/**
50 * All operations that interact with the Aerospike cluster accept an as_error
51 * argument and return an as_status value. The as_error argument is populated
52 * with information about the error that occurred. The as_status return value
53 * is the as_error.code value.
54 *
55 * When an operation succeeds, the as_error.code value is usually set to
56 * `AEROSPIKE_OK`. There are some operations which may have other success
57 * status codes, so please review each operation for information on status
58 * codes.
59 *
60 * When as_error.code is not a success value (`AEROSPIKE_OK`), then you can
61 * expect the other fields of as_error.code to be populated.
62 *
63 * Example usage:
64 * ~~~~~~~~~~{.c}
65 * as_error err;
66 *
67 * if ( aerospike_key_get(&as, &err, NULL, &key, &rec) != AEROSPIKE_OK ) {
68 * fprintf(stderr, "(%d) %s at %s[%s:%d]\n", error.code, err.message, err.func, err.file. err.line);
69 * }
70 * ~~~~~~~~~~
71 *
72 * You can reuse an as_error with multiple operations. Each operation
73 * internally resets the error. So, if an error occurred in one operation,
74 * and you did not check it, then the error will be lost with subsequent
75 * operations.
76 *
77 * Example usage:
78 *
79 * ~~~~~~~~~~{.c}
80 * as_error err;
81 *
82 * if ( aerospike_key_put(&as, &err, NULL, &key, rec) != AEROSPIKE_OK ) {
83 * fprintf(stderr, "(%d) %s at %s[%s:%d]\n", error.code, err.message, err.func, err.file. err.line);
84 * }
85 *
86 * if ( aerospike_key_get(&as, &err, NULL, &key, &rec) != AEROSPIKE_OK ) {
87 * fprintf(stderr, "(%d) %s at %s[%s:%d]\n", error.code, err.message, err.func, err.file. err.line);
88 * }
89 * ~~~~~~~~~~
90 *
91 * @ingroup client_objects
92 */
93typedef struct as_error_s {
94
95 /**
96 * Numeric error code
97 */
99
100 /**
101 * NULL-terminated error message
102 */
104
105 /**
106 * Name of the function where the error occurred.
107 */
108 const char * func;
109
110 /**
111 * Name of the file where the error occurred.
112 */
113 const char * file;
114
115 /**
116 * Line in the file where the error occurred.
117 */
118 uint32_t line;
119
120 /**
121 * Is it possible that the write transaction completed even though this error was generated.
122 * This may be the case when a client error occurs (like timeout) after the command was sent
123 * to the server.
124 */
126
127} as_error;
128
129//---------------------------------
130// Macros
131//---------------------------------
132
133/**
134 * Set all as_error fields and default in_doubt to false. Variable arguments are accepted.
135 *
136 * @relates as_error
137 */
138#define as_error_update(__err, __code, __fmt, ...) \
139 as_error_setallv( __err, __code, __func__, __FILE__, __LINE__, __fmt, ##__VA_ARGS__ );
140
141/**
142 * Set all as_error fields and default in_doubt to false. Variable arguments are not accepted.
143 *
144 * @relates as_error
145 */
146#define as_error_set_message(__err, __code, __msg) \
147 as_error_setall( __err, __code, __msg, __func__, __FILE__, __LINE__ );
148
149//---------------------------------
150// Functions
151//---------------------------------
152
153/**
154 * Initialize the error to default (empty) values, returning the error.
155 *
156 * @param err The error to initialize.
157 *
158 * @returns The initialized err.
159 *
160 * @relates as_error
161 */
162static inline as_error*
164{
165 err->code = AEROSPIKE_OK;
166 err->message[0] = '\0';
167 err->func = NULL;
168 err->file = NULL;
169 err->line = 0;
170 err->in_doubt = false;
171 return err;
172}
173
174/**
175 * Resets the error to default (empty) values, returning the status code.
176 *
177 * @param err The error to reset.
178 *
179 * @returns AEROSPIKE_OK.
180 *
181 * @relates as_error
182 */
183static inline as_status
185{
186 err->code = AEROSPIKE_OK;
187 err->message[0] = '\0';
188 err->func = NULL;
189 err->file = NULL;
190 err->line = 0;
191 err->in_doubt = false;
192 return err->code;
193}
194
195/**
196 * Sets the error.
197 *
198 * @return The status code set for the error.
199 *
200 * @relates as_error
201 */
202static inline as_status
203as_error_setall(as_error* err, as_status code, const char * message, const char * func, const char * file, uint32_t line)
204{
205 err->code = code;
207 err->func = func;
208 err->file = file;
209 err->line = line;
210 err->in_doubt = false;
211 return err->code;
212}
213
214/**
215 * Sets the error.
216 *
217 * @return The status code set for the error.
218 *
219 * @relates as_error
220 */
221static inline as_status
222as_error_setallv(as_error* err, as_status code, const char * func, const char * file, uint32_t line, const char * fmt, ...)
223{
224 if ( fmt != NULL ) {
225 va_list ap;
226 va_start(ap, fmt);
227 vsnprintf(err->message, AS_ERROR_MESSAGE_MAX_LEN, fmt, ap);
229 va_end(ap);
230 }
231 err->code = code;
232 err->func = func;
233 err->file = file;
234 err->line = line;
235 err->in_doubt = false;
236 return err->code;
237}
238
239/**
240 * Set whether it is possible that the write transaction may have completed
241 * even though this exception was generated. This may be the case when a
242 * client error occurs (like timeout) after the command was sent to the server.
243 *
244 * @relates as_error
245 */
246static inline void
247as_error_set_in_doubt(as_error* err, bool is_read, uint32_t command_sent_counter)
248{
249 err->in_doubt = (!is_read && (command_sent_counter > 1 || (command_sent_counter == 1 &&
250 (err->code == AEROSPIKE_ERR_TIMEOUT || err->code <= 0))));
251}
252
253/**
254 * Copy error from source to target.
255 *
256 * @relates as_error
257 */
258static inline void
259as_error_copy(as_error * trg, const as_error * src)
260{
261 trg->code = src->code;
262 strcpy(trg->message, src->message);
263 trg->func = src->func;
264 trg->file = src->file;
265 trg->line = src->line;
266 trg->in_doubt = src->in_doubt;
267}
268
269/**
270 * Append string to error message.
271 *
272 * @relates as_error
273 */
274static inline void
275as_error_append(as_error* err, const char* str)
276{
277 strncat(err->message, str, sizeof(err->message) - strlen(err->message) - 1);
278}
279
280/**
281 * Return string representation of error code. Result should not be freed.
282 *
283 * @relates as_error
284 */
285AS_EXTERN char*
287
288#ifdef __cplusplus
289} // end extern "C"
290#endif
#define AS_ERROR_MESSAGE_MAX_LEN
Definition as_error.h:43
#define AS_ERROR_MESSAGE_MAX_SIZE
Definition as_error.h:38
as_status
Definition as_status.h:30
@ AEROSPIKE_ERR_TIMEOUT
Definition as_status.h:181
@ AEROSPIKE_OK
Definition as_status.h:128
#define AS_EXTERN
Definition as_std.h:25
AS_EXTERN bool as_strncpy(char *trg, const char *src, int size)
static void as_error_append(as_error *err, const char *str)
Definition as_error.h:275
char message[AS_ERROR_MESSAGE_MAX_SIZE]
Definition as_error.h:103
static as_status as_error_setall(as_error *err, as_status code, const char *message, const char *func, const char *file, uint32_t line)
Definition as_error.h:203
const char * file
Definition as_error.h:113
static as_status as_error_setallv(as_error *err, as_status code, const char *func, const char *file, uint32_t line, const char *fmt,...)
Definition as_error.h:222
AS_EXTERN char * as_error_string(as_status status)
uint32_t line
Definition as_error.h:118
bool in_doubt
Definition as_error.h:125
static void as_error_copy(as_error *trg, const as_error *src)
Definition as_error.h:259
const char * func
Definition as_error.h:108
static as_status as_error_reset(as_error *err)
Definition as_error.h:184
static as_error * as_error_init(as_error *err)
Definition as_error.h:163
static void as_error_set_in_doubt(as_error *err, bool is_read, uint32_t command_sent_counter)
Definition as_error.h:247
as_status code
Definition as_error.h:98