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 if (str
and str
->size
and local_errno
)
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
);
109 else if (local_errno
)
111 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "%s(%s) -> %s",
112 memcached_strerror(&memc
, rc
),
113 strerror(local_errno
), at
);
115 else if (str
and str
->size
)
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
);
123 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "%s -> %s",
124 memcached_strerror(&memc
, rc
), at
);
127 error
->next
= memc
.error_messages
;
128 memc
.error_messages
= error
;
131 memcached_return_t
memcached_set_error(memcached_st
& memc
, memcached_return_t rc
, const char *at
, const char *str
, size_t length
)
133 assert(rc
!= MEMCACHED_ERRNO
);
134 memcached_string_t tmp
= { str
, length
};
135 return memcached_set_error(memc
, rc
, at
, tmp
);
138 memcached_return_t
memcached_set_error(memcached_server_st
& self
, memcached_return_t rc
, const char *at
, const char *str
, size_t length
)
140 assert(rc
!= MEMCACHED_ERRNO
);
141 memcached_string_t tmp
= { str
, length
};
142 return memcached_set_error(self
, rc
, at
, tmp
);
145 memcached_return_t
memcached_set_error(memcached_st
& memc
, memcached_return_t rc
, const char *at
, memcached_string_t
& str
)
147 assert(rc
!= MEMCACHED_ERRNO
);
148 if (memcached_success(rc
))
149 return MEMCACHED_SUCCESS
;
151 _set(memc
, &str
, rc
, at
);
156 memcached_return_t
memcached_set_error(memcached_server_st
& self
, memcached_return_t rc
, const char *at
, memcached_string_t
& str
)
158 assert(rc
!= MEMCACHED_ERRNO
);
159 if (memcached_success(rc
))
160 return MEMCACHED_SUCCESS
;
162 char hostname_port_message
[MAX_ERROR_LENGTH
];
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
));
172 size
= snprintf(hostname_port_message
, sizeof(hostname_port_message
), "host: %s:%d",
173 self
.hostname
, int(self
.port
));
176 memcached_string_t error_host
= { hostname_port_message
, size
};
181 _set(*self
.root
, &error_host
, rc
, at
);
186 memcached_return_t
memcached_set_error(memcached_server_st
& self
, memcached_return_t rc
, const char *at
)
188 assert(rc
!= MEMCACHED_ERRNO
);
189 if (memcached_success(rc
))
190 return MEMCACHED_SUCCESS
;
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
));
195 memcached_string_t error_host
= { hostname_port
, size
};
200 _set(*self
.root
, &error_host
, rc
, at
);
205 memcached_return_t
memcached_set_error(memcached_st
& self
, memcached_return_t rc
, const char *at
)
207 assert(rc
!= MEMCACHED_ERRNO
);
208 if (memcached_success(rc
))
209 return MEMCACHED_SUCCESS
;
211 _set(self
, NULL
, rc
, at
);
216 memcached_return_t
memcached_set_errno(memcached_st
& self
, int local_errno
, const char *at
, const char *str
, size_t length
)
218 memcached_string_t tmp
= { str
, length
};
219 return memcached_set_errno(self
, local_errno
, at
, tmp
);
222 memcached_return_t
memcached_set_errno(memcached_server_st
& self
, int local_errno
, const char *at
, const char *str
, size_t length
)
224 memcached_string_t tmp
= { str
, length
};
225 return memcached_set_errno(self
, local_errno
, at
, tmp
);
228 memcached_return_t
memcached_set_errno(memcached_st
& self
, int local_errno
, const char *at
)
231 return MEMCACHED_SUCCESS
;
233 memcached_return_t rc
= MEMCACHED_ERRNO
;
234 _set(self
, NULL
, rc
, at
, local_errno
);
239 memcached_return_t
memcached_set_errno(memcached_st
& memc
, int local_errno
, const char *at
, memcached_string_t
& str
)
242 return MEMCACHED_SUCCESS
;
244 memcached_return_t rc
= MEMCACHED_ERRNO
;
245 _set(memc
, &str
, rc
, at
, local_errno
);
250 memcached_return_t
memcached_set_errno(memcached_server_st
& self
, int local_errno
, const char *at
, memcached_string_t
& str
)
253 return MEMCACHED_SUCCESS
;
255 char hostname_port_message
[MAX_ERROR_LENGTH
];
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
));
265 size
= snprintf(hostname_port_message
, sizeof(hostname_port_message
), "host: %s:%d",
266 self
.hostname
, int(self
.port
));
269 memcached_string_t error_host
= { hostname_port_message
, size
};
271 self
.cached_errno
= local_errno
; // Store in the actual server
273 memcached_return_t rc
= MEMCACHED_ERRNO
;
277 _set(*self
.root
, &error_host
, rc
, at
, local_errno
);
282 memcached_return_t
memcached_set_errno(memcached_server_st
& self
, int local_errno
, const char *at
)
285 return MEMCACHED_SUCCESS
;
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
));
291 memcached_string_t error_host
= { hostname_port_message
, size
};
293 self
.cached_errno
= local_errno
; // Store in the actual server
295 memcached_return_t rc
= MEMCACHED_ERRNO
;
299 _set(*self
.root
, &error_host
, rc
, at
, local_errno
);
304 static void _error_print(const memcached_error_t
*error
)
311 fprintf(stderr
, "%s\n", memcached_strerror(NULL
, error
->rc
) );
315 fprintf(stderr
, "%s %s\n", memcached_strerror(NULL
, error
->rc
), error
->message
);
318 _error_print(error
->next
);
321 void memcached_error_print(const memcached_st
*self
)
326 _error_print(self
->error_messages
);
329 static void _error_free(memcached_error_t
*error
)
334 _error_free(error
->next
);
336 if (error
&& error
->root
)
338 libmemcached_free(error
->root
, error
);
346 void memcached_error_free(memcached_st
*self
)
351 _error_free(self
->error_messages
);
352 self
->error_messages
= NULL
;
355 const char *memcached_last_error_message(memcached_st
*memc
)
358 return memcached_strerror(memc
, MEMCACHED_INVALID_ARGUMENTS
);
360 if (not memc
->error_messages
)
361 return memcached_strerror(memc
, MEMCACHED_SUCCESS
);
363 if (not memc
->error_messages
->size
)
364 return memcached_strerror(memc
, memc
->error_messages
->rc
);
366 return memc
->error_messages
->message
;
370 bool memcached_has_current_error(memcached_st
&memc
)
372 if (memc
.error_messages
373 and memc
.error_messages
->query_id
== memc
.query_id
374 and memcached_failed(memc
.error_messages
->rc
))
382 memcached_return_t
memcached_last_error(memcached_st
*memc
)
385 return MEMCACHED_INVALID_ARGUMENTS
;
387 if (not memc
->error_messages
)
388 return MEMCACHED_SUCCESS
;
390 return memc
->error_messages
->rc
;
393 int memcached_last_error_errno(memcached_st
*memc
)
398 if (not memc
->error_messages
)
401 return memc
->error_messages
->local_errno
;