Document/standardize the return types from the fetch methods.
[awesomized/libmemcached] / libmemcached / error.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * LibMemcached
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
22 * written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 */
37
38 #include <libmemcached/common.h>
39 #include <cassert>
40
41 #define MAX_ERROR_LENGTH 2048
42 struct memcached_error_t
43 {
44 memcached_st *root;
45 uint64_t query_id;
46 struct memcached_error_t *next;
47 memcached_return_t rc;
48 int local_errno;
49 size_t size;
50 char message[MAX_ERROR_LENGTH];
51 };
52
53 static void _set(memcached_st& memc, memcached_string_t *str, memcached_return_t &rc, const char *at, int local_errno= 0)
54 {
55 (void)at;
56 if (memc.error_messages && memc.error_messages->query_id != memc.query_id)
57 {
58 memcached_error_free(&memc);
59 }
60
61 // For memory allocation we use our error since it is a bit more specific
62 if (local_errno == ENOMEM and rc == MEMCACHED_ERRNO)
63 {
64 rc= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
65 }
66
67 if (rc == MEMCACHED_MEMORY_ALLOCATION_FAILURE)
68 {
69 local_errno= ENOMEM;
70 }
71
72 if (rc == MEMCACHED_ERRNO and not local_errno)
73 {
74 local_errno= errno;
75 rc= MEMCACHED_ERRNO;
76 }
77
78 if (rc == MEMCACHED_ERRNO and local_errno == ENOTCONN)
79 {
80 rc= MEMCACHED_CONNECTION_FAILURE;
81 }
82
83 if (local_errno == EINVAL)
84 {
85 rc= MEMCACHED_INVALID_ARGUMENTS;
86 }
87
88 if (local_errno == ECONNREFUSED)
89 {
90 rc= MEMCACHED_CONNECTION_FAILURE;
91 }
92
93 memcached_error_t *error= (struct memcached_error_t *)libmemcached_malloc(&memc, sizeof(struct memcached_error_t));
94 if (not error) // Bad business if this happens
95 return;
96
97 error->root= &memc;
98 error->query_id= memc.query_id;
99 error->rc= rc;
100 error->local_errno= local_errno;
101
102 if (str and str->size and local_errno)
103 {
104 error->size= (int)snprintf(error->message, MAX_ERROR_LENGTH, "%s(%s), %.*s -> %s",
105 memcached_strerror(&memc, rc),
106 strerror(local_errno),
107 memcached_string_printf(*str), at);
108 }
109 else if (local_errno)
110 {
111 error->size= (int)snprintf(error->message, MAX_ERROR_LENGTH, "%s(%s) -> %s",
112 memcached_strerror(&memc, rc),
113 strerror(local_errno), at);
114 }
115 else if (str and str->size)
116 {
117 error->size= (int)snprintf(error->message, MAX_ERROR_LENGTH, "%s, %.*s -> %s",
118 memcached_strerror(&memc, rc),
119 int(str->size), str->c_str, at);
120 }
121 else
122 {
123 error->size= (int)snprintf(error->message, MAX_ERROR_LENGTH, "%s -> %s",
124 memcached_strerror(&memc, rc), at);
125 }
126
127 error->next= memc.error_messages;
128 memc.error_messages= error;
129 }
130
131 memcached_return_t memcached_set_error(memcached_st& memc, memcached_return_t rc, const char *at, const char *str, size_t length)
132 {
133 assert(rc != MEMCACHED_ERRNO);
134 memcached_string_t tmp= { str, length };
135 return memcached_set_error(memc, rc, at, tmp);
136 }
137
138 memcached_return_t memcached_set_error(memcached_server_st& self, memcached_return_t rc, const char *at, const char *str, size_t length)
139 {
140 assert(rc != MEMCACHED_ERRNO);
141 memcached_string_t tmp= { str, length };
142 return memcached_set_error(self, rc, at, tmp);
143 }
144
145 memcached_return_t memcached_set_error(memcached_st& memc, memcached_return_t rc, const char *at, memcached_string_t& str)
146 {
147 assert(rc != MEMCACHED_ERRNO);
148 if (memcached_success(rc))
149 return MEMCACHED_SUCCESS;
150
151 _set(memc, &str, rc, at);
152
153 return rc;
154 }
155
156 memcached_return_t memcached_set_error(memcached_server_st& self, memcached_return_t rc, const char *at, memcached_string_t& str)
157 {
158 assert(rc != MEMCACHED_ERRNO);
159 if (memcached_success(rc))
160 return MEMCACHED_SUCCESS;
161
162 char hostname_port_message[MAX_ERROR_LENGTH];
163 int size;
164 if (str.size)
165 {
166 size= snprintf(hostname_port_message, sizeof(hostname_port_message), "%.*s, host: %s:%d",
167 memcached_string_printf(str),
168 self.hostname, int(self.port));
169 }
170 else
171 {
172 size= snprintf(hostname_port_message, sizeof(hostname_port_message), "host: %s:%d",
173 self.hostname, int(self.port));
174 }
175
176 memcached_string_t error_host= { hostname_port_message, size };
177
178 if (not self.root)
179 return rc;
180
181 _set(*self.root, &error_host, rc, at);
182
183 return rc;
184 }
185
186 memcached_return_t memcached_set_error(memcached_server_st& self, memcached_return_t rc, const char *at)
187 {
188 assert(rc != MEMCACHED_ERRNO);
189 if (memcached_success(rc))
190 return MEMCACHED_SUCCESS;
191
192 char hostname_port[NI_MAXHOST +NI_MAXSERV + sizeof("host : ")];
193 int size= snprintf(hostname_port, sizeof(hostname_port), "host: %s:%d", self.hostname, int(self.port));
194
195 memcached_string_t error_host= { hostname_port, size};
196
197 if (not self.root)
198 return rc;
199
200 _set(*self.root, &error_host, rc, at);
201
202 return rc;
203 }
204
205 memcached_return_t memcached_set_error(memcached_st& self, memcached_return_t rc, const char *at)
206 {
207 assert(rc != MEMCACHED_ERRNO);
208 if (memcached_success(rc))
209 return MEMCACHED_SUCCESS;
210
211 _set(self, NULL, rc, at);
212
213 return rc;
214 }
215
216 memcached_return_t memcached_set_errno(memcached_st& self, int local_errno, const char *at, const char *str, size_t length)
217 {
218 memcached_string_t tmp= { str, length };
219 return memcached_set_errno(self, local_errno, at, tmp);
220 }
221
222 memcached_return_t memcached_set_errno(memcached_server_st& self, int local_errno, const char *at, const char *str, size_t length)
223 {
224 memcached_string_t tmp= { str, length };
225 return memcached_set_errno(self, local_errno, at, tmp);
226 }
227
228 memcached_return_t memcached_set_errno(memcached_st& self, int local_errno, const char *at)
229 {
230 if (not local_errno)
231 return MEMCACHED_SUCCESS;
232
233 memcached_return_t rc= MEMCACHED_ERRNO;
234 _set(self, NULL, rc, at, local_errno);
235
236 return rc;
237 }
238
239 memcached_return_t memcached_set_errno(memcached_st& memc, int local_errno, const char *at, memcached_string_t& str)
240 {
241 if (not local_errno)
242 return MEMCACHED_SUCCESS;
243
244 memcached_return_t rc= MEMCACHED_ERRNO;
245 _set(memc, &str, rc, at, local_errno);
246
247 return rc;
248 }
249
250 memcached_return_t memcached_set_errno(memcached_server_st& self, int local_errno, const char *at, memcached_string_t& str)
251 {
252 if (not local_errno)
253 return MEMCACHED_SUCCESS;
254
255 char hostname_port_message[MAX_ERROR_LENGTH];
256 int size;
257 if (str.size)
258 {
259 size= snprintf(hostname_port_message, sizeof(hostname_port_message), "%.*s, host: %s:%d",
260 memcached_string_printf(str),
261 self.hostname, int(self.port));
262 }
263 else
264 {
265 size= snprintf(hostname_port_message, sizeof(hostname_port_message), "host: %s:%d",
266 self.hostname, int(self.port));
267 }
268
269 memcached_string_t error_host= { hostname_port_message, size };
270
271 self.cached_errno= local_errno; // Store in the actual server
272
273 memcached_return_t rc= MEMCACHED_ERRNO;
274 if (not self.root)
275 return rc;
276
277 _set(*self.root, &error_host, rc, at, local_errno);
278
279 return rc;
280 }
281
282 memcached_return_t memcached_set_errno(memcached_server_st& self, int local_errno, const char *at)
283 {
284 if (not local_errno)
285 return MEMCACHED_SUCCESS;
286
287 char hostname_port_message[MAX_ERROR_LENGTH];
288 int size = snprintf(hostname_port_message, sizeof(hostname_port_message), "host: %s:%d",
289 self.hostname, int(self.port));
290
291 memcached_string_t error_host= { hostname_port_message, size };
292
293 self.cached_errno= local_errno; // Store in the actual server
294
295 memcached_return_t rc= MEMCACHED_ERRNO;
296 if (not self.root)
297 return rc;
298
299 _set(*self.root, &error_host, rc, at, local_errno);
300
301 return rc;
302 }
303
304 static void _error_print(const memcached_error_t *error)
305 {
306 if (not error)
307 return;
308
309 if (not error->size)
310 {
311 fprintf(stderr, "%s\n", memcached_strerror(NULL, error->rc) );
312 }
313 else
314 {
315 fprintf(stderr, "%s %s\n", memcached_strerror(NULL, error->rc), error->message);
316 }
317
318 _error_print(error->next);
319 }
320
321 void memcached_error_print(const memcached_st *self)
322 {
323 if (not self)
324 return;
325
326 _error_print(self->error_messages);
327 }
328
329 static void _error_free(memcached_error_t *error)
330 {
331 if (not error)
332 return;
333
334 _error_free(error->next);
335
336 if (error && error->root)
337 {
338 libmemcached_free(error->root, error);
339 }
340 else if (error)
341 {
342 free(error);
343 }
344 }
345
346 void memcached_error_free(memcached_st *self)
347 {
348 if (not self)
349 return;
350
351 _error_free(self->error_messages);
352 self->error_messages= NULL;
353 }
354
355 const char *memcached_last_error_message(memcached_st *memc)
356 {
357 if (not memc)
358 return memcached_strerror(memc, MEMCACHED_INVALID_ARGUMENTS);
359
360 if (not memc->error_messages)
361 return memcached_strerror(memc, MEMCACHED_SUCCESS);
362
363 if (not memc->error_messages->size)
364 return memcached_strerror(memc, memc->error_messages->rc);
365
366 return memc->error_messages->message;
367 }
368
369
370 bool memcached_has_current_error(memcached_st &memc)
371 {
372 if (memc.error_messages
373 and memc.error_messages->query_id == memc.query_id
374 and memcached_failed(memc.error_messages->rc))
375 {
376 return true;
377 }
378
379 return false;
380 }
381
382 memcached_return_t memcached_last_error(memcached_st *memc)
383 {
384 if (not memc)
385 return MEMCACHED_INVALID_ARGUMENTS;
386
387 if (not memc->error_messages)
388 return MEMCACHED_SUCCESS;
389
390 return memc->error_messages->rc;
391 }
392
393 int memcached_last_error_errno(memcached_st *memc)
394 {
395 if (not memc)
396 return 0;
397
398 if (not memc->error_messages)
399 return 0;
400
401 return memc->error_messages->local_errno;
402 }