Skip to content

Commit 6ae98ea

Browse files
authored
Return single-line error in response. Free err details with RM_Free. (#76)
1 parent 8e8304e commit 6ae98ea

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed

src/err.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@
55

66
#include "redismodule.h"
77

8+
char *RAI_Chomp(const char *src) {
9+
char* str = RedisModule_Strdup(src);
10+
size_t len = strlen(src);
11+
for (size_t i=0; i<len; i++) {
12+
if (str[i] == '\n' || str[i] == '\r') {
13+
str[i] = ' ';
14+
}
15+
}
16+
return str;
17+
}
18+
819
void RAI_SetError(RAI_Error *err, RAI_ErrorCode code, const char *detail) {
920
if (err->code != RAI_OK) {
1021
return;
@@ -15,19 +26,23 @@ void RAI_SetError(RAI_Error *err, RAI_ErrorCode code, const char *detail) {
1526
if (detail) {
1627
err->detail = RedisModule_Strdup(detail);
1728
} else {
18-
err->detail = strdup("Generic error");
29+
err->detail = RedisModule_Strdup("Generic error");
1930
}
31+
32+
err->detail_oneline = RAI_Chomp(err->detail);
2033
}
2134

2235
RAI_Error RAI_InitError() {
23-
RAI_Error err = {.code = RAI_OK, .detail = NULL};
36+
RAI_Error err = {.code = RAI_OK, .detail = NULL, .detail_oneline = NULL};
2437
return err;
2538
}
2639

2740
void RAI_ClearError(RAI_Error *err) {
2841
if (err->detail) {
29-
free(err->detail);
42+
RedisModule_Free(err->detail);
43+
RedisModule_Free(err->detail_oneline);
3044
err->detail = NULL;
45+
err->detail_oneline = NULL;
3146
}
3247
err->code = RAI_OK;
3348
}

src/err.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ typedef enum {
1919
typedef struct RAI_Error {
2020
RAI_ErrorCode code;
2121
char* detail;
22+
char* detail_oneline;
2223
} RAI_Error;
2324

2425
RAI_Error RAI_InitError();

src/redisai.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ int RedisAI_ModelSet_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv,
474474
#ifdef RAI_PRINT_BACKEND_ERRORS
475475
printf("ERR: %s\n", err.detail);
476476
#endif
477-
int ret = RedisModule_ReplyWithError(ctx, err.detail);
477+
int ret = RedisModule_ReplyWithError(ctx, err.detail_oneline);
478478
RAI_ClearError(&err);
479479
return ret;
480480
}
@@ -531,7 +531,7 @@ void *RedisAI_RunSession(void *arg) {
531531
#ifdef RAI_PRINT_BACKEND_ERRORS
532532
printf("ERR: %s\n", err.detail);
533533
#endif
534-
int ret = RedisModule_ReplyWithError(ctx, err.detail);
534+
int ret = RedisModule_ReplyWithError(ctx, err.detail_oneline);
535535
RAI_ClearError(&err);
536536
return NULL;
537537
}
@@ -844,7 +844,7 @@ int RedisAI_ScriptRun_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv
844844
#ifdef RAI_PRINT_BACKEND_ERRORS
845845
printf("ERR: %s\n", err.detail);
846846
#endif
847-
int ret = RedisModule_ReplyWithError(ctx, err.detail);
847+
int ret = RedisModule_ReplyWithError(ctx, err.detail_oneline);
848848
RAI_ClearError(&err);
849849
return ret;
850850
}
@@ -918,12 +918,11 @@ int RedisAI_ScriptSet_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv
918918
RAI_Error err = RAI_InitError();
919919
script = RAI_ScriptCreate(device, scriptdef, &err);
920920

921-
// if (script == NULL){
922921
if (err.code != RAI_OK){
923922
#ifdef RAI_PRINT_BACKEND_ERRORS
924923
printf("ERR: %s\n", err.detail);
925924
#endif
926-
int ret = RedisModule_ReplyWithError(ctx, err.detail);
925+
int ret = RedisModule_ReplyWithError(ctx, err.detail_oneline);
927926
RAI_ClearError(&err);
928927
return ret;
929928
}

test/basic_tests.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,18 @@ def test_run_mobilenet_multiproc(env):
149149
env.assertEqual(
150150
label, 'giant_panda'
151151
)
152+
153+
def test_set_incorrect_script(env):
154+
try:
155+
env.execute_command('AI.SCRIPTSET', 'ket', 'CPU', 'return 1')
156+
except Exception as e:
157+
exception = e
158+
env.assertEqual(type(exception), redis.exceptions.ResponseError)
159+
160+
def test_set_correct_script(env):
161+
script = '''
162+
def foo(a, b):
163+
return a + b
164+
'''
165+
env.execute_command('AI.SCRIPTSET', 'ket', 'CPU', script)
166+

0 commit comments

Comments
 (0)