diff options
author | Binbin <binloveplay1314@qq.com> | 2023-04-12 15:11:29 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-12 10:11:29 +0300 |
commit | bfec2d700b8b0ccf6669e0408b5c5c75299ef47e (patch) | |
tree | 04e20c7adfdb2738f59f50f9fcc5c3f43e272556 /src/module.c | |
parent | 997fa41e99271cc5c3a79e9bf8a1332b3d9ab0c2 (diff) | |
download | redis-bfec2d700b8b0ccf6669e0408b5c5c75299ef47e.tar.gz |
Add RM_ReplyWithErrorFormat that can support format (#11923)
* Add RM_ReplyWithErrorFormat that can support format
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.
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); |