Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error handling strreamlined #1361

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions docs/src/content/docs/commands/HSET.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
---
title: HSET
description: The `HSET` command in DiceDB is used to set the value of a field in a hash. If the hash does not exist, a new hash is created. If the field already exists in the hash, the value is updated. This command is useful for managing and storing key-value pairs within a hash data structure.
description: The `HSET` command in DiceDB is used to set the value of a field in a hash table. If the hash table does not exist, a new hash is created. If the field already exists in the hash table, the value is updated. This command is useful for managing and storing key-value pairs within a hash data structure.
---

The `HSET` command in DiceDB is used to set the value of a field in a hash. If the hash does not exist, a new hash is created. If the field already exists in the hash, the value is updated. This command is useful for managing and storing key-value pairs within a hash data structure.
The `HSET` command in DiceDB is used to set the value of a field in a hash table. If the hash table does not exist, a new hash is created. If the field already exists in the hash table, the value is updated. This command is useful for managing and storing key-value pairs within a hash data structure.

## Syntax

Expand All @@ -22,12 +22,14 @@ HSET key field value [field value ...]

## Return Values

The number of fields that were added.

| Condition | Return Value |
| ------------------------ | --------------------------------------------------------------------------- |
| A new field added | `1` |
| Existing field updated | `0` |
| Multiple fields added | `Integer` (count of new fields) |
| Wrong data type | `(error) WRONGTYPE Operation against a key holding the wrong kind of value` |
| HSET on different type | `(error) WRONGTYPE Operation against a key holding the wrong kind of value` |
| Incorrect Argument Count | `(error) ERR wrong number of arguments for 'hset' command` |

## Behaviour
Expand Down
12 changes: 6 additions & 6 deletions integration_tests/commands/http/append_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestAPPEND(t *testing.T) {
{Command: "LPUSH", Body: map[string]interface{}{"key": "m", "value": "bhima"}},
{Command: "APPEND", Body: map[string]interface{}{"key": "m", "value": "shankar"}},
},
expected: []interface{}{float64(1), "WRONGTYPE Operation against a key holding the wrong kind of value"},
expected: []interface{}{float64(1), diceerrors.ErrWrongTypeOperation.Error()},
cleanup: []HTTPCommand{
{Command: "del", Body: map[string]interface{}{"key": "m"}},
},
Expand All @@ -89,7 +89,7 @@ func TestAPPEND(t *testing.T) {
{Command: "SADD", Body: map[string]interface{}{"key": "key", "value": "apple"}},
{Command: "APPEND", Body: map[string]interface{}{"key": "key", "value": "banana"}},
},
expected: []interface{}{float64(1), "WRONGTYPE Operation against a key holding the wrong kind of value"},
expected: []interface{}{float64(1), diceerrors.ErrWrongTypeOperation.Error()},
cleanup: []HTTPCommand{
{Command: "del", Body: map[string]interface{}{"key": "key"}},
},
Expand All @@ -100,7 +100,7 @@ func TestAPPEND(t *testing.T) {
{Command: "ZADD", Body: map[string]interface{}{"key": "myzset", "values": []string{"1", "one"}}},
{Command: "APPEND", Body: map[string]interface{}{"key": "myzset", "value": "two"}},
},
expected: []interface{}{float64(1), "WRONGTYPE Operation against a key holding the wrong kind of value"},
expected: []interface{}{float64(1), diceerrors.ErrWrongTypeOperation.Error()},
cleanup: []HTTPCommand{
{Command: "del", Body: map[string]interface{}{"key": "myzset"}},
},
Expand Down Expand Up @@ -185,10 +185,10 @@ func TestAPPEND(t *testing.T) {
float64(0),
float64(1),
float64(1),
"WRONGTYPE Operation against a key holding the wrong kind of value",
diceerrors.ErrWrongTypeOperation.Error(),
float64(6),
"WRONGTYPE Operation against a key holding the wrong kind of value",
"WRONGTYPE Operation against a key holding the wrong kind of value",
diceerrors.ErrWrongTypeOperation.Error(),
diceerrors.ErrWrongTypeOperation.Error(),
},
cleanup: []HTTPCommand{
{Command: "del", Body: map[string]interface{}{"key": "listKey"}},
Expand Down
12 changes: 6 additions & 6 deletions integration_tests/commands/http/bit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1002,21 +1002,21 @@ func TestBitfield(t *testing.T) {
{
Name: "BITFIELD on unsupported type of SET",
Commands: []HTTPCommand{{Command: "SADD", Body: map[string]interface{}{"key": "bits", "values": []string{"a", "b", "c"}}}, {Command: "BITFIELD", Body: map[string]interface{}{"key": "bits"}}},
Expected: []interface{}{float64(3), "WRONGTYPE Operation against a key holding the wrong kind of value"},
Expected: []interface{}{float64(3), diceerrors.ErrWrongTypeOperation.Error()},
Delay: []time.Duration{0, 0},
CleanUp: []HTTPCommand{{Command: "DEL", Body: map[string]interface{}{"key": "bits"}}},
},
{
Name: "BITFIELD on unsupported type of JSON",
Commands: []HTTPCommand{{Command: "json.set", Body: map[string]interface{}{"key": "bits", "path": "$", "value": "1"}}, {Command: "BITFIELD", Body: map[string]interface{}{"key": "bits"}}},
Expected: []interface{}{"OK", "WRONGTYPE Operation against a key holding the wrong kind of value"},
Expected: []interface{}{"OK", diceerrors.ErrWrongTypeOperation.Error()},
Delay: []time.Duration{0, 0},
CleanUp: []HTTPCommand{{Command: "DEL", Body: map[string]interface{}{"key": "bits"}}},
},
{
Name: "BITFIELD on unsupported type of HSET",
Commands: []HTTPCommand{{Command: "HSET", Body: map[string]interface{}{"key": "bits", "field": "a", "value": "1"}}, {Command: "BITFIELD", Body: map[string]interface{}{"key": "bits"}}},
Expected: []interface{}{float64(1), "WRONGTYPE Operation against a key holding the wrong kind of value"},
Expected: []interface{}{float64(1), diceerrors.ErrWrongTypeOperation.Error()},
Delay: []time.Duration{0, 0},
CleanUp: []HTTPCommand{{Command: "DEL", Body: map[string]interface{}{"key": "bits"}}},
},
Expand Down Expand Up @@ -1257,21 +1257,21 @@ func TestBitfieldRO(t *testing.T) {
{
Name: "BITFIELD_RO on unsupported type of SET",
Commands: []HTTPCommand{{Command: "SADD", Body: map[string]interface{}{"key": "bits", "values": []string{"a", "b", "c"}}}, {Command: "BITFIELD_RO", Body: map[string]interface{}{"key": "bits"}}},
Expected: []interface{}{float64(3), "WRONGTYPE Operation against a key holding the wrong kind of value"},
Expected: []interface{}{float64(3), diceerrors.ErrWrongTypeOperation.Error()},
Delay: []time.Duration{0, 0},
CleanUp: []HTTPCommand{{Command: "DEL", Body: map[string]interface{}{"key": "bits"}}},
},
{
Name: "BITFIELD_RO on unsupported type of JSON",
Commands: []HTTPCommand{{Command: "JSON.SET", Body: map[string]interface{}{"key": "bits", "path": "$", "value": "1"}}, {Command: "BITFIELD_RO", Body: map[string]interface{}{"key": "bits"}}},
Expected: []interface{}{"OK", "WRONGTYPE Operation against a key holding the wrong kind of value"},
Expected: []interface{}{"OK", diceerrors.ErrWrongTypeOperation.Error()},
Delay: []time.Duration{0, 0},
CleanUp: []HTTPCommand{{Command: "DEL", Body: map[string]interface{}{"key": "bits"}}},
},
{
Name: "BITFIELD_RO on unsupported type of HSET",
Commands: []HTTPCommand{{Command: "HSET", Body: map[string]interface{}{"key": "bits", "field": "a", "value": "1"}}, {Command: "BITFIELD_RO", Body: map[string]interface{}{"key": "bits"}}},
Expected: []interface{}{float64(1), "WRONGTYPE Operation against a key holding the wrong kind of value"},
Expected: []interface{}{float64(1), diceerrors.ErrWrongTypeOperation.Error()},
Delay: []time.Duration{0, 0},
CleanUp: []HTTPCommand{{Command: "DEL", Body: map[string]interface{}{"key": "bits"}}},
},
Expand Down
6 changes: 3 additions & 3 deletions integration_tests/commands/http/bloom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ func TestBFEdgeCasesAndErrors(t *testing.T) {
Body: map[string]interface{}{"key": "bf", "values": []interface{}{0.01, 1000}},
},
},
expected: []interface{}{"OK", "WRONGTYPE Operation against a key holding the wrong kind of value"},
expected: []interface{}{"OK", diceerrors.ErrWrongTypeOperation.Error()},
},
{
name: "BF.ADD on a key holding a list",
Expand All @@ -342,7 +342,7 @@ func TestBFEdgeCasesAndErrors(t *testing.T) {
Body: map[string]interface{}{"key": "bf", "value": "item2"},
},
},
expected: []interface{}{float64(1), "WRONGTYPE Operation against a key holding the wrong kind of value"},
expected: []interface{}{float64(1), diceerrors.ErrWrongTypeOperation.Error()},
},
{
name: "BF.INFO on a key holding a hash",
Expand All @@ -356,7 +356,7 @@ func TestBFEdgeCasesAndErrors(t *testing.T) {
Body: map[string]interface{}{"key": "bf"},
},
},
expected: []interface{}{float64(1), "WRONGTYPE Operation against a key holding the wrong kind of value"},
expected: []interface{}{float64(1), diceerrors.ErrWrongTypeOperation.Error()},
},
}
for _, tc := range testCases {
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/commands/http/check_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// This file may contain test cases for checking error messages across all commands
func TestErrorsForSetData(t *testing.T) {
exec := NewHTTPCommandExecutor()
setErrorMsg := "WRONGTYPE Operation against a key holding the wrong kind of value"
setErrorMsg := diceerrors.ErrWrongTypeOperation.Error()
testCases := []struct {
name string
commands []HTTPCommand
Expand Down
4 changes: 2 additions & 2 deletions integration_tests/commands/http/getdel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestGetDel(t *testing.T) {
{Command: "SADD", Body: map[string]interface{}{"key": "myset", "member": "member1"}},
{Command: "GETDEL", Body: map[string]interface{}{"key": "myset"}},
},
expected: []interface{}{float64(1), "WRONGTYPE Operation against a key holding the wrong kind of value"},
expected: []interface{}{float64(1), diceerrors.ErrWrongTypeOperation.Error()},
delays: []time.Duration{0, 0},
},
{
Expand All @@ -83,7 +83,7 @@ func TestGetDel(t *testing.T) {
{Command: "GETDEL", Body: map[string]interface{}{"key": "k"}},
{Command: "JSON.GET", Body: map[string]interface{}{"key": "k"}},
},
expected: []interface{}{"OK", "WRONGTYPE Operation against a key holding the wrong kind of value", "1"},
expected: []interface{}{"OK", diceerrors.ErrWrongTypeOperation.Error(), "1"},
delays: []time.Duration{0, 0, 0},
},
}
Expand Down
8 changes: 4 additions & 4 deletions integration_tests/commands/http/getex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func TestGetEx(t *testing.T) {
{Command: "JSON.SET", Body: map[string]interface{}{"key": "KEY", "path": "$", "value": "1"}},
{Command: "GETEX", Body: map[string]interface{}{"key": "KEY"}},
},
expected: []interface{}{"OK", "WRONGTYPE Operation against a key holding the wrong kind of value"},
expected: []interface{}{"OK", diceerrors.ErrWrongTypeOperation.Error()},
assertType: []string{"equal", "equal"},
delay: []time.Duration{0, 0},
},
Expand All @@ -231,9 +231,9 @@ func TestGetEx(t *testing.T) {
},
expected: []interface{}{
"OK",
"WRONGTYPE Operation against a key holding the wrong kind of value",
diceerrors.ErrWrongTypeOperation.Error(),
"OK",
"WRONGTYPE Operation against a key holding the wrong kind of value",
diceerrors.ErrWrongTypeOperation.Error(),
},
assertType: []string{"equal", "equal", "equal", "equal"},
delay: []time.Duration{0, 0, 0, 0},
Expand All @@ -244,7 +244,7 @@ func TestGetEx(t *testing.T) {
{Command: "SADD", Body: map[string]interface{}{"key": "SKEY", "members": []interface{}{1, 2, 3}}},
{Command: "GETEX", Body: map[string]interface{}{"key": "SKEY"}},
},
expected: []interface{}{float64(3), "WRONGTYPE Operation against a key holding the wrong kind of value"},
expected: []interface{}{float64(3), diceerrors.ErrWrongTypeOperation.Error()},
assertType: []string{"equal", "equal"},
delay: []time.Duration{0, 0},
},
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/commands/http/getrange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestGETRANGE(t *testing.T) {
{Command: "LPUSH", Body: map[string]interface{}{"key": "test3", "value": "shankar"}},
{Command: "GETRANGE", Body: map[string]interface{}{"key": "test3", "values": []interface{}{0, 7}}},
},
expected: []interface{}{float64(1), "WRONGTYPE Operation against a key holding the wrong kind of value"},
expected: []interface{}{float64(1), diceerrors.ErrWrongTypeOperation.Error()},
cleanup: []HTTPCommand{
{Command: "del", Body: map[string]interface{}{"key": "test3"}},
},
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/commands/http/getset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestGetSet(t *testing.T) {
{Command: "LPUSH", Body: map[string]interface{}{"key": "k1", "value": "val"}},
{Command: "GETSET", Body: map[string]interface{}{"key": "k1", "value": "v1"}},
},
expected: []interface{}{float64(1), "WRONGTYPE Operation against a key holding the wrong kind of value"},
expected: []interface{}{float64(1), diceerrors.ErrWrongTypeOperation.Error()},
delays: []time.Duration{0, 0},
},
}
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/commands/http/hdel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestHDel(t *testing.T) {
{Command: "SET", Body: map[string]interface{}{"key": "string_key", "value": "value"}},
{Command: "HDEL", Body: map[string]interface{}{"key": "string_key", "field": "field"}},
},
expected: []interface{}{"OK", "WRONGTYPE Operation against a key holding the wrong kind of value"},
expected: []interface{}{"OK", diceerrors.ErrWrongTypeOperation.Error()},
delays: []time.Duration{0, 0},
},
}
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/commands/http/hget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestHGet(t *testing.T) {
{Command: "SET", Body: map[string]interface{}{"key": "string_key", "value": "value"}},
{Command: "HGET", Body: map[string]interface{}{"key": "string_key", "field": "field"}},
},
expected: []interface{}{"OK", "WRONGTYPE Operation against a key holding the wrong kind of value"},
expected: []interface{}{"OK", diceerrors.ErrWrongTypeOperation.Error()},
delays: []time.Duration{0, 0},
},
}
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/commands/http/hlen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestHLen(t *testing.T) {
{Command: "SET", Body: map[string]interface{}{"key": "key", "value": "value"}},
{Command: "HLEN", Body: map[string]interface{}{"key": "key"}},
},
expected: []interface{}{"OK", "WRONGTYPE Operation against a key holding the wrong kind of value"},
expected: []interface{}{"OK", diceerrors.ErrWrongTypeOperation.Error()},
delays: []time.Duration{0, 0},
},
}
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/commands/http/hscan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestHScan(t *testing.T) {
{Command: "SET", Body: map[string]interface{}{"key": "string_key", "value": "string_value"}},
{Command: "HSCAN", Body: map[string]interface{}{"values": []interface{}{"string_key", 0}}},
},
expected: []interface{}{"OK", "WRONGTYPE Operation against a key holding the wrong kind of value"},
expected: []interface{}{"OK", diceerrors.ErrWrongTypeOperation.Error()},
delays: []time.Duration{0, 0},
},
{
Expand Down
3 changes: 2 additions & 1 deletion integration_tests/commands/http/hset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"
"time"

diceerrors "github.com/dicedb/dice/internal/errors"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -63,7 +64,7 @@ func TestHSet(t *testing.T) {
{Command: "SET", Body: map[string]interface{}{"key": "string_key", "value": "value"}},
{Command: "HSET", Body: map[string]interface{}{"key": "string_key", "field": "field", "value": "value"}},
},
expected: []interface{}{"OK", "WRONGTYPE Operation against a key holding the wrong kind of value"},
expected: []interface{}{"OK", diceerrors.ErrWrongTypeOperation.Error()},
delays: []time.Duration{0, 0},
},
}
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/commands/http/hsetnx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestHSetNX(t *testing.T) {
{Command: "SET", Body: map[string]interface{}{"key": "key_nx_t4", "value": "v"}},
{Command: "HSETNX", Body: map[string]interface{}{"key": "key_nx_t4", "field": "f", "value": "v_new"}},
},
expected: []interface{}{"OK", "WRONGTYPE Operation against a key holding the wrong kind of value"},
expected: []interface{}{"OK", diceerrors.ErrWrongTypeOperation.Error()},
delays: []time.Duration{0, 0},
},
}
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/commands/http/hstrlen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestHStrLen(t *testing.T) {
{Command: "SET", Body: map[string]interface{}{"key": "key", "value": "value"}},
{Command: "HSTRLEN", Body: map[string]interface{}{"key": "key", "field": "field"}},
},
expected: []interface{}{"OK", "WRONGTYPE Operation against a key holding the wrong kind of value"},
expected: []interface{}{"OK", diceerrors.ErrWrongTypeOperation.Error()},
delays: []time.Duration{0, 0},
},
}
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/commands/http/json_arrpop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func TestJSONARRPOP(t *testing.T) {
Body: map[string]interface{}{"key": "doc_new"},
},
},
expected: []interface{}{"OK", "WRONGTYPE Operation against a key holding the wrong kind of value"},
expected: []interface{}{"OK", diceerrors.ErrWrongTypeOperation.Error()},
},
{
name: "nil response for arr pop",
Expand Down
4 changes: 2 additions & 2 deletions integration_tests/commands/http/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func TestJSONOperations(t *testing.T) {
{Command: "SET", Body: map[string]interface{}{"key": "k1", "value": "1"}},
{Command: "JSON.GET", Body: map[string]interface{}{"key": "k1"}},
},
expected: []interface{}{"OK", "WRONGTYPE Operation against a key holding the wrong kind of value"},
expected: []interface{}{"OK", diceerrors.ErrWrongTypeOperation.Error()},
},
{
name: "Set Empty JSON Object",
Expand Down Expand Up @@ -1286,7 +1286,7 @@ func TestJsonObjLen(t *testing.T) {
commands: []HTTPCommand{
{Command: "json.objlen", Body: map[string]interface{}{"key": "c", "path": ".name"}},
},
expected: []interface{}{"WRONGTYPE Operation against a key holding the wrong kind of value"},
expected: []interface{}{diceerrors.ErrWrongTypeOperation.Error()},
},
{
name: "JSON.OBJLEN with legacy path - inner existent path recursive object",
Expand Down
Loading
Loading