Skip to content

Commit

Permalink
LSET return no suck key instread of NotFound when key not exist (#1557)
Browse files Browse the repository at this point in the history
The current code directly returns s.ToString() in
this case:
```
127.0.0.1:6666> lset nokey 0 1
(error) ERR NotFound:
```

NotFound might not be that friendly, and there's an
extra `:` after it. Modify it to return no such key:
```
127.0.0.1:6666> lset nokey 0 1
(error) ERR no such key
```

Note Redis also return the no such key error in this
case, so it should be fine to return this error.
  • Loading branch information
enjoy-binbin authored Jul 6, 2023
1 parent 7e23c0b commit 80913bc
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/commands/cmd_list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -458,10 +458,14 @@ class CommandLSet : public Commander {
Status Execute(Server *svr, Connection *conn, std::string *output) override {
redis::List list_db(svr->storage, conn->GetNamespace());
auto s = list_db.Set(args_[1], index_, args_[3]);
if (!s.ok()) {
if (!s.ok() && !s.IsNotFound()) {
return {Status::RedisExecErr, s.ToString()};
}

if (s.IsNotFound()) {
return {Status::RedisExecErr, errNoSuchKey};
}

*output = redis::SimpleString("OK");
return Status::OK();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/gocase/unit/type/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ func TestList(t *testing.T) {
}

t.Run("LSET against non existing key", func(t *testing.T) {
util.ErrorRegexp(t, rdb.LSet(ctx, "nosuchkey", 10, "foo").Err(), "ERR.*NotFound.*")
util.ErrorRegexp(t, rdb.LSet(ctx, "nosuchkey", 10, "foo").Err(), ".*no such key.*")
})

t.Run("LSET against non list value", func(t *testing.T) {
Expand Down

0 comments on commit 80913bc

Please sign in to comment.