Skip to content

Commit

Permalink
Add ErrKeyExists that is compatible with errors.Is on kv.Create (#1136)
Browse files Browse the repository at this point in the history
* Add ErrKeyExists error that is compatible with errors.Is on kv.Create

Signed-off-by: Waldemar Quevedo <[email protected]>
  • Loading branch information
wallyqs authored Nov 17, 2022
1 parent 5044b6e commit 8499836
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
2 changes: 2 additions & 0 deletions jserrors.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ const (
JSErrCodeMessageNotFound ErrorCode = 10037

JSErrCodeBadRequest ErrorCode = 10003

JSErrCodeStreamWrongLastSequence ErrorCode = 10071
)

// APIError is included in all API responses if there was an error.
Expand Down
12 changes: 7 additions & 5 deletions kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,10 @@ var (
ErrKeyDeleted = errors.New("nats: key was deleted")
ErrHistoryToLarge = errors.New("nats: history limited to a max of 64")
ErrNoKeysFound = errors.New("nats: no keys found")
ErrKeyExists = errors.New("nats: key exists")
)

var (
ErrKeyExists JetStreamError = &jsError{apiErr: &APIError{ErrorCode: JSErrCodeStreamWrongLastSequence, Code: 400}, message: "key exists"}
)

const (
Expand Down Expand Up @@ -632,10 +635,9 @@ func (kv *kvs) Create(key string, value []byte) (revision uint64, err error) {

// Check if the expected last subject sequence is not zero which implies
// the key already exists.
if aerr, ok := err.(*APIError); ok {
if aerr.ErrorCode == 10071 {
return 0, ErrKeyExists
}
if errors.Is(err, ErrKeyExists) {
jserr := ErrKeyExists.(*jsError)
return 0, fmt.Errorf("%w: %s", err, jserr.message)
}

return 0, err
Expand Down
27 changes: 26 additions & 1 deletion kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package nats

import (
"errors"
"fmt"
"testing"
"time"
Expand Down Expand Up @@ -222,7 +223,31 @@ func TestKeyValueCreate(t *testing.T) {
}

_, err = kv.Create("key", []byte("1"))
if err != ErrKeyExists {
expected := "nats: wrong last sequence: 1: key exists"
if err.Error() != expected {
t.Fatalf("Expected %q, got: %v", expected, err)
}
if !errors.Is(err, ErrKeyExists) {
t.Fatalf("Expected ErrKeyExists, got: %v", err)
}
aerr := &APIError{}
if !errors.As(err, &aerr) {
t.Fatalf("Expected APIError, got: %v", err)
}
if aerr.Description != "wrong last sequence: 1" {
t.Fatalf("Unexpected APIError message, got: %v", aerr.Description)
}
if aerr.ErrorCode != 10071 {
t.Fatalf("Unexpected error code, got: %v", aerr.ErrorCode)
}
if aerr.Code != ErrKeyExists.APIError().Code {
t.Fatalf("Unexpected error code, got: %v", aerr.Code)
}
var kerr JetStreamError
if !errors.As(err, &kerr) {
t.Fatalf("Expected KeyValueError, got: %v", err)
}
if kerr.APIError().ErrorCode != 10071 {
t.Fatalf("Unexpected error code, got: %v", kerr.APIError().ErrorCode)
}
}

0 comments on commit 8499836

Please sign in to comment.