Skip to content

Commit

Permalink
js: switch to APIError
Browse files Browse the repository at this point in the history
Signed-off-by: Waldemar Quevedo <wally@nats.io>
  • Loading branch information
wallyqs committed Aug 22, 2022
1 parent 21ea50a commit 36b8662
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
32 changes: 16 additions & 16 deletions jsm.go
Expand Up @@ -154,22 +154,22 @@ type ExternalStream struct {
DeliverPrefix string `json:"deliver"`
}

// JetStreamAPIError is included in all API responses if there was an error.
type JetStreamAPIError struct {
// APIError is included in all API responses if there was an error.
type APIError struct {
Code int `json:"code"`
ErrorCode ErrorCode `json:"err_code"`
Description string `json:"description,omitempty"`
}

// Error prints the JetStream API error code and description
func (e *JetStreamAPIError) Error() string {
func (e *APIError) Error() string {
return fmt.Sprintf("nats: API error %d: %s", e.ErrorCode, e.Description)
}

// Is matches against an JetStreamAPIError.
func (e *JetStreamAPIError) Is(err error) bool {
// Is matches against an APIError.
func (e *APIError) Is(err error) bool {
// Extract internal APIError to match against.
var aerr *JetStreamAPIError
var aerr *APIError
ok := errors.As(err, &aerr)
if !ok {
return ok
Expand All @@ -179,16 +179,16 @@ func (e *JetStreamAPIError) Is(err error) bool {

// JetStreamError is an error result that happens when using JetStream.
type JetStreamError interface {
APIError() *JetStreamAPIError
APIError() *APIError
error
}

type jsError struct {
apiErr *JetStreamAPIError
apiErr *APIError
message string
}

func (err *jsError) APIError() *JetStreamAPIError {
func (err *jsError) APIError() *APIError {
return err.apiErr
}

Expand All @@ -206,8 +206,8 @@ func (err *jsError) Unwrap() error {

// apiResponse is a standard response from the JetStream JSON API
type apiResponse struct {
Type string `json:"type"`
Error *JetStreamAPIError `json:"error,omitempty"`
Type string `json:"type"`
Error *APIError `json:"error,omitempty"`
}

// apiPaged includes variables used to create paged responses from the JSON API
Expand Down Expand Up @@ -803,11 +803,11 @@ type StreamInfo struct {

// StreamSourceInfo shows information about an upstream stream source.
type StreamSourceInfo struct {
Name string `json:"name"`
Lag uint64 `json:"lag"`
Active time.Duration `json:"active"`
External *ExternalStream `json:"external"`
Error *JetStreamAPIError `json:"error"`
Name string `json:"name"`
Lag uint64 `json:"lag"`
Active time.Duration `json:"active"`
External *ExternalStream `json:"external"`
Error *APIError `json:"error"`
}

// StreamState is information about the given stream.
Expand Down
4 changes: 2 additions & 2 deletions nats.go
Expand Up @@ -172,10 +172,10 @@ var (

var (
// ErrJetStreamNotEnabled is an error returned when JetStream is not enabled for an account.
ErrJetStreamNotEnabled JetStreamError = &jsError{apiErr: &JetStreamAPIError{ErrorCode: JSErrCodeJetStreamNotEnabled, Description: "jetstream not enabled"}}
ErrJetStreamNotEnabled JetStreamError = &jsError{apiErr: &APIError{ErrorCode: JSErrCodeJetStreamNotEnabled, Description: "jetstream not enabled"}}

// ErrJetStreamNotEnabledForAccount is an error returned when JetStream is not enabled for an account.
ErrJetStreamNotEnabledForAccount JetStreamError = &jsError{apiErr: &JetStreamAPIError{ErrorCode: JSErrCodeJetStreamNotEnabledForAccount, Description: "jetstream not enabled"}}
ErrJetStreamNotEnabledForAccount JetStreamError = &jsError{apiErr: &APIError{ErrorCode: JSErrCodeJetStreamNotEnabledForAccount, Description: "jetstream not enabled"}}

// ErrConsumerNotActive is an error returned when consumer is not active.
ErrConsumerNotActive JetStreamError = &jsError{message: "consumer not active"}
Expand Down
8 changes: 4 additions & 4 deletions test/js_test.go
Expand Up @@ -135,17 +135,17 @@ func TestJetStreamNotAccountEnabled(t *testing.T) {
}

// matching arbitrary custom error via errors.Is(...)
customErr := &nats.JetStreamAPIError{ErrorCode: expected}
customErr := &nats.APIError{ErrorCode: expected}
if ok := errors.Is(customErr, nats.ErrJetStreamNotEnabledForAccount); !ok {
t.Fatal("Expected wrapped ErrJetStreamNotEnabledForAccount")
}
customErr = &nats.JetStreamAPIError{ErrorCode: 1}
customErr = &nats.APIError{ErrorCode: 1}
if ok := errors.Is(customErr, nats.ErrJetStreamNotEnabledForAccount); ok {
t.Fatal("Expected to not match ErrJetStreamNotEnabled")
}

// matching to concrete type via errors.As(...)
var aerr *nats.JetStreamAPIError
var aerr *nats.APIError
ok = errors.As(err, &aerr)
if !ok {
t.Fatal("Expected an APIError")
Expand Down Expand Up @@ -4900,7 +4900,7 @@ func testJetStreamMirror_Source(t *testing.T, nodes ...*jsServer) {
if err == nil {
t.Fatal("Unexpected success")
}
apiErr := &nats.JetStreamAPIError{}
apiErr := &nats.APIError{}
if !errors.As(err, &apiErr) || apiErr.ErrorCode != 10093 {
t.Fatalf("Expected API error 10093; got: %v", err)
}
Expand Down

0 comments on commit 36b8662

Please sign in to comment.