Skip to content

Commit

Permalink
Merge pull request #290 from ripienaar/no_log_10077
Browse files Browse the repository at this point in the history
do not log nats error code 10077
  • Loading branch information
ripienaar committed Jul 6, 2021
2 parents 1816a22 + dc6ff48 commit ba78726
Show file tree
Hide file tree
Showing 5 changed files with 392 additions and 68 deletions.
2 changes: 1 addition & 1 deletion api/schemas_generated.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// auto generated 2021-06-28 11:15:43.55852 +0200 CEST m=+0.028381408
// auto generated 2021-07-06 10:24:11.587448 +0200 CEST m=+0.026334259

package api

Expand Down
5 changes: 4 additions & 1 deletion governor/governor.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,10 @@ func (g *jsGMgr) Start(ctx context.Context, name string) (Finisher, error) {

res, err := jsm.ParsePubAck(m)
if err != nil {
g.Errorf("Invalid pub ack: %s", err)
if !jsm.IsNatsError(err, 10077) {
g.Errorf("Invalid pub ack: %s", err)
}

return err
}

Expand Down
13 changes: 13 additions & 0 deletions jsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,16 @@ func ParsePubAck(m *nats.Msg) (*api.PubAck, error) {

return &res, nil
}

// IsNatsError checks if err is a ApiErr matching code
func IsNatsError(err error, code uint16) bool {
if ae, ok := err.(*api.ApiError); ok {
return ae.NatsErrorCode() == code
}

if ae, ok := err.(api.ApiError); ok {
return ae.NatsErrorCode() == code
}

return false
}
25 changes: 25 additions & 0 deletions jsm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
package jsm_test

import (
"encoding/json"
"fmt"
"testing"

"github.com/nats-io/jsm.go/api"
"github.com/nats-io/nats.go"

"github.com/nats-io/jsm.go"
Expand Down Expand Up @@ -120,3 +123,25 @@ func TestIsValidName(t *testing.T) {
}
}
}

func TestIsNatsError(t *testing.T) {
pubAck := `{"error":{"code":503,"err_code":10077,"description":"maximum messages exceeded"},"stream":"GOVERNOR_TEST","seq":0}`

resp := api.JSApiResponse{}
err := json.Unmarshal([]byte(pubAck), &resp)
if err != nil {
t.Fatalf("unmarshal failed: %s", err)
}

if !jsm.IsNatsError(resp.ToError(), 10077) {
t.Fatalf("Expected response to be 10077")
}

if jsm.IsNatsError(resp.ToError(), 10076) {
t.Fatalf("Expected response to not be 10076")
}

if jsm.IsNatsError(fmt.Errorf("error"), 10077) {
t.Fatalf("Non api error is 10077")
}
}

0 comments on commit ba78726

Please sign in to comment.