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
)
63 memcached_error_t
*error
= (struct memcached_error_t
*)libmemcached_malloc(&memc
, sizeof(struct memcached_error_t
));
64 if (not error
) // Bad business if this happens
67 memcpy(error
, memc
.error_messages
, sizeof(memcached_error_t
));
68 error
->next
= server
.error_messages
;
69 server
.error_messages
= error
;
72 static void _set(memcached_st
& memc
, memcached_string_t
*str
, memcached_return_t
&rc
, const char *at
, int local_errno
= 0)
75 if (memc
.error_messages
&& memc
.error_messages
->query_id
!= memc
.query_id
)
77 memcached_error_free(memc
);
80 // For memory allocation we use our error since it is a bit more specific
81 if (local_errno
== ENOMEM
and rc
== MEMCACHED_ERRNO
)
83 rc
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
86 if (rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
)
91 if (rc
== MEMCACHED_ERRNO
and not local_errno
)
97 if (rc
== MEMCACHED_ERRNO
and local_errno
== ENOTCONN
)
99 rc
= MEMCACHED_CONNECTION_FAILURE
;
102 if (local_errno
== EINVAL
)
104 rc
= MEMCACHED_INVALID_ARGUMENTS
;
107 if (local_errno
== ECONNREFUSED
)
109 rc
= MEMCACHED_CONNECTION_FAILURE
;
112 memcached_error_t
*error
= (struct memcached_error_t
*)libmemcached_malloc(&memc
, sizeof(struct memcached_error_t
));
113 if (not error
) // Bad business if this happens
117 error
->query_id
= memc
.query_id
;
119 error
->local_errno
= local_errno
;
121 const char *errmsg_ptr
;
122 char errmsg
[MAX_ERROR_LENGTH
];
128 #ifdef STRERROR_R_CHAR_P
129 errmsg_ptr
= strerror_r(local_errno
, errmsg
, sizeof(errmsg
));
131 strerror_r(local_errno
, errmsg
, sizeof(errmsg
));
137 if (str
and str
->size
and local_errno
)
139 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "%s(%s), %.*s -> %s",
140 memcached_strerror(&memc
, rc
),
142 memcached_string_printf(*str
), at
);
144 else if (local_errno
)
146 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "%s(%s) -> %s",
147 memcached_strerror(&memc
, rc
),
151 else if (str
and str
->size
)
153 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "%s, %.*s -> %s",
154 memcached_strerror(&memc
, rc
),
155 int(str
->size
), str
->c_str
, at
);
159 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "%s -> %s",
160 memcached_strerror(&memc
, rc
), at
);
163 error
->next
= memc
.error_messages
;
164 memc
.error_messages
= error
;
167 memcached_return_t
memcached_set_error(memcached_st
& memc
, memcached_return_t rc
, const char *at
, const char *str
, size_t length
)
169 assert(rc
!= MEMCACHED_ERRNO
);
170 memcached_string_t tmp
= { str
, length
};
171 return memcached_set_error(memc
, rc
, at
, tmp
);
174 memcached_return_t
memcached_set_error(memcached_server_st
& self
, memcached_return_t rc
, const char *at
, const char *str
, size_t length
)
176 assert(rc
!= MEMCACHED_ERRNO
);
177 assert(rc
!= MEMCACHED_SOME_ERRORS
);
178 memcached_string_t tmp
= { str
, length
};
179 return memcached_set_error(self
, rc
, at
, tmp
);
182 memcached_return_t
memcached_set_error(memcached_st
& memc
, memcached_return_t rc
, const char *at
, memcached_string_t
& str
)
184 assert(rc
!= MEMCACHED_ERRNO
);
185 if (memcached_success(rc
))
186 return MEMCACHED_SUCCESS
;
188 _set(memc
, &str
, rc
, at
);
193 memcached_return_t
memcached_set_error(memcached_server_st
& self
, memcached_return_t rc
, const char *at
, memcached_string_t
& str
)
195 assert(rc
!= MEMCACHED_ERRNO
);
196 assert(rc
!= MEMCACHED_SOME_ERRORS
);
197 if (memcached_success(rc
))
198 return MEMCACHED_SUCCESS
;
200 char hostname_port_message
[MAX_ERROR_LENGTH
];
204 size
= snprintf(hostname_port_message
, sizeof(hostname_port_message
), "%.*s, host: %s:%d",
205 memcached_string_printf(str
),
206 self
.hostname
, int(self
.port
));
210 size
= snprintf(hostname_port_message
, sizeof(hostname_port_message
), "host: %s:%d",
211 self
.hostname
, int(self
.port
));
214 memcached_string_t error_host
= { hostname_port_message
, size
};
219 _set(*self
.root
, &error_host
, rc
, at
);
220 _set(self
, (*self
.root
));
225 memcached_return_t
memcached_set_error(memcached_server_st
& self
, memcached_return_t rc
, const char *at
)
227 assert(rc
!= MEMCACHED_ERRNO
);
228 assert(rc
!= MEMCACHED_SOME_ERRORS
);
229 if (memcached_success(rc
))
230 return MEMCACHED_SUCCESS
;
232 char hostname_port
[NI_MAXHOST
+NI_MAXSERV
+ sizeof("host : ")];
233 int size
= snprintf(hostname_port
, sizeof(hostname_port
), "host: %s:%d", self
.hostname
, int(self
.port
));
235 memcached_string_t error_host
= { hostname_port
, size
};
240 _set(*self
.root
, &error_host
, rc
, at
);
241 _set(self
, *self
.root
);
246 memcached_return_t
memcached_set_error(memcached_st
& self
, memcached_return_t rc
, const char *at
)
248 assert(rc
!= MEMCACHED_ERRNO
);
249 if (memcached_success(rc
))
250 return MEMCACHED_SUCCESS
;
252 _set(self
, NULL
, rc
, at
);
257 memcached_return_t
memcached_set_errno(memcached_st
& self
, int local_errno
, const char *at
, const char *str
, size_t length
)
259 memcached_string_t tmp
= { str
, length
};
260 return memcached_set_errno(self
, local_errno
, at
, tmp
);
263 memcached_return_t
memcached_set_errno(memcached_server_st
& self
, int local_errno
, const char *at
, const char *str
, size_t length
)
265 memcached_string_t tmp
= { str
, length
};
266 return memcached_set_errno(self
, local_errno
, at
, tmp
);
269 memcached_return_t
memcached_set_errno(memcached_st
& self
, int local_errno
, const char *at
)
272 return MEMCACHED_SUCCESS
;
274 memcached_return_t rc
= MEMCACHED_ERRNO
;
275 _set(self
, NULL
, rc
, at
, local_errno
);
280 memcached_return_t
memcached_set_errno(memcached_st
& memc
, int local_errno
, const char *at
, memcached_string_t
& str
)
283 return MEMCACHED_SUCCESS
;
285 memcached_return_t rc
= MEMCACHED_ERRNO
;
286 _set(memc
, &str
, rc
, at
, local_errno
);
291 memcached_return_t
memcached_set_errno(memcached_server_st
& self
, int local_errno
, const char *at
, memcached_string_t
& str
)
294 return MEMCACHED_SUCCESS
;
296 char hostname_port_message
[MAX_ERROR_LENGTH
];
300 size
= snprintf(hostname_port_message
, sizeof(hostname_port_message
), "%.*s, host: %s:%d",
301 memcached_string_printf(str
),
302 self
.hostname
, int(self
.port
));
306 size
= snprintf(hostname_port_message
, sizeof(hostname_port_message
), "host: %s:%d",
307 self
.hostname
, int(self
.port
));
310 memcached_string_t error_host
= { hostname_port_message
, size
};
312 memcached_return_t rc
= MEMCACHED_ERRNO
;
316 _set(*self
.root
, &error_host
, rc
, at
, local_errno
);
317 _set(self
, (*self
.root
));
322 memcached_return_t
memcached_set_errno(memcached_server_st
& self
, int local_errno
, const char *at
)
325 return MEMCACHED_SUCCESS
;
327 char hostname_port_message
[MAX_ERROR_LENGTH
];
328 int size
= snprintf(hostname_port_message
, sizeof(hostname_port_message
), "host: %s:%d",
329 self
.hostname
, int(self
.port
));
331 memcached_string_t error_host
= { hostname_port_message
, size
};
333 memcached_return_t rc
= MEMCACHED_ERRNO
;
337 _set(*self
.root
, &error_host
, rc
, at
, local_errno
);
338 _set(self
, (*self
.root
));
343 static void _error_print(const memcached_error_t
*error
)
350 fprintf(stderr
, "%s\n", memcached_strerror(NULL
, error
->rc
) );
354 fprintf(stderr
, "%s %s\n", memcached_strerror(NULL
, error
->rc
), error
->message
);
357 _error_print(error
->next
);
360 void memcached_error_print(const memcached_st
*self
)
365 _error_print(self
->error_messages
);
368 static void _error_free(memcached_error_t
*error
)
373 _error_free(error
->next
);
375 if (error
&& error
->root
)
377 libmemcached_free(error
->root
, error
);
385 void memcached_error_free(memcached_st
& self
)
387 _error_free(self
.error_messages
);
388 self
.error_messages
= NULL
;
391 void memcached_error_free(memcached_server_st
& self
)
393 _error_free(self
.error_messages
);
394 self
.error_messages
= NULL
;
397 const char *memcached_last_error_message(memcached_st
*memc
)
400 return memcached_strerror(memc
, MEMCACHED_INVALID_ARGUMENTS
);
402 if (not memc
->error_messages
)
403 return memcached_strerror(memc
, MEMCACHED_SUCCESS
);
405 if (not memc
->error_messages
->size
)
406 return memcached_strerror(memc
, memc
->error_messages
->rc
);
408 return memc
->error_messages
->message
;
412 bool memcached_has_current_error(memcached_st
&memc
)
414 if (memc
.error_messages
415 and memc
.error_messages
->query_id
== memc
.query_id
416 and memcached_failed(memc
.error_messages
->rc
))
424 memcached_return_t
memcached_last_error(memcached_st
*memc
)
427 return MEMCACHED_INVALID_ARGUMENTS
;
429 if (not memc
->error_messages
)
430 return MEMCACHED_SUCCESS
;
432 return memc
->error_messages
->rc
;
435 int memcached_last_error_errno(memcached_st
*memc
)
440 if (not memc
->error_messages
)
443 return memc
->error_messages
->local_errno
;
446 const char *memcached_server_error(memcached_server_instance_st server
)
449 return memcached_strerror(server
->root
, MEMCACHED_INVALID_ARGUMENTS
);
451 if (not server
->error_messages
)
452 return memcached_strerror(server
->root
, MEMCACHED_SUCCESS
);
454 if (not server
->error_messages
->size
)
455 return memcached_strerror(server
->root
, server
->error_messages
->rc
);
457 return server
->error_messages
->message
;
461 memcached_error_t
*memcached_error_copy(const memcached_server_st
& server
)
463 if (not server
.error_messages
)
466 memcached_error_t
*error
= (memcached_error_t
*)libmemcached_malloc(server
.root
, sizeof(memcached_error_t
));
467 memcpy(error
, server
.error_messages
, sizeof(memcached_error_t
));
473 memcached_return_t
memcached_server_error_return(memcached_server_instance_st ptr
)
475 if (ptr
and ptr
->error_messages
)
477 return ptr
->error_messages
->rc
;
480 return MEMCACHED_FAILURE
;