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

wrap js publish error #1513

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions js.go
Expand Up @@ -575,14 +575,14 @@ func (js *js) PublishMsg(m *Msg, opts ...PubOpt) (*PubAck, error) {
}

var pa pubAckResponse
if err := json.Unmarshal(resp.Data, &pa); err != nil {
return nil, ErrInvalidJSAck
if err = json.Unmarshal(resp.Data, &pa); err != nil {
return nil, wrapError(ErrInvalidJSAck, err.Error())
}
if pa.Error != nil {
return nil, pa.Error
}
if pa.PubAck == nil || pa.PubAck.Stream == _EMPTY_ {
return nil, ErrInvalidJSAck
return nil, wrapError(ErrInvalidJSAck, "missing stream or sequence")
}
return pa.PubAck, nil
}
Expand Down Expand Up @@ -3638,7 +3638,7 @@ const (
// DiscardOld will remove older messages to return to the limits. This is
// the default.
DiscardOld DiscardPolicy = iota
//DiscardNew will fail to store new messages.
// DiscardNew will fail to store new messages.
DiscardNew
)

Expand Down
8 changes: 8 additions & 0 deletions jserrors.go
Expand Up @@ -233,3 +233,11 @@ func (err *jsError) Unwrap() error {
}
return err.apiErr
}

func wrapError(err error, message string) error {
if err == nil {
return nil
}

return fmt.Errorf("%w: %s", err, message)
}
74 changes: 74 additions & 0 deletions jserrors_test.go
@@ -0,0 +1,74 @@
package nats

import (
"errors"
"testing"
)

func Test_wrapError_Unwrap(t *testing.T) {
err := wrapError(ErrInvalidJSAck, "external error message")
if !errors.Is(err, ErrInvalidJSAck) {
t.Errorf("wrapError() = %v, want %v", err, ErrInvalidJSAck)
}

err = wrapError(err, "")
if !errors.Is(err, ErrInvalidJSAck) {
t.Errorf("wrapError() = %v, want %v", err, ErrInvalidJSAck)
}
}

func Test_wrapError(t *testing.T) {
type args struct {
err error
message string
}
tests := []struct {
name string
args args
wantNil bool
wantError string
}{
{
name: "test1",
args: args{err: nil},
wantNil: true,
wantError: "",
},
{
name: "test2",
args: args{
err: ErrInvalidJSAck,
message: "test",
},
wantNil: false,
wantError: "nats: invalid jetstream publish response: test",
},
{
name: "test2",
args: args{
err: ErrInvalidJSAck,
message: "",
},
wantNil: false,
wantError: "nats: invalid jetstream publish response: ",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := wrapError(tt.args.err, tt.args.message)
if tt.wantNil {
if got != nil {
t.Errorf("wrapError() = %v, want nil", got)
}
return
}

if got == nil {
t.Errorf("wrapError() = %v, want not nil", got)
} else if got.Error() != tt.wantError {
t.Errorf("wrapError() = %v, want %v", got, tt.wantError)
}
})
}
}