-
Notifications
You must be signed in to change notification settings - Fork 704
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 ( | ||
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 then to make it compatible with
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 |
||
return 0, ErrKeyExists | ||
} | ||
} | ||
|
||
return 0, err | ||
} | ||
|
||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe add it as one of the constants here: https://github.com/nats-io/nats.go/blob/main/jserrors.go#L112
so that it represents this error from the server:
https://github.com/nats-io/nats-server/blob/e42260cc80f793006ef7f90cc91ede26fe14073f/server/jetstream_errors_generated.go#L536