summaryrefslogtreecommitdiff
path: root/hiredis.c
diff options
context:
space:
mode:
authorPieter Noordhuis <pcnoordhuis@gmail.com>2010-11-02 16:36:38 +0100
committerPieter Noordhuis <pcnoordhuis@gmail.com>2010-11-02 16:36:38 +0100
commitffa8666a647068f802eb784737ec533b6fc46115 (patch)
tree774a6ec4abc53f3b2f81e69c82e89949f83a39bc /hiredis.c
parent5db8008d97c7ec85d7a4e03df3c28a2f7cf46894 (diff)
Change error reporting to have an explicit type
When there is an I/O error, errno should be used to find out what is wrong. In other cases, errno cannot be used. So, use an explicit type in Hiredis to define the different error scenarios that can occur.
Diffstat (limited to 'hiredis.c')
-rw-r--r--hiredis.c37
1 files changed, 22 insertions, 15 deletions
diff --git a/hiredis.c b/hiredis.c
index 8848def..0aabd25 100644
--- a/hiredis.c
+++ b/hiredis.c
@@ -614,6 +614,17 @@ int redisFormatCommandArgv(char **target, int argc, const char **argv, const siz
return totlen;
}
+static void __redisSetError(redisContext *c, int type, const char *str) {
+ c->err = type;
+ if (str) {
+ c->errstr = sdsnew(str);
+ } else {
+ /* Only REDIS_ERR_IO may lack a description! */
+ assert(type == REDIS_ERR_IO);
+ c->errstr = sdsnew(strerror(errno));
+ }
+}
+
static int redisContextConnect(redisContext *c, const char *ip, int port) {
char err[ANET_ERR_LEN];
if (c->flags & REDIS_BLOCK) {
@@ -623,11 +634,11 @@ static int redisContextConnect(redisContext *c, const char *ip, int port) {
}
if (c->fd == ANET_ERR) {
- c->error = sdsnew(err);
+ __redisSetError(c,REDIS_ERR_CONN,err);
return REDIS_ERR;
}
if (anetTcpNoDelay(err,c->fd) == ANET_ERR) {
- c->error = sdsnew(err);
+ __redisSetError(c,REDIS_ERR_CONN,err);
return REDIS_ERR;
}
return REDIS_OK;
@@ -635,7 +646,8 @@ static int redisContextConnect(redisContext *c, const char *ip, int port) {
static redisContext *redisContextInit() {
redisContext *c = calloc(sizeof(redisContext),1);
- c->error = NULL;
+ c->err = 0;
+ c->errstr = NULL;
c->obuf = sdsempty();
c->fn = &defaultFunctions;
c->reader = NULL;
@@ -646,8 +658,8 @@ void redisFree(redisContext *c) {
/* Disconnect before free'ing if not yet disconnected. */
if (c->flags & REDIS_CONNECTED)
close(c->fd);
- if (c->error != NULL)
- sdsfree(c->error);
+ if (c->errstr != NULL)
+ sdsfree(c->errstr);
if (c->obuf != NULL)
sdsfree(c->obuf);
if (c->reader != NULL)
@@ -702,14 +714,12 @@ int redisBufferRead(redisContext *c) {
if (errno == EAGAIN) {
/* Try again later */
} else {
- /* Set error in context */
- c->error = sdscatprintf(sdsempty(),
- "read: %s", strerror(errno));
+ __redisSetError(c,REDIS_ERR_IO,NULL);
return REDIS_ERR;
}
} else if (nread == 0) {
- c->error = sdscatprintf(sdsempty(),
- "read: Server closed the connection");
+ __redisSetError(c,REDIS_ERR_EOF,
+ "Server closed the connection");
return REDIS_ERR;
} else {
__redisCreateReplyReader(c);
@@ -735,9 +745,7 @@ int redisBufferWrite(redisContext *c, int *done) {
if (errno == EAGAIN) {
/* Try again later */
} else {
- /* Set error in context */
- c->error = sdscatprintf(sdsempty(),
- "write: %s", strerror(errno));
+ __redisSetError(c,REDIS_ERR_IO,NULL);
return REDIS_ERR;
}
} else if (nwritten > 0) {
@@ -758,8 +766,7 @@ int redisBufferWrite(redisContext *c, int *done) {
static int __redisGetReply(redisContext *c, void **reply) {
__redisCreateReplyReader(c);
if (redisReplyReaderGetReply(c->reader,reply) == REDIS_ERR) {
- /* Copy the (protocol) error from the reader to the context. */
- c->error = sdsnew(((redisReader*)c->reader)->error);
+ __redisSetError(c,REDIS_ERR_PROTOCOL,((redisReader*)c->reader)->error);
return REDIS_ERR;
}
return REDIS_OK;