From 8fee091099a0aa8672701df247841780db965079 Mon Sep 17 00:00:00 2001 From: Waldemar Quevedo Date: Mon, 22 Aug 2022 11:28:05 -0700 Subject: [PATCH] js: make an APIError implement the interface Signed-off-by: Waldemar Quevedo --- jsm.go | 12 +++++++----- test/js_test.go | 7 +++++++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/jsm.go b/jsm.go index b4b29c92b..a64c8572e 100644 --- a/jsm.go +++ b/jsm.go @@ -166,6 +166,11 @@ func (e *APIError) Error() string { return fmt.Sprintf("nats: API error %d: %s", e.ErrorCode, e.Description) } +// APIError implements the JetStreamError interface. +func (e *APIError) APIError() *APIError { + return e +} + // Is matches against an APIError. func (e *APIError) Is(err error) bool { // Extract internal APIError to match against. @@ -303,14 +308,11 @@ func (js *js) AccountInfo(opts ...JSOpt) (*AccountInfo, error) { return nil, err } if info.Error != nil { - var err error + var err JetStreamError = info.Error + // Internally checks based on error code instead of description match. if errors.Is(info.Error, ErrJetStreamNotEnabledForAccount) { err = ErrJetStreamNotEnabledForAccount - } else { - err = &jsError{ - apiErr: info.Error, - } } return nil, err } diff --git a/test/js_test.go b/test/js_test.go index 86babe57d..2d6180d13 100644 --- a/test/js_test.go +++ b/test/js_test.go @@ -134,6 +134,9 @@ func TestJetStreamNotAccountEnabled(t *testing.T) { t.Fatalf("Expected: %v, got: %v", expectedMessage, apierr.Error()) } + // an APIError also implements the JetStreamError interface. + var _ nats.JetStreamError = &nats.APIError{} + // matching arbitrary custom error via errors.Is(...) customErr := &nats.APIError{ErrorCode: expected} if ok := errors.Is(customErr, nats.ErrJetStreamNotEnabledForAccount); !ok { @@ -143,6 +146,10 @@ func TestJetStreamNotAccountEnabled(t *testing.T) { if ok := errors.Is(customErr, nats.ErrJetStreamNotEnabledForAccount); ok { t.Fatal("Expected to not match ErrJetStreamNotEnabled") } + var cerr nats.JetStreamError + if ok := errors.As(customErr, &cerr); !ok { + t.Fatal("Expected custom error to be a JetStreamError") + } // matching to concrete type via errors.As(...) var aerr *nats.APIError