summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorBinbin <binloveplay1314@qq.com>2023-04-12 15:11:29 +0800
committerGitHub <noreply@github.com>2023-04-12 10:11:29 +0300
commitbfec2d700b8b0ccf6669e0408b5c5c75299ef47e (patch)
tree04e20c7adfdb2738f59f50f9fcc5c3f43e272556 /tests
parent997fa41e99271cc5c3a79e9bf8a1332b3d9ab0c2 (diff)
downloadredis-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 'tests')
-rw-r--r--tests/modules/reply.c10
-rw-r--r--tests/unit/moduleapi/reply.tcl11
2 files changed, 21 insertions, 0 deletions
diff --git a/tests/modules/reply.c b/tests/modules/reply.c
index f890560e0..c5baa6635 100644
--- a/tests/modules/reply.c
+++ b/tests/modules/reply.c
@@ -156,6 +156,14 @@ int rw_error(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
return RedisModule_ReplyWithError(ctx, "An error");
}
+int rw_error_format(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
+ if (argc != 3) return RedisModule_WrongArity(ctx);
+
+ return RedisModule_ReplyWithErrorFormat(ctx,
+ RedisModule_StringPtrLen(argv[1], NULL),
+ RedisModule_StringPtrLen(argv[2], NULL));
+}
+
int rw_verbatim(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (argc != 2) return RedisModule_WrongArity(ctx);
@@ -197,6 +205,8 @@ int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
return REDISMODULE_ERR;
if (RedisModule_CreateCommand(ctx,"rw.error",rw_error,"",0,0,0) != REDISMODULE_OK)
return REDISMODULE_ERR;
+ if (RedisModule_CreateCommand(ctx,"rw.error_format",rw_error_format,"",0,0,0) != REDISMODULE_OK)
+ return REDISMODULE_ERR;
if (RedisModule_CreateCommand(ctx,"rw.verbatim",rw_verbatim,"",0,0,0) != REDISMODULE_OK)
return REDISMODULE_ERR;
diff --git a/tests/unit/moduleapi/reply.tcl b/tests/unit/moduleapi/reply.tcl
index 291253d3c..547be21c0 100644
--- a/tests/unit/moduleapi/reply.tcl
+++ b/tests/unit/moduleapi/reply.tcl
@@ -126,6 +126,17 @@ start_server {tags {"modules"}} {
assert_match "An error" $e
}
+ test "RESP$proto: RM_ReplyWithErrorFormat: error format reply" {
+ catch {r rw.error_format "An error: %s" foo} e
+ assert_match "ERR An error: foo" $e
+
+ catch {r rw.error_format "-ERR An error: %s" foo2} e
+ assert_match "ERR An error: foo2" $e
+
+ catch {r rw.error_format "-WRONGTYPE A type error: %s" foo3} e
+ assert_match "WRONGTYPE A type error: foo3" $e
+ }
+
r hello 2
}