1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
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.
38 #include <libmemcached/common.h>
41 #define MAX_ERROR_LENGTH 2048
42 struct memcached_error_t
46 struct memcached_error_t
*next
;
47 memcached_return_t rc
;
50 char message
[MAX_ERROR_LENGTH
];
53 static void _set(memcached_server_st
& server
, memcached_st
& memc
)
55 if (server
.error_messages
&& server
.error_messages
->query_id
!= server
.root
->query_id
)
57 memcached_error_free(server
);
60 if (memc
.error_messages
== NULL
)
65 memcached_error_t
*error
= (struct memcached_error_t
*)libmemcached_malloc(&memc
, sizeof(struct memcached_error_t
));
66 if (error
== NULL
) // Bad business if this happens
71 memcpy(error
, memc
.error_messages
, sizeof(memcached_error_t
));
72 error
->next
= server
.error_messages
;
73 server
.error_messages
= error
;
76 static void _set(memcached_st
& memc
, memcached_string_t
*str
, memcached_return_t
&rc
, const char *at
, int local_errno
= 0)
78 if (memc
.error_messages
&& memc
.error_messages
->query_id
!= memc
.query_id
)
80 memcached_error_free(memc
);
83 // For memory allocation we use our error since it is a bit more specific
84 if (local_errno
== ENOMEM
and rc
== MEMCACHED_ERRNO
)
86 rc
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
89 if (rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
)
94 if (rc
== MEMCACHED_ERRNO
and not local_errno
)
100 if (rc
== MEMCACHED_ERRNO
and local_errno
== ENOTCONN
)
102 rc
= MEMCACHED_CONNECTION_FAILURE
;
105 if (local_errno
== EINVAL
)
107 rc
= MEMCACHED_INVALID_ARGUMENTS
;
110 if (local_errno
== ECONNREFUSED
)
112 rc
= MEMCACHED_CONNECTION_FAILURE
;
115 memcached_error_t
*error
= (struct memcached_error_t
*)libmemcached_malloc(&memc
, sizeof(struct memcached_error_t
));
116 if (error
== NULL
) // Bad business if this happens
122 error
->query_id
= memc
.query_id
;
124 error
->local_errno
= local_errno
;
126 const char *errmsg_ptr
;
127 char errmsg
[MAX_ERROR_LENGTH
];
133 #ifdef STRERROR_R_CHAR_P
134 errmsg_ptr
= strerror_r(local_errno
, errmsg
, sizeof(errmsg
));
136 strerror_r(local_errno
, errmsg
, sizeof(errmsg
));
142 if (str
and str
->size
and local_errno
)
144 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "%s(%s), %.*s -> %s",
145 memcached_strerror(&memc
, rc
),
147 memcached_string_printf(*str
), at
);
149 else if (local_errno
)
151 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "%s(%s) -> %s",
152 memcached_strerror(&memc
, rc
),
156 else if (rc
== MEMCACHED_PARSE_ERROR
and str
and str
->size
)
158 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "%.*s -> %s",
159 int(str
->size
), str
->c_str
, at
);
161 else if (str
and str
->size
)
163 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "%s, %.*s -> %s",
164 memcached_strerror(&memc
, rc
),
165 int(str
->size
), str
->c_str
, at
);
169 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "%s -> %s",
170 memcached_strerror(&memc
, rc
), at
);
173 error
->next
= memc
.error_messages
;
174 memc
.error_messages
= error
;
177 memcached_return_t
memcached_set_error(memcached_st
& memc
, memcached_return_t rc
, const char *at
, const char *str
, size_t length
)
179 assert_msg(rc
!= MEMCACHED_ERRNO
, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
180 memcached_string_t tmp
= { str
, length
};
181 return memcached_set_error(memc
, rc
, at
, tmp
);
184 memcached_return_t
memcached_set_error(memcached_server_st
& self
, memcached_return_t rc
, const char *at
, const char *str
, size_t length
)
186 assert_msg(rc
!= MEMCACHED_ERRNO
, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
187 assert_msg(rc
!= MEMCACHED_SOME_ERRORS
, "Programmer error, MEMCACHED_SOME_ERRORS was about to be set on a memcached_server_st");
188 memcached_string_t tmp
= { str
, length
};
189 return memcached_set_error(self
, rc
, at
, tmp
);
192 memcached_return_t
memcached_set_error(memcached_st
& memc
, memcached_return_t rc
, const char *at
, memcached_string_t
& str
)
194 assert_msg(rc
!= MEMCACHED_ERRNO
, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
195 if (memcached_success(rc
))
200 _set(memc
, &str
, rc
, at
);
205 memcached_return_t
memcached_set_parser_error(memcached_st
& memc
,
207 const char *format
, ...)
212 va_start(args
, format
);
213 int length
= vsnprintf(buffer
, sizeof(buffer
), format
, args
);
216 return memcached_set_error(memc
, MEMCACHED_PARSE_ERROR
, at
, buffer
, length
);
219 memcached_return_t
memcached_set_error(memcached_server_st
& self
, memcached_return_t rc
, const char *at
, memcached_string_t
& str
)
221 assert_msg(rc
!= MEMCACHED_ERRNO
, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
222 assert_msg(rc
!= MEMCACHED_SOME_ERRORS
, "Programmer error, MEMCACHED_SOME_ERRORS was about to be set on a memcached_server_st");
223 if (memcached_success(rc
))
228 char hostname_port_message
[MAX_ERROR_LENGTH
];
232 size
= snprintf(hostname_port_message
, sizeof(hostname_port_message
), "%.*s, host: %s:%d",
233 memcached_string_printf(str
),
234 self
.hostname
, int(self
.port
));
238 size
= snprintf(hostname_port_message
, sizeof(hostname_port_message
), "host: %s:%d",
239 self
.hostname
, int(self
.port
));
242 memcached_string_t error_host
= { hostname_port_message
, size
};
245 if (self
.root
== NULL
)
250 _set(*self
.root
, &error_host
, rc
, at
);
251 _set(self
, (*self
.root
));
252 assert(self
.root
->error_messages
);
253 assert(self
.error_messages
);
258 memcached_return_t
memcached_set_error(memcached_server_st
& self
, memcached_return_t rc
, const char *at
)
260 assert_msg(rc
!= MEMCACHED_SOME_ERRORS
, "Programmer error, MEMCACHED_SOME_ERRORS was about to be set on a memcached_server_st");
261 if (memcached_success(rc
))
266 char hostname_port
[NI_MAXHOST
+NI_MAXSERV
+ sizeof("host : ")];
267 int size
= snprintf(hostname_port
, sizeof(hostname_port
), "host: %s:%d", self
.hostname
, int(self
.port
));
269 memcached_string_t error_host
= { hostname_port
, size
};
271 if (self
.root
== NULL
)
276 _set(*self
.root
, &error_host
, rc
, at
);
277 _set(self
, *self
.root
);
282 memcached_return_t
memcached_set_error(memcached_st
& self
, memcached_return_t rc
, const char *at
)
284 assert_msg(rc
!= MEMCACHED_ERRNO
, "Programmer error, MEMCACHED_ERRNO was set to be returned to client");
285 if (memcached_success(rc
))
290 _set(self
, NULL
, rc
, at
);
295 memcached_return_t
memcached_set_errno(memcached_st
& self
, int local_errno
, const char *at
, const char *str
, size_t length
)
297 memcached_string_t tmp
= { str
, length
};
298 return memcached_set_errno(self
, local_errno
, at
, tmp
);
301 memcached_return_t
memcached_set_errno(memcached_server_st
& self
, int local_errno
, const char *at
, const char *str
, size_t length
)
303 memcached_string_t tmp
= { str
, length
};
304 return memcached_set_errno(self
, local_errno
, at
, tmp
);
307 memcached_return_t
memcached_set_errno(memcached_st
& self
, int local_errno
, const char *at
)
309 if (local_errno
== 0)
311 return MEMCACHED_SUCCESS
;
314 memcached_return_t rc
= MEMCACHED_ERRNO
;
315 _set(self
, NULL
, rc
, at
, local_errno
);
320 memcached_return_t
memcached_set_errno(memcached_st
& memc
, int local_errno
, const char *at
, memcached_string_t
& str
)
322 if (local_errno
== 0)
324 return MEMCACHED_SUCCESS
;
327 memcached_return_t rc
= MEMCACHED_ERRNO
;
328 _set(memc
, &str
, rc
, at
, local_errno
);
333 memcached_return_t
memcached_set_errno(memcached_server_st
& self
, int local_errno
, const char *at
, memcached_string_t
& str
)
335 if (local_errno
== 0)
337 return MEMCACHED_SUCCESS
;
340 char hostname_port_message
[MAX_ERROR_LENGTH
];
344 size
= snprintf(hostname_port_message
, sizeof(hostname_port_message
), "%.*s, host: %s:%d",
345 memcached_string_printf(str
),
346 self
.hostname
, int(self
.port
));
350 size
= snprintf(hostname_port_message
, sizeof(hostname_port_message
), "host: %s:%d",
351 self
.hostname
, int(self
.port
));
354 memcached_string_t error_host
= { hostname_port_message
, size
};
356 memcached_return_t rc
= MEMCACHED_ERRNO
;
360 _set(*self
.root
, &error_host
, rc
, at
, local_errno
);
361 _set(self
, (*self
.root
));
366 memcached_return_t
memcached_set_errno(memcached_server_st
& self
, int local_errno
, const char *at
)
368 if (local_errno
== 0)
370 return MEMCACHED_SUCCESS
;
373 char hostname_port_message
[MAX_ERROR_LENGTH
];
374 int size
= snprintf(hostname_port_message
, sizeof(hostname_port_message
), "host: %s:%d",
375 self
.hostname
, int(self
.port
));
377 memcached_string_t error_host
= { hostname_port_message
, size
};
379 memcached_return_t rc
= MEMCACHED_ERRNO
;
383 _set(*self
.root
, &error_host
, rc
, at
, local_errno
);
384 _set(self
, (*self
.root
));
389 static void _error_print(const memcached_error_t
*error
)
396 if (error
->size
== 0)
398 fprintf(stderr
, "%s\n", memcached_strerror(NULL
, error
->rc
) );
402 fprintf(stderr
, "%s %s\n", memcached_strerror(NULL
, error
->rc
), error
->message
);
405 _error_print(error
->next
);
408 void memcached_error_print(const memcached_st
*self
)
413 _error_print(self
->error_messages
);
416 static void _error_free(memcached_error_t
*error
)
423 _error_free(error
->next
);
425 if (error
&& error
->root
)
427 libmemcached_free(error
->root
, error
);
435 void memcached_error_free(memcached_st
& self
)
437 _error_free(self
.error_messages
);
438 self
.error_messages
= NULL
;
441 void memcached_error_free(memcached_server_st
& self
)
443 _error_free(self
.error_messages
);
444 self
.error_messages
= NULL
;
447 const char *memcached_last_error_message(memcached_st
*memc
)
451 return memcached_strerror(memc
, MEMCACHED_INVALID_ARGUMENTS
);
454 if (memc
->error_messages
== NULL
)
456 return memcached_strerror(memc
, MEMCACHED_SUCCESS
);
459 if (memc
->error_messages
->size
== 0)
461 return memcached_strerror(memc
, memc
->error_messages
->rc
);
464 return memc
->error_messages
->message
;
467 bool memcached_has_current_error(memcached_st
&memc
)
469 if (memc
.error_messages
470 and memc
.error_messages
->query_id
== memc
.query_id
471 and memcached_failed(memc
.error_messages
->rc
))
479 bool memcached_has_current_error(memcached_server_st
& server
)
481 return memcached_has_current_error(*(server
.root
));
484 memcached_return_t
memcached_last_error(memcached_st
*memc
)
488 return MEMCACHED_INVALID_ARGUMENTS
;
491 if (not memc
->error_messages
)
492 return MEMCACHED_SUCCESS
;
494 return memc
->error_messages
->rc
;
497 int memcached_last_error_errno(memcached_st
*memc
)
504 if (not memc
->error_messages
)
509 return memc
->error_messages
->local_errno
;
512 const char *memcached_server_error(memcached_server_instance_st server
)
519 if (not server
->error_messages
)
520 return memcached_strerror(server
->root
, MEMCACHED_SUCCESS
);
522 if (not server
->error_messages
->size
)
523 return memcached_strerror(server
->root
, server
->error_messages
->rc
);
525 return server
->error_messages
->message
;
529 memcached_error_t
*memcached_error_copy(const memcached_server_st
& server
)
531 if (server
.error_messages
== NULL
)
536 memcached_error_t
*error
= (memcached_error_t
*)libmemcached_malloc(server
.root
, sizeof(memcached_error_t
));
537 memcpy(error
, server
.error_messages
, sizeof(memcached_error_t
));
543 memcached_return_t
memcached_server_error_return(memcached_server_instance_st ptr
)
547 return MEMCACHED_INVALID_ARGUMENTS
;
550 if (ptr
and ptr
->error_messages
)
552 return ptr
->error_messages
->rc
;
555 return MEMCACHED_SUCCESS
;