1ca355b82231ac424fbbc89fb5bc1d50d9d1c8e4
[m6w6/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 <cstdarg>
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_server_st& server, memcached_st& memc)
54 {
55 if (server.error_messages && server.error_messages->query_id != server.root->query_id)
56 {
57 memcached_error_free(server);
58 }
59
60 if (memc.error_messages == NULL)
61 {
62 return;
63 }
64
65 memcached_error_t *error= libmemcached_xmalloc(&memc, memcached_error_t);
66 if (error == NULL) // Bad business if this happens
67 {
68 return;
69 }
70
71 memcpy(error, memc.error_messages, sizeof(memcached_error_t));
72 error->next= server.error_messages;
73 server.error_messages= error;
74 }
75
76 #if 0
77 static int error_log_fd= -1;
78 #endif
79
80 static void _set(memcached_st& memc, memcached_string_t *str, memcached_return_t &rc, const char *at, int local_errno= 0)
81 {
82 if (memc.error_messages && memc.error_messages->query_id != memc.query_id)
83 {
84 memcached_error_free(memc);
85 }
86
87 // For memory allocation we use our error since it is a bit more specific
88 if (local_errno == ENOMEM and rc == MEMCACHED_ERRNO)
89 {
90 rc= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
91 }
92
93 if (rc == MEMCACHED_MEMORY_ALLOCATION_FAILURE)
94 {
95 local_errno= ENOMEM;
96 }
97
98 if (rc == MEMCACHED_ERRNO and not local_errno)
99 {
100 local_errno= errno;
101 rc= MEMCACHED_ERRNO;
102 }
103
104 if (rc == MEMCACHED_ERRNO and local_errno == ENOTCONN)
105 {
106 rc= MEMCACHED_CONNECTION_FAILURE;
107 }
108
109 if (local_errno == EINVAL)
110 {
111 rc= MEMCACHED_INVALID_ARGUMENTS;
112 }
113
114 if (local_errno == ECONNREFUSED)
115 {
116 rc= MEMCACHED_CONNECTION_FAILURE;
117 }
118
119 memcached_error_t *error= libmemcached_xmalloc(&memc, memcached_error_t);
120 if (error == NULL) // Bad business if this happens
121 {
122 return;
123 }
124
125 error->root= &memc;
126 error->query_id= memc.query_id;
127 error->rc= rc;
128 error->local_errno= local_errno;
129
130 const char *errmsg_ptr;
131 char errmsg[MAX_ERROR_LENGTH];
132 errmsg[0]= 0;
133 errmsg_ptr= errmsg;
134
135 if (local_errno)
136 {
137 #ifdef STRERROR_R_CHAR_P
138 errmsg_ptr= strerror_r(local_errno, errmsg, sizeof(errmsg));
139 #else
140 strerror_r(local_errno, errmsg, sizeof(errmsg));
141 errmsg_ptr= errmsg;
142 #endif
143 }
144
145
146 if (str and str->size and local_errno)
147 {
148 error->size= (int)snprintf(error->message, MAX_ERROR_LENGTH, "(%lu) %s(%s), %.*s -> %s",
149 long(error->root),
150 memcached_strerror(&memc, rc),
151 errmsg_ptr,
152 memcached_string_printf(*str), at);
153 }
154 else if (local_errno)
155 {
156 error->size= (int)snprintf(error->message, MAX_ERROR_LENGTH, "(%lu) %s(%s) -> %s",
157 long(error->root),
158 memcached_strerror(&memc, rc),
159 errmsg_ptr,
160 at);
161 }
162 else if (rc == MEMCACHED_PARSE_ERROR and str and str->size)
163 {
164 error->size= (int)snprintf(error->message, MAX_ERROR_LENGTH, "(%lu) %.*s -> %s",
165 long(error->root),
166 int(str->size), str->c_str, at);
167 }
168 else if (str and str->size)
169 {
170 error->size= (int)snprintf(error->message, MAX_ERROR_LENGTH, "(%lu) %s, %.*s -> %s",
171 long(error->root),
172 memcached_strerror(&memc, rc),
173 int(str->size), str->c_str, at);
174 }
175 else
176 {
177 error->size= (int)snprintf(error->message, MAX_ERROR_LENGTH, "(%lu) %s -> %s",
178 long(error->root),
179 memcached_strerror(&memc, rc), at);
180 }
181
182 error->next= memc.error_messages;
183 memc.error_messages= error;
184
185 #if 0
186 if (error_log_fd == -1)
187 {
188 // unlink("/tmp/libmemcachd.log");
189 if ((error_log_fd= open("/tmp/libmemcachd.log", O_CREAT | O_WRONLY | O_APPEND, 0644)) < 0)
190 {
191 perror("open");
192 error_log_fd= -1;
193 }
194 }
195 ::write(error_log_fd, error->message, error->size);
196 ::write(error_log_fd, "\n", 1);
197 #endif
198 }
199
200 memcached_return_t memcached_set_error(memcached_st& memc, memcached_return_t rc, const char *at, const char *str, size_t length)
201 {
202 assert_msg(rc != MEMCACHED_ERRNO, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
203 memcached_string_t tmp= { str, length };
204 return memcached_set_error(memc, rc, at, tmp);
205 }
206
207 memcached_return_t memcached_set_error(memcached_server_st& self, memcached_return_t rc, const char *at, const char *str, size_t length)
208 {
209 assert_msg(rc != MEMCACHED_ERRNO, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
210 assert_msg(rc != MEMCACHED_SOME_ERRORS, "Programmer error, MEMCACHED_SOME_ERRORS was about to be set on a memcached_server_st");
211 memcached_string_t tmp= { str, length };
212 return memcached_set_error(self, rc, at, tmp);
213 }
214
215 memcached_return_t memcached_set_error(memcached_st& memc, memcached_return_t rc, const char *at, memcached_string_t& str)
216 {
217 assert_msg(rc != MEMCACHED_ERRNO, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
218 if (memcached_fatal(rc) == false)
219 {
220 return rc;
221 }
222
223 _set(memc, &str, rc, at);
224
225 return rc;
226 }
227
228 memcached_return_t memcached_set_parser_error(memcached_st& memc,
229 const char *at,
230 const char *format, ...)
231 {
232 va_list args;
233
234 char buffer[BUFSIZ];
235 va_start(args, format);
236 int length= vsnprintf(buffer, sizeof(buffer), format, args);
237 va_end(args);
238
239 return memcached_set_error(memc, MEMCACHED_PARSE_ERROR, at, buffer, length);
240 }
241
242 memcached_return_t memcached_set_error(memcached_server_st& self, memcached_return_t rc, const char *at, memcached_string_t& str)
243 {
244 assert_msg(rc != MEMCACHED_ERRNO, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
245 assert_msg(rc != MEMCACHED_SOME_ERRORS, "Programmer error, MEMCACHED_SOME_ERRORS was about to be set on a memcached_server_st");
246 if (memcached_fatal(rc) == false)
247 {
248 return rc;
249 }
250
251 char hostname_port_message[MAX_ERROR_LENGTH];
252 int size;
253 if (str.size)
254 {
255 size= snprintf(hostname_port_message, sizeof(hostname_port_message), "%.*s, host: %s:%d",
256 memcached_string_printf(str),
257 self.hostname, int(self.port));
258 }
259 else
260 {
261 size= snprintf(hostname_port_message, sizeof(hostname_port_message), "host: %s:%d",
262 self.hostname, int(self.port));
263 }
264
265 memcached_string_t error_host= { hostname_port_message, size };
266
267 assert(self.root);
268 if (self.root == NULL)
269 {
270 return rc;
271 }
272
273 _set(*self.root, &error_host, rc, at);
274 _set(self, (*self.root));
275 assert(self.root->error_messages);
276 assert(self.error_messages);
277
278 return rc;
279 }
280
281 memcached_return_t memcached_set_error(memcached_server_st& self, memcached_return_t rc, const char *at)
282 {
283 assert_msg(rc != MEMCACHED_SOME_ERRORS, "Programmer error, MEMCACHED_SOME_ERRORS was about to be set on a memcached_server_st");
284 if (memcached_fatal(rc) == false)
285 {
286 return rc;
287 }
288
289 char hostname_port[NI_MAXHOST +NI_MAXSERV + sizeof("host : ")];
290 int size= snprintf(hostname_port, sizeof(hostname_port), "host: %s:%d", self.hostname, int(self.port));
291
292 memcached_string_t error_host= { hostname_port, size};
293
294 if (self.root == NULL)
295 {
296 return rc;
297 }
298
299 _set(*self.root, &error_host, rc, at);
300 _set(self, *self.root);
301
302 return rc;
303 }
304
305 memcached_return_t memcached_set_error(memcached_st& self, memcached_return_t rc, const char *at)
306 {
307 assert_msg(rc != MEMCACHED_ERRNO, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
308 if (memcached_fatal(rc) == false)
309 {
310 return rc;
311 }
312
313 _set(self, NULL, rc, at);
314
315 return rc;
316 }
317
318 memcached_return_t memcached_set_errno(memcached_st& self, int local_errno, const char *at, const char *str, size_t length)
319 {
320 memcached_string_t tmp= { str, length };
321 return memcached_set_errno(self, local_errno, at, tmp);
322 }
323
324 memcached_return_t memcached_set_errno(memcached_server_st& self, int local_errno, const char *at, const char *str, size_t length)
325 {
326 memcached_string_t tmp= { str, length };
327 return memcached_set_errno(self, local_errno, at, tmp);
328 }
329
330 memcached_return_t memcached_set_errno(memcached_st& self, int local_errno, const char *at)
331 {
332 if (local_errno == 0)
333 {
334 return MEMCACHED_SUCCESS;
335 }
336
337 memcached_return_t rc= MEMCACHED_ERRNO;
338 _set(self, NULL, rc, at, local_errno);
339
340 return rc;
341 }
342
343 memcached_return_t memcached_set_errno(memcached_st& memc, int local_errno, const char *at, memcached_string_t& str)
344 {
345 if (local_errno == 0)
346 {
347 return MEMCACHED_SUCCESS;
348 }
349
350 memcached_return_t rc= MEMCACHED_ERRNO;
351 _set(memc, &str, rc, at, local_errno);
352
353 return rc;
354 }
355
356 memcached_return_t memcached_set_errno(memcached_server_st& self, int local_errno, const char *at, memcached_string_t& str)
357 {
358 if (local_errno == 0)
359 {
360 return MEMCACHED_SUCCESS;
361 }
362
363 char hostname_port_message[MAX_ERROR_LENGTH];
364 int size;
365 if (str.size)
366 {
367 size= snprintf(hostname_port_message, sizeof(hostname_port_message), "%.*s, host: %s:%d",
368 memcached_string_printf(str),
369 self.hostname, int(self.port));
370 }
371 else
372 {
373 size= snprintf(hostname_port_message, sizeof(hostname_port_message), "host: %s:%d",
374 self.hostname, int(self.port));
375 }
376
377 memcached_string_t error_host= { hostname_port_message, size };
378
379 memcached_return_t rc= MEMCACHED_ERRNO;
380 if (self.root == NULL)
381 {
382 return rc;
383 }
384
385 _set(*self.root, &error_host, rc, at, local_errno);
386 _set(self, (*self.root));
387
388 return rc;
389 }
390
391 memcached_return_t memcached_set_errno(memcached_server_st& self, int local_errno, const char *at)
392 {
393 if (local_errno == 0)
394 {
395 return MEMCACHED_SUCCESS;
396 }
397
398 char hostname_port_message[MAX_ERROR_LENGTH];
399 int size = snprintf(hostname_port_message, sizeof(hostname_port_message), "host: %s:%d",
400 self.hostname, int(self.port));
401
402 memcached_string_t error_host= { hostname_port_message, size };
403
404 memcached_return_t rc= MEMCACHED_ERRNO;
405 if (self.root == NULL)
406 {
407 return rc;
408 }
409
410 _set(*self.root, &error_host, rc, at, local_errno);
411 _set(self, (*self.root));
412
413 return rc;
414 }
415
416 static void _error_print(const memcached_error_t *error)
417 {
418 if (error == NULL)
419 {
420 return;
421 }
422
423 if (error->size == 0)
424 {
425 fprintf(stderr, "\t%s\n", memcached_strerror(NULL, error->rc) );
426 }
427 else
428 {
429 fprintf(stderr, "\t%s %s\n", memcached_strerror(NULL, error->rc), error->message);
430 }
431
432 _error_print(error->next);
433 }
434
435 void memcached_error_print(const memcached_st *self)
436 {
437 if (self == NULL)
438 {
439 return;
440 }
441
442 _error_print(self->error_messages);
443
444 for (uint32_t x= 0; x < memcached_server_count(self); x++)
445 {
446 memcached_server_instance_st instance= memcached_server_instance_by_position(self, x);
447
448 _error_print(instance->error_messages);
449 }
450 }
451
452 static void _error_free(memcached_error_t *error)
453 {
454 if (not error)
455 {
456 return;
457 }
458
459 _error_free(error->next);
460
461 if (error && error->root)
462 {
463 libmemcached_free(error->root, error);
464 }
465 else if (error)
466 {
467 free(error);
468 }
469 }
470
471 void memcached_error_free(memcached_st& self)
472 {
473 _error_free(self.error_messages);
474 self.error_messages= NULL;
475 }
476
477 void memcached_error_free(memcached_server_st& self)
478 {
479 _error_free(self.error_messages);
480 self.error_messages= NULL;
481 }
482
483 const char *memcached_last_error_message(memcached_st *memc)
484 {
485 if (memc == NULL)
486 {
487 return memcached_strerror(memc, MEMCACHED_INVALID_ARGUMENTS);
488 }
489
490 if (memc->error_messages == NULL)
491 {
492 return memcached_strerror(memc, MEMCACHED_SUCCESS);
493 }
494
495 if (memc->error_messages->size == 0)
496 {
497 return memcached_strerror(memc, memc->error_messages->rc);
498 }
499
500 return memc->error_messages->message;
501 }
502
503 bool memcached_has_current_error(memcached_st &memc)
504 {
505 if (memc.error_messages
506 and memc.error_messages->query_id == memc.query_id
507 and memcached_failed(memc.error_messages->rc))
508 {
509 return true;
510 }
511
512 return false;
513 }
514
515 bool memcached_has_current_error(memcached_server_st& server)
516 {
517 return memcached_has_current_error(*(server.root));
518 }
519
520 memcached_return_t memcached_last_error(memcached_st *memc)
521 {
522 if (memc == NULL)
523 {
524 return MEMCACHED_INVALID_ARGUMENTS;
525 }
526
527 if (not memc->error_messages)
528 {
529 return MEMCACHED_SUCCESS;
530 }
531
532 return memc->error_messages->rc;
533 }
534
535 int memcached_last_error_errno(memcached_st *memc)
536 {
537 if (memc == NULL)
538 {
539 return 0;
540 }
541
542 if (not memc->error_messages)
543 {
544 return 0;
545 }
546
547 return memc->error_messages->local_errno;
548 }
549
550 const char *memcached_server_error(memcached_server_instance_st server)
551 {
552 if (server == NULL)
553 {
554 return NULL;
555 }
556
557 if (not server->error_messages)
558 {
559 return memcached_strerror(server->root, MEMCACHED_SUCCESS);
560 }
561
562 if (not server->error_messages->size)
563 {
564 return memcached_strerror(server->root, server->error_messages->rc);
565 }
566
567 return server->error_messages->message;
568 }
569
570
571 memcached_error_t *memcached_error_copy(const memcached_server_st& server)
572 {
573 if (server.error_messages == NULL)
574 {
575 return NULL;
576 }
577
578 memcached_error_t *error= libmemcached_xmalloc(server.root, memcached_error_t);
579 memcpy(error, server.error_messages, sizeof(memcached_error_t));
580 error->next= NULL;
581
582 return error;
583 }
584
585 memcached_return_t memcached_server_error_return(memcached_server_instance_st ptr)
586 {
587 if (ptr == NULL)
588 {
589 return MEMCACHED_INVALID_ARGUMENTS;
590 }
591
592 if (ptr and ptr->error_messages)
593 {
594 return ptr->error_messages->rc;
595 }
596
597 return MEMCACHED_SUCCESS;
598 }