Skip to content

Commit

Permalink
js: Add JetStreamAPIError with more api response details
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 12, 2022
1 parent 30d5319 commit 3d28ca3
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
40 changes: 38 additions & 2 deletions jsm.go
Expand Up @@ -161,6 +161,37 @@ type APIError struct {
Description string `json:"description,omitempty"`
}

// JetStreamAPIError is an error result from making a request to the
// JetStream API.
type JetStreamAPIError interface {
Code() int
ErrorCode() int
Description() string
Error() string
}

type jsAPIError struct {
code int
errorCode int
description string
}

func (err *jsAPIError) Code() int {
return err.code
}

func (err *jsAPIError) ErrorCode() int {
return err.errorCode
}

func (err *jsAPIError) Description() string {
return err.description
}

func (err *jsAPIError) Error() string {
return "nats: jetstream not enabled"
}

// apiResponse is a standard response from the JetStream JSON API
type apiResponse struct {
Type string `json:"type"`
Expand Down Expand Up @@ -245,10 +276,15 @@ func (js *js) AccountInfo(opts ...JSOpt) (*AccountInfo, error) {
}
if info.Error != nil {
var err error
if strings.Contains(info.Error.Description, "not enabled for") {
// Check based on error code instead of description match.
if info.Error.ErrorCode == ErrJetStreamNotEnabled.ErrorCode() {
err = ErrJetStreamNotEnabled
} else {
err = errors.New(info.Error.Description)
err = &jsAPIError{
code: info.Error.Code,
errorCode: info.Error.ErrorCode,
description: info.Error.Description,
}
}
return nil, err
}
Expand Down
6 changes: 5 additions & 1 deletion nats.go
Expand Up @@ -139,7 +139,6 @@ var (
ErrNoResponders = errors.New("nats: no responders available for request")
ErrNoContextOrTimeout = errors.New("nats: no context or timeout given")
ErrPullModeNotAllowed = errors.New("nats: pull based not supported")
ErrJetStreamNotEnabled = errors.New("nats: jetstream not enabled")
ErrJetStreamBadPre = errors.New("nats: jetstream api prefix not valid")
ErrNoStreamResponse = errors.New("nats: no response from stream")
ErrNotJSMessage = errors.New("nats: not a jetstream message")
Expand Down Expand Up @@ -172,6 +171,11 @@ var (
ErrConnectionNotTLS = errors.New("nats: connection is not tls")
)

var (
// ErrJetStreamNotEnabled is an error returned when JetStream is not enabled for an account.
ErrJetStreamNotEnabled JetStreamAPIError = &jsAPIError{errorCode: 10039}
)

func init() {
rand.Seed(time.Now().UnixNano())
}
Expand Down
11 changes: 10 additions & 1 deletion test/js_test.go
Expand Up @@ -87,9 +87,18 @@ func TestJetStreamNotAccountEnabled(t *testing.T) {
nc, js := jsClient(t, s)
defer nc.Close()

if _, err := js.AccountInfo(); err != nats.ErrJetStreamNotEnabled {
_, err := js.AccountInfo()
if err != nats.ErrJetStreamNotEnabled {
t.Fatalf("Did not get the proper error, got %v", err)
}
jserr, ok := err.(nats.JetStreamAPIError)
if !ok {
t.Fatal("Expected a JetStreamAPIError")
}
expected := 10039
if jserr.ErrorCode() != expected {
t.Fatalf("Expected: %v, got: %v", expected, jserr.ErrorCode())
}
}

func TestJetStreamPublish(t *testing.T) {
Expand Down

0 comments on commit 3d28ca3

Please sign in to comment.