Add context to createErrorObject

This commit is contained in:
Pieter Noordhuis 2010-09-20 18:18:25 +02:00
parent db5244045c
commit 0a6e3b38e7

View File

@ -51,8 +51,12 @@ typedef struct redisReader {
static redisReply *redisReadReply(int fd); static redisReply *redisReadReply(int fd);
static redisReply *createReplyObject(int type, sds reply); static redisReply *createReplyObject(int type, sds reply);
static redisReply *createErrorObject(const char *fmt, ...); static void *createErrorObject(redisReader *context, const char *fmt, ...);
static void redisSetReplyReaderError(redisReader *r, redisReply *error); static void *createStringObject(redisReadTask *task, char *str, size_t len);
static void *createArrayObject(redisReadTask *task, int elements);
static void *createIntegerObject(redisReadTask *task, long long value);
static void *createNilObject(redisReadTask *task);
static void redisSetReplyReaderError(redisReader *r, void *obj);
/* We simply abort on out of memory */ /* We simply abort on out of memory */
static void redisOOM(void) { static void redisOOM(void) {
@ -69,7 +73,7 @@ redisReply *redisConnect(int *fd, const char *ip, int port) {
*fd = anetTcpConnect(err,ip,port); *fd = anetTcpConnect(err,ip,port);
if (*fd == ANET_ERR) if (*fd == ANET_ERR)
return createErrorObject(err); return (redisReply*)createErrorObject(NULL,err);
anetTcpNoDelay(NULL,*fd); anetTcpNoDelay(NULL,*fd);
return NULL; return NULL;
} }
@ -104,24 +108,23 @@ void freeReplyObject(redisReply *r) {
free(r); free(r);
} }
static redisReply *createErrorObject(const char *fmt, ...) { static void *createErrorObject(redisReader *context, const char *fmt, ...) {
va_list ap; va_list ap;
sds err; sds err;
redisReply *r; void *obj;
redisReadTask t = { REDIS_PROTOCOL_ERROR, NULL, -1 };
va_start(ap,fmt); va_start(ap,fmt);
err = sdscatvprintf(sdsempty(),fmt,ap); err = sdscatvprintf(sdsempty(),fmt,ap);
va_end(ap); va_end(ap);
r = createReplyObject(REDIS_PROTOCOL_ERROR,err); obj = createStringObject(&t,err,sdslen(err));
return r; sdsfree(err);
} return obj;
static redisReply *redisIOError(void) {
return createErrorObject("I/O error");
} }
static void *createStringObject(redisReadTask *task, char *str, size_t len) { static void *createStringObject(redisReadTask *task, char *str, size_t len) {
redisReply *r = createReplyObject(task->type,sdsnewlen(str,len)); redisReply *r = createReplyObject(task->type,sdsnewlen(str,len));
assert(task->type == REDIS_REPLY_ERROR || assert(task->type == REDIS_PROTOCOL_ERROR ||
task->type == REDIS_REPLY_ERROR ||
task->type == REDIS_REPLY_STATUS || task->type == REDIS_REPLY_STATUS ||
task->type == REDIS_REPLY_STRING); task->type == REDIS_REPLY_STRING);
@ -325,7 +328,7 @@ static int processItem(redisReader *r) {
break; break;
default: default:
byte = sdscatrepr(sdsempty(),p,1); byte = sdscatrepr(sdsempty(),p,1);
redisSetReplyReaderError(r,createErrorObject( redisSetReplyReaderError(r,createErrorObject(r,
"protocol error, got %s as reply type byte", byte)); "protocol error, got %s as reply type byte", byte));
sdsfree(byte); sdsfree(byte);
return -1; return -1;
@ -347,7 +350,7 @@ static int processItem(redisReader *r) {
case REDIS_REPLY_ARRAY: case REDIS_REPLY_ARRAY:
return processMultiBulkItem(r); return processMultiBulkItem(r);
default: default:
redisSetReplyReaderError(r,createErrorObject( redisSetReplyReaderError(r,createErrorObject(r,
"unknown item type '%d'", cur->type)); "unknown item type '%d'", cur->type));
return -1; return -1;
} }
@ -362,7 +365,7 @@ static redisReply *redisReadReply(int fd) {
do { do {
if ((nread = read(fd,buf,sizeof(buf))) <= 0) { if ((nread = read(fd,buf,sizeof(buf))) <= 0) {
reply = redisIOError(); reply = createErrorObject(reader,"I/O error");
break; break;
} else { } else {
reply = redisFeedReplyReader(reader,buf,nread); reply = redisFeedReplyReader(reader,buf,nread);
@ -398,7 +401,7 @@ int redisIsReplyReaderEmpty(void *reader) {
return 1; return 1;
} }
static void redisSetReplyReaderError(redisReader *r, redisReply *error) { static void redisSetReplyReaderError(redisReader *r, void *obj) {
if (r->reply != NULL) if (r->reply != NULL)
freeReplyObject(r->reply); freeReplyObject(r->reply);
@ -409,7 +412,7 @@ static void redisSetReplyReaderError(redisReader *r, redisReply *error) {
r->pos = 0; r->pos = 0;
} }
r->rlen = r->rpos = 0; r->rlen = r->rpos = 0;
r->reply = error; r->reply = obj;
} }
void *redisFeedReplyReader(void *reader, char *buf, int len) { void *redisFeedReplyReader(void *reader, char *buf, int len) {