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>
40 #define MAX_ERROR_LENGTH 2048
41 struct memcached_error_t
45 struct memcached_error_t
*next
;
46 memcached_return_t rc
;
49 char message
[MAX_ERROR_LENGTH
];
52 static void _set(memcached_st
& memc
, memcached_string_t
*str
, memcached_return_t
&rc
, const char *at
, int local_errno
= 0)
55 if (memc
.error_messages
&& memc
.error_messages
->query_id
!= memc
.query_id
)
57 memcached_error_free(&memc
);
60 // For memory allocation we use our error since it is a bit more specific
61 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 memcached_error_t
*error
= (struct memcached_error_t
*)libmemcached_malloc(&memc
, sizeof(struct memcached_error_t
));
84 if (not error
) // Bad business if this happens
88 error
->query_id
= memc
.query_id
;
90 error
->local_errno
= local_errno
;
92 if (str
and local_errno
)
94 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "%s(%s), %.*s -> %s",
95 memcached_strerror(&memc
, rc
),
96 strerror(local_errno
),
97 int(error
->size
), str
->c_str
, at
);
101 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "%s(%s) -> %s",
102 memcached_strerror(&memc
, rc
),
103 strerror(local_errno
), at
);
107 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "%s, %.*s -> %s",
108 memcached_strerror(&memc
, rc
),
109 int(error
->size
), str
->c_str
, at
);
113 error
->size
= (int)snprintf(error
->message
, MAX_ERROR_LENGTH
, "%s -> %s",
114 memcached_strerror(&memc
, rc
), at
);
117 error
->next
= memc
.error_messages
;
118 memc
.error_messages
= error
;
121 memcached_return_t
memcached_set_error(memcached_st
& memc
, memcached_return_t rc
, const char *at
, const char *str
, size_t length
)
123 memcached_string_t tmp
= { str
, length
};
124 return memcached_set_error(memc
, rc
, at
, tmp
);
127 memcached_return_t
memcached_set_error(memcached_server_st
& self
, memcached_return_t rc
, const char *at
, const char *str
, size_t length
)
129 memcached_string_t tmp
= { str
, length
};
130 return memcached_set_error(self
, rc
, at
, tmp
);
133 memcached_return_t
memcached_set_error(memcached_st
& memc
, memcached_return_t rc
, const char *at
, memcached_string_t
& str
)
135 if (memcached_success(rc
))
136 return MEMCACHED_SUCCESS
;
138 _set(memc
, &str
, rc
, at
);
143 memcached_return_t
memcached_set_error(memcached_server_st
& self
, memcached_return_t rc
, const char *at
, memcached_string_t
& str
)
145 if (memcached_success(rc
))
146 return MEMCACHED_SUCCESS
;
148 char hostname_port_message
[MAX_ERROR_LENGTH
];
152 size
= snprintf(hostname_port_message
, sizeof(hostname_port_message
), "%.*s, host: %s:%d",
153 memcached_string_printf(str
),
154 self
.hostname
, int(self
.port
));
158 size
= snprintf(hostname_port_message
, sizeof(hostname_port_message
), "host: %s:%d",
159 self
.hostname
, int(self
.port
));
162 memcached_string_t error_host
= { hostname_port_message
, size
};
167 _set(*self
.root
, &error_host
, rc
, at
);
172 memcached_return_t
memcached_set_error(memcached_server_st
& self
, memcached_return_t rc
, const char *at
)
174 if (memcached_success(rc
))
175 return MEMCACHED_SUCCESS
;
177 char hostname_port
[NI_MAXHOST
+NI_MAXSERV
+ sizeof("host : ")];
178 int size
= snprintf(hostname_port
, sizeof(hostname_port
), "host: %s:%d", self
.hostname
, int(self
.port
));
180 memcached_string_t error_host
= { hostname_port
, size
};
185 _set(*self
.root
, &error_host
, rc
, at
);
190 memcached_return_t
memcached_set_error(memcached_st
& self
, memcached_return_t rc
, const char *at
)
192 if (memcached_success(rc
))
193 return MEMCACHED_SUCCESS
;
195 _set(self
, NULL
, rc
, at
);
200 memcached_return_t
memcached_set_errno(memcached_st
& self
, int local_errno
, const char *at
, const char *str
, size_t length
)
202 memcached_string_t tmp
= { str
, length
};
203 return memcached_set_errno(self
, local_errno
, at
, tmp
);
206 memcached_return_t
memcached_set_errno(memcached_server_st
& self
, int local_errno
, const char *at
, const char *str
, size_t length
)
208 memcached_string_t tmp
= { str
, length
};
209 return memcached_set_errno(self
, local_errno
, at
, tmp
);
212 memcached_return_t
memcached_set_errno(memcached_st
& self
, int local_errno
, const char *at
)
215 return MEMCACHED_SUCCESS
;
217 memcached_return_t rc
= MEMCACHED_ERRNO
;
218 _set(self
, NULL
, rc
, at
, local_errno
);
223 memcached_return_t
memcached_set_errno(memcached_st
& memc
, int local_errno
, const char *at
, memcached_string_t
& str
)
226 return MEMCACHED_SUCCESS
;
228 memcached_return_t rc
= MEMCACHED_ERRNO
;
229 _set(memc
, &str
, rc
, at
, local_errno
);
234 memcached_return_t
memcached_set_errno(memcached_server_st
& self
, int local_errno
, const char *at
, memcached_string_t
& str
)
237 return MEMCACHED_SUCCESS
;
239 char hostname_port_message
[MAX_ERROR_LENGTH
];
243 size
= snprintf(hostname_port_message
, sizeof(hostname_port_message
), "%.*s, host: %s:%d",
244 memcached_string_printf(str
),
245 self
.hostname
, int(self
.port
));
249 size
= snprintf(hostname_port_message
, sizeof(hostname_port_message
), "host: %s:%d",
250 self
.hostname
, int(self
.port
));
253 memcached_string_t error_host
= { hostname_port_message
, size
};
255 self
.cached_errno
= local_errno
; // Store in the actual server
257 memcached_return_t rc
= MEMCACHED_ERRNO
;
261 _set(*self
.root
, &error_host
, rc
, at
, local_errno
);
266 memcached_return_t
memcached_set_errno(memcached_server_st
& self
, int local_errno
, const char *at
)
269 return MEMCACHED_SUCCESS
;
271 char hostname_port_message
[MAX_ERROR_LENGTH
];
272 int size
= snprintf(hostname_port_message
, sizeof(hostname_port_message
), "host: %s:%d",
273 self
.hostname
, int(self
.port
));
275 memcached_string_t error_host
= { hostname_port_message
, size
};
277 self
.cached_errno
= local_errno
; // Store in the actual server
279 memcached_return_t rc
= MEMCACHED_ERRNO
;
283 _set(*self
.root
, &error_host
, rc
, at
, local_errno
);
288 static void _error_print(const memcached_error_t
*error
)
295 fprintf(stderr
, "%s\n", memcached_strerror(NULL
, error
->rc
) );
299 fprintf(stderr
, "%s %s\n", memcached_strerror(NULL
, error
->rc
), error
->message
);
302 _error_print(error
->next
);
305 void memcached_error_print(const memcached_st
*self
)
310 _error_print(self
->error_messages
);
313 static void _error_free(memcached_error_t
*error
)
318 _error_free(error
->next
);
320 if (error
&& error
->root
)
322 libmemcached_free(error
->root
, error
);
330 void memcached_error_free(memcached_st
*self
)
335 _error_free(self
->error_messages
);
336 self
->error_messages
= NULL
;
339 const char *memcached_last_error_message(memcached_st
*memc
)
342 return memcached_strerror(memc
, MEMCACHED_INVALID_ARGUMENTS
);
344 if (not memc
->error_messages
)
345 return memcached_strerror(memc
, MEMCACHED_SUCCESS
);
347 if (not memc
->error_messages
->size
)
348 return memcached_strerror(memc
, memc
->error_messages
->rc
);
350 return memc
->error_messages
->message
;
354 bool memcached_has_current_error(memcached_st
&memc
)
356 if (memc
.error_messages
357 and memc
.error_messages
->query_id
== memc
.query_id
358 and memcached_failed(memc
.error_messages
->rc
))
366 memcached_return_t
memcached_last_error(memcached_st
*memc
)
369 return MEMCACHED_INVALID_ARGUMENTS
;
371 if (not memc
->error_messages
)
372 return MEMCACHED_SUCCESS
;
374 return memc
->error_messages
->rc
;
377 int memcached_last_error_errno(memcached_st
*memc
)
382 if (not memc
->error_messages
)
385 return memc
->error_messages
->local_errno
;