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_st
& memc
, memcached_string_t
*str
, memcached_return_t
&rc
, const char *at
, int local_errno
= 0)
56 if (memc
.error_messages
&& memc
.error_messages
->query_id
!= memc
.query_id
)
58 memcached_error_free(&memc
);
61 // For memory allocation we use our error since it is a bit more specific
62 if (local_errno
== ENOMEM
and rc
== MEMCACHED_ERRNO
)
64 rc
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
67 if (rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
)
72 if (rc
== MEMCACHED_ERRNO
and not local_errno
)
78 if (rc
== MEMCACHED_ERRNO
and local_errno
== ENOTCONN
)
80 rc
= MEMCACHED_CONNECTION_FAILURE
;
83 if (local_errno
== EINVAL
)
85 rc
= MEMCACHED_INVALID_ARGUMENTS
;
88 if (local_errno
== ECONNREFUSED
)
90 rc
= MEMCACHED_CONNECTION_FAILURE
;
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
98 error
->query_id
= memc
.query_id
;
100 error
->local_errno
= local_errno
;
102 const char *errmsg_ptr
;
103 char errmsg
[MAX_ERROR_LENGTH
];
109 #ifdef STRERROR_R_CHAR_P
110 errmsg_ptr
= strerror_r(local_errno
, errmsg
, sizeof(errmsg
));
112 strerror_r(local_errno
, errmsg
, sizeof(errmsg
));
118 if (str
and str
->size
and local_errno
)
120 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "%s(%s), %.*s -> %s",
121 memcached_strerror(&memc
, rc
),
123 memcached_string_printf(*str
), at
);
125 else if (local_errno
)
127 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "%s(%s) -> %s",
128 memcached_strerror(&memc
, rc
),
132 else if (str
and str
->size
)
134 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "%s, %.*s -> %s",
135 memcached_strerror(&memc
, rc
),
136 int(str
->size
), str
->c_str
, at
);
140 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "%s -> %s",
141 memcached_strerror(&memc
, rc
), at
);
144 error
->next
= memc
.error_messages
;
145 memc
.error_messages
= error
;
148 memcached_return_t
memcached_set_error(memcached_st
& memc
, memcached_return_t rc
, const char *at
, const char *str
, size_t length
)
150 assert(rc
!= MEMCACHED_ERRNO
);
151 memcached_string_t tmp
= { str
, length
};
152 return memcached_set_error(memc
, rc
, at
, tmp
);
155 memcached_return_t
memcached_set_error(memcached_server_st
& self
, memcached_return_t rc
, const char *at
, const char *str
, size_t length
)
157 assert(rc
!= MEMCACHED_ERRNO
);
158 memcached_string_t tmp
= { str
, length
};
159 return memcached_set_error(self
, rc
, at
, tmp
);
162 memcached_return_t
memcached_set_error(memcached_st
& memc
, memcached_return_t rc
, const char *at
, memcached_string_t
& str
)
164 assert(rc
!= MEMCACHED_ERRNO
);
165 if (memcached_success(rc
))
166 return MEMCACHED_SUCCESS
;
168 _set(memc
, &str
, rc
, at
);
173 memcached_return_t
memcached_set_error(memcached_server_st
& self
, memcached_return_t rc
, const char *at
, memcached_string_t
& str
)
175 assert(rc
!= MEMCACHED_ERRNO
);
176 if (memcached_success(rc
))
177 return MEMCACHED_SUCCESS
;
179 char hostname_port_message
[MAX_ERROR_LENGTH
];
183 size
= snprintf(hostname_port_message
, sizeof(hostname_port_message
), "%.*s, host: %s:%d",
184 memcached_string_printf(str
),
185 self
.hostname
, int(self
.port
));
189 size
= snprintf(hostname_port_message
, sizeof(hostname_port_message
), "host: %s:%d",
190 self
.hostname
, int(self
.port
));
193 memcached_string_t error_host
= { hostname_port_message
, size
};
198 _set(*self
.root
, &error_host
, rc
, at
);
203 memcached_return_t
memcached_set_error(memcached_server_st
& self
, memcached_return_t rc
, const char *at
)
205 assert(rc
!= MEMCACHED_ERRNO
);
206 if (memcached_success(rc
))
207 return MEMCACHED_SUCCESS
;
209 char hostname_port
[NI_MAXHOST
+NI_MAXSERV
+ sizeof("host : ")];
210 int size
= snprintf(hostname_port
, sizeof(hostname_port
), "host: %s:%d", self
.hostname
, int(self
.port
));
212 memcached_string_t error_host
= { hostname_port
, size
};
217 _set(*self
.root
, &error_host
, rc
, at
);
222 memcached_return_t
memcached_set_error(memcached_st
& self
, memcached_return_t rc
, const char *at
)
224 assert(rc
!= MEMCACHED_ERRNO
);
225 if (memcached_success(rc
))
226 return MEMCACHED_SUCCESS
;
228 _set(self
, NULL
, rc
, at
);
233 memcached_return_t
memcached_set_errno(memcached_st
& self
, int local_errno
, const char *at
, const char *str
, size_t length
)
235 memcached_string_t tmp
= { str
, length
};
236 return memcached_set_errno(self
, local_errno
, at
, tmp
);
239 memcached_return_t
memcached_set_errno(memcached_server_st
& self
, int local_errno
, const char *at
, const char *str
, size_t length
)
241 memcached_string_t tmp
= { str
, length
};
242 return memcached_set_errno(self
, local_errno
, at
, tmp
);
245 memcached_return_t
memcached_set_errno(memcached_st
& self
, int local_errno
, const char *at
)
248 return MEMCACHED_SUCCESS
;
250 memcached_return_t rc
= MEMCACHED_ERRNO
;
251 _set(self
, NULL
, rc
, at
, local_errno
);
256 memcached_return_t
memcached_set_errno(memcached_st
& memc
, int local_errno
, const char *at
, memcached_string_t
& str
)
259 return MEMCACHED_SUCCESS
;
261 memcached_return_t rc
= MEMCACHED_ERRNO
;
262 _set(memc
, &str
, rc
, at
, local_errno
);
267 memcached_return_t
memcached_set_errno(memcached_server_st
& self
, int local_errno
, const char *at
, memcached_string_t
& str
)
270 return MEMCACHED_SUCCESS
;
272 char hostname_port_message
[MAX_ERROR_LENGTH
];
276 size
= snprintf(hostname_port_message
, sizeof(hostname_port_message
), "%.*s, host: %s:%d",
277 memcached_string_printf(str
),
278 self
.hostname
, int(self
.port
));
282 size
= snprintf(hostname_port_message
, sizeof(hostname_port_message
), "host: %s:%d",
283 self
.hostname
, int(self
.port
));
286 memcached_string_t error_host
= { hostname_port_message
, size
};
288 self
.cached_errno
= local_errno
; // Store in the actual server
290 memcached_return_t rc
= MEMCACHED_ERRNO
;
294 _set(*self
.root
, &error_host
, rc
, at
, local_errno
);
299 memcached_return_t
memcached_set_errno(memcached_server_st
& self
, int local_errno
, const char *at
)
302 return MEMCACHED_SUCCESS
;
304 char hostname_port_message
[MAX_ERROR_LENGTH
];
305 int size
= snprintf(hostname_port_message
, sizeof(hostname_port_message
), "host: %s:%d",
306 self
.hostname
, int(self
.port
));
308 memcached_string_t error_host
= { hostname_port_message
, size
};
310 self
.cached_errno
= local_errno
; // Store in the actual server
312 memcached_return_t rc
= MEMCACHED_ERRNO
;
316 _set(*self
.root
, &error_host
, rc
, at
, local_errno
);
321 static void _error_print(const memcached_error_t
*error
)
328 fprintf(stderr
, "%s\n", memcached_strerror(NULL
, error
->rc
) );
332 fprintf(stderr
, "%s %s\n", memcached_strerror(NULL
, error
->rc
), error
->message
);
335 _error_print(error
->next
);
338 void memcached_error_print(const memcached_st
*self
)
343 _error_print(self
->error_messages
);
346 static void _error_free(memcached_error_t
*error
)
351 _error_free(error
->next
);
353 if (error
&& error
->root
)
355 libmemcached_free(error
->root
, error
);
363 void memcached_error_free(memcached_st
*self
)
368 _error_free(self
->error_messages
);
369 self
->error_messages
= NULL
;
372 const char *memcached_last_error_message(memcached_st
*memc
)
375 return memcached_strerror(memc
, MEMCACHED_INVALID_ARGUMENTS
);
377 if (not memc
->error_messages
)
378 return memcached_strerror(memc
, MEMCACHED_SUCCESS
);
380 if (not memc
->error_messages
->size
)
381 return memcached_strerror(memc
, memc
->error_messages
->rc
);
383 return memc
->error_messages
->message
;
387 bool memcached_has_current_error(memcached_st
&memc
)
389 if (memc
.error_messages
390 and memc
.error_messages
->query_id
== memc
.query_id
391 and memcached_failed(memc
.error_messages
->rc
))
399 memcached_return_t
memcached_last_error(memcached_st
*memc
)
402 return MEMCACHED_INVALID_ARGUMENTS
;
404 if (not memc
->error_messages
)
405 return MEMCACHED_SUCCESS
;
407 return memc
->error_messages
->rc
;
410 int memcached_last_error_errno(memcached_st
*memc
)
415 if (not memc
->error_messages
)
418 return memc
->error_messages
->local_errno
;