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

Add ErrKeyExists on kv.Create #1135

Merged
merged 1 commit into from Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions kv.go
Expand Up @@ -297,6 +297,7 @@ 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")
)

const (
Expand Down Expand Up @@ -629,6 +630,14 @@ func (kv *kvs) Create(key string, value []byte) (revision uint64, err error) {
return kv.Update(key, value, e.Revision())
}

// 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 {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know if there is a better way to identify this particular error type.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something that we started doing in the nats.go client is to move these codes into jserrors.go:
https://github.com/nats-io/nats.go/blob/main/jserrors.go#L121

then to make it compatible with errors.Is defining something like:

ErrWrongLastSequence JetStreamError = &jsError{apiErr: &APIError{ErrorCode: JSErrStreamWrongLastSequence}}

then this check would become:

if errors.Is(err, ErrWrongLastSequence) {
 return ErrKeyExists
}

What I think might be helpful eventually is to have something like another KeyValueError type that embeds the original JS APIError to be able to inspect the original JS API Error which resulted into the KeyValueError by using errors.As.

return 0, ErrKeyExists
}
}

return 0, err
}

Expand Down
23 changes: 23 additions & 0 deletions kv_test.go
Expand Up @@ -203,3 +203,26 @@ func TestKeyValueMirrorDirectGet(t *testing.T) {
}
}
}

func TestKeyValueCreate(t *testing.T) {
s := RunBasicJetStreamServer()
defer shutdownJSServerAndRemoveStorage(t, s)

nc, js := jsClient(t, s)
defer nc.Close()

kv, err := js.CreateKeyValue(&KeyValueConfig{Bucket: "TEST"})
if err != nil {
t.Fatalf("Error creating kv: %v", err)
}

_, err = kv.Create("key", []byte("1"))
if err != nil {
t.Fatalf("Error creating key: %v", err)
}

_, err = kv.Create("key", []byte("1"))
if err != ErrKeyExists {
t.Fatalf("Expected ErrKeyExists, got: %v", err)
}
}