Skip to content

Commit

Permalink
Issue #48: Try to recover after received RedisError
Browse files Browse the repository at this point in the history
  • Loading branch information
ra1u committed Sep 21, 2021
1 parent 559eaf4 commit 8a61893
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
10 changes: 8 additions & 2 deletions lib/command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@ class Command {
/// send_object(["SET","key","value"]);
Future send_object(Object obj) {
try {
// RedisSerialise.Serialise
return _connection._sendraw(RedisSerialise.Serialise(obj));
return _connection._sendraw(RedisSerialise.Serialise(obj)).then((v) {
// turn RedisError into exception
if (v is RedisError) {
return Future.error(v);
} else {
return v;
}
});
} catch (e) {
return Future.error(e);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/redisparser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class RedisParser {
case TYPE_BULK:
return parseBulk(s);
case TYPE_ERROR:
return parseError(s).then((e) => Future.error(e));
return parseError(s);
default:
return Future.error(
RedisRuntimeError("got element that cant not be parsed"));
Expand Down
10 changes: 10 additions & 0 deletions test/error_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ main() {
expect(cmd.send_object("GARBAGE"), throwsA(isRedisError));
});
});

group("Recover after received Redis Errors", () {
test("Expect error when sending Garbage 2", () async {
Command cmd = await generate_connect();
expect(cmd.send_object(["GARBAGE"]), throwsA(isRedisError));
// next two commands over same connection should be fine
expect(await cmd.send_object(["SET","garbage_test","grb"]),equals("OK"));
expect(await cmd.send_object(["GET","garbage_test"]),equals("grb"));
});
});
}

const Matcher isRedisError = TypeMatcher<RedisError>();

0 comments on commit 8a61893

Please sign in to comment.