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
)
65 rc
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
68 if (rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
)
73 if (rc
== MEMCACHED_ERRNO
and not local_errno
)
79 if (rc
== MEMCACHED_ERRNO
and local_errno
== ENOTCONN
)
81 rc
= MEMCACHED_CONNECTION_FAILURE
;
84 memcached_error_t
*error
= (struct memcached_error_t
*)libmemcached_malloc(&memc
, sizeof(struct memcached_error_t
));
85 if (not error
) // Bad business if this happens
89 error
->query_id
= memc
.query_id
;
91 error
->local_errno
= local_errno
;
93 if (str
and str
->size
and local_errno
)
95 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "%s(%s), %.*s -> %s",
96 memcached_strerror(&memc
, rc
),
97 strerror(local_errno
),
98 memcached_string_printf(*str
), at
);
100 else if (local_errno
)
102 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "%s(%s) -> %s",
103 memcached_strerror(&memc
, rc
),
104 strerror(local_errno
), at
);
106 else if (str
and str
->size
)
108 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "%s, %.*s -> %s",
109 memcached_strerror(&memc
, rc
),
110 int(str
->size
), str
->c_str
, at
);
114 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "%s -> %s",
115 memcached_strerror(&memc
, rc
), at
);
118 error
->next
= memc
.error_messages
;
119 memc
.error_messages
= error
;
122 memcached_return_t
memcached_set_error(memcached_st
& memc
, memcached_return_t rc
, const char *at
, const char *str
, size_t length
)
124 assert(rc
!= MEMCACHED_ERRNO
);
125 memcached_string_t tmp
= { str
, length
};
126 return memcached_set_error(memc
, rc
, at
, tmp
);
129 memcached_return_t
memcached_set_error(memcached_server_st
& self
, memcached_return_t rc
, const char *at
, const char *str
, size_t length
)
131 assert(rc
!= MEMCACHED_ERRNO
);
132 memcached_string_t tmp
= { str
, length
};
133 return memcached_set_error(self
, rc
, at
, tmp
);
136 memcached_return_t
memcached_set_error(memcached_st
& memc
, memcached_return_t rc
, const char *at
, memcached_string_t
& str
)
138 assert(rc
!= MEMCACHED_ERRNO
);
139 if (memcached_success(rc
))
140 return MEMCACHED_SUCCESS
;
142 _set(memc
, &str
, rc
, at
);
147 memcached_return_t
memcached_set_error(memcached_server_st
& self
, memcached_return_t rc
, const char *at
, memcached_string_t
& str
)
149 assert(rc
!= MEMCACHED_ERRNO
);
150 if (memcached_success(rc
))
151 return MEMCACHED_SUCCESS
;
153 char hostname_port_message
[MAX_ERROR_LENGTH
];
157 size
= snprintf(hostname_port_message
, sizeof(hostname_port_message
), "%.*s, host: %s:%d",
158 memcached_string_printf(str
),
159 self
.hostname
, int(self
.port
));
163 size
= snprintf(hostname_port_message
, sizeof(hostname_port_message
), "host: %s:%d",
164 self
.hostname
, int(self
.port
));
167 memcached_string_t error_host
= { hostname_port_message
, size
};
172 _set(*self
.root
, &error_host
, rc
, at
);
177 memcached_return_t
memcached_set_error(memcached_server_st
& self
, memcached_return_t rc
, const char *at
)
179 assert(rc
!= MEMCACHED_ERRNO
);
180 if (memcached_success(rc
))
181 return MEMCACHED_SUCCESS
;
183 char hostname_port
[NI_MAXHOST
+NI_MAXSERV
+ sizeof("host : ")];
184 int size
= snprintf(hostname_port
, sizeof(hostname_port
), "host: %s:%d", self
.hostname
, int(self
.port
));
186 memcached_string_t error_host
= { hostname_port
, size
};
191 _set(*self
.root
, &error_host
, rc
, at
);
196 memcached_return_t
memcached_set_error(memcached_st
& self
, memcached_return_t rc
, const char *at
)
198 assert(rc
!= MEMCACHED_ERRNO
);
199 if (memcached_success(rc
))
200 return MEMCACHED_SUCCESS
;
202 _set(self
, NULL
, rc
, at
);
207 memcached_return_t
memcached_set_errno(memcached_st
& self
, int local_errno
, const char *at
, const char *str
, size_t length
)
209 memcached_string_t tmp
= { str
, length
};
210 return memcached_set_errno(self
, local_errno
, at
, tmp
);
213 memcached_return_t
memcached_set_errno(memcached_server_st
& self
, int local_errno
, const char *at
, const char *str
, size_t length
)
215 memcached_string_t tmp
= { str
, length
};
216 return memcached_set_errno(self
, local_errno
, at
, tmp
);
219 memcached_return_t
memcached_set_errno(memcached_st
& self
, int local_errno
, const char *at
)
222 return MEMCACHED_SUCCESS
;
224 memcached_return_t rc
= MEMCACHED_ERRNO
;
225 _set(self
, NULL
, rc
, at
, local_errno
);
230 memcached_return_t
memcached_set_errno(memcached_st
& memc
, int local_errno
, const char *at
, memcached_string_t
& str
)
233 return MEMCACHED_SUCCESS
;
235 memcached_return_t rc
= MEMCACHED_ERRNO
;
236 _set(memc
, &str
, rc
, at
, local_errno
);
241 memcached_return_t
memcached_set_errno(memcached_server_st
& self
, int local_errno
, const char *at
, memcached_string_t
& str
)
244 return MEMCACHED_SUCCESS
;
246 char hostname_port_message
[MAX_ERROR_LENGTH
];
250 size
= snprintf(hostname_port_message
, sizeof(hostname_port_message
), "%.*s, host: %s:%d",
251 memcached_string_printf(str
),
252 self
.hostname
, int(self
.port
));
256 size
= snprintf(hostname_port_message
, sizeof(hostname_port_message
), "host: %s:%d",
257 self
.hostname
, int(self
.port
));
260 memcached_string_t error_host
= { hostname_port_message
, size
};
262 self
.cached_errno
= local_errno
; // Store in the actual server
264 memcached_return_t rc
= MEMCACHED_ERRNO
;
268 _set(*self
.root
, &error_host
, rc
, at
, local_errno
);
273 memcached_return_t
memcached_set_errno(memcached_server_st
& self
, int local_errno
, const char *at
)
276 return MEMCACHED_SUCCESS
;
278 char hostname_port_message
[MAX_ERROR_LENGTH
];
279 int size
= snprintf(hostname_port_message
, sizeof(hostname_port_message
), "host: %s:%d",
280 self
.hostname
, int(self
.port
));
282 memcached_string_t error_host
= { hostname_port_message
, size
};
284 self
.cached_errno
= local_errno
; // Store in the actual server
286 memcached_return_t rc
= MEMCACHED_ERRNO
;
290 _set(*self
.root
, &error_host
, rc
, at
, local_errno
);
295 static void _error_print(const memcached_error_t
*error
)
302 fprintf(stderr
, "%s\n", memcached_strerror(NULL
, error
->rc
) );
306 fprintf(stderr
, "%s %s\n", memcached_strerror(NULL
, error
->rc
), error
->message
);
309 _error_print(error
->next
);
312 void memcached_error_print(const memcached_st
*self
)
317 _error_print(self
->error_messages
);
320 static void _error_free(memcached_error_t
*error
)
325 _error_free(error
->next
);
327 if (error
&& error
->root
)
329 libmemcached_free(error
->root
, error
);
337 void memcached_error_free(memcached_st
*self
)
342 _error_free(self
->error_messages
);
343 self
->error_messages
= NULL
;
346 const char *memcached_last_error_message(memcached_st
*memc
)
349 return memcached_strerror(memc
, MEMCACHED_INVALID_ARGUMENTS
);
351 if (not memc
->error_messages
)
352 return memcached_strerror(memc
, MEMCACHED_SUCCESS
);
354 if (not memc
->error_messages
->size
)
355 return memcached_strerror(memc
, memc
->error_messages
->rc
);
357 return memc
->error_messages
->message
;
361 bool memcached_has_current_error(memcached_st
&memc
)
363 if (memc
.error_messages
364 and memc
.error_messages
->query_id
== memc
.query_id
365 and memcached_failed(memc
.error_messages
->rc
))
373 memcached_return_t
memcached_last_error(memcached_st
*memc
)
376 return MEMCACHED_INVALID_ARGUMENTS
;
378 if (not memc
->error_messages
)
379 return MEMCACHED_SUCCESS
;
381 return memc
->error_messages
->rc
;
384 int memcached_last_error_errno(memcached_st
*memc
)
389 if (not memc
->error_messages
)
392 return memc
->error_messages
->local_errno
;