diff options
Diffstat (limited to 'src/module.c')
-rw-r--r-- | src/module.c | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/module.c b/src/module.c index e29277956..2197111c2 100644 --- a/src/module.c +++ b/src/module.c @@ -2996,6 +2996,32 @@ int RM_ReplyWithError(RedisModuleCtx *ctx, const char *err) { return REDISMODULE_OK; } +/* Reply with the error create from a printf format and arguments. + * + * If the error code is already passed in the string 'fmt', the error + * code provided is used, otherwise the string "-ERR " for the generic + * error code is automatically added. + * + * The usage is, for example: + * + * RedisModule_ReplyWithErrorFormat(ctx, "An error: %s", "foo"); + * + * RedisModule_ReplyWithErrorFormat(ctx, "-WRONGTYPE Wrong Type: %s", "foo"); + * + * The function always returns REDISMODULE_OK. + */ +int RM_ReplyWithErrorFormat(RedisModuleCtx *ctx, const char *fmt, ...) { + client *c = moduleGetReplyClient(ctx); + if (c == NULL) return REDISMODULE_OK; + + va_list ap; + va_start(ap, fmt); + addReplyErrorFormatInternal(c, 0, fmt, ap); + va_end(ap); + + return REDISMODULE_OK; +} + /* Reply with a simple string (`+... \r\n` in RESP protocol). This replies * are suitable only when sending a small non-binary string with small * overhead, like "OK" or similar replies. @@ -13438,6 +13464,7 @@ void moduleRegisterCoreAPI(void) { REGISTER_API(WrongArity); REGISTER_API(ReplyWithLongLong); REGISTER_API(ReplyWithError); + REGISTER_API(ReplyWithErrorFormat); REGISTER_API(ReplyWithSimpleString); REGISTER_API(ReplyWithArray); REGISTER_API(ReplyWithMap); |