Skip to content

Commit

Permalink
wrap no responder errors so there is a universal error for all provid…
Browse files Browse the repository at this point in the history
…ers to check

Signed-off-by: Mike Mason <mimason@equinix.com>
  • Loading branch information
mikemrm committed Aug 3, 2023
1 parent 9c4b442 commit 8f6b649
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions events/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ var (
ErrMissingAuthRelationshipRequestRelationRelation = errors.New("auth relationship request message Relations Relation field required")
// ErrMissingAuthRelationshipRequestRelationSubjectID is returned when the event message Relations has the incorrect field SubjectID value.
ErrMissingAuthRelationshipRequestRelationSubjectID = errors.New("auth relationship request message Relations SubjectID field required")

// ErrRequestNoResponders is returned when a request is attempted but no responder is listening.
ErrRequestNoResponders = errors.New("no responders for request")
)
7 changes: 7 additions & 0 deletions events/nats_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package events
import (
"context"
"encoding/json"
"errors"
"fmt"
"strconv"
"time"

Expand Down Expand Up @@ -178,6 +180,11 @@ func (m *NatsMessage[T]) request(ctx context.Context) (Message[AuthRelationshipR

nMsg, err := m.conn.conn.RequestMsgWithContext(ctx, m.source)
if err != nil {
// ensure we wrap no responder errors with ErrRequestNoResponders.
if errors.Is(err, nats.ErrNoResponders) {
return nil, fmt.Errorf("%w: %w", ErrRequestNoResponders, err)
}

return nil, err
}

Expand Down
7 changes: 7 additions & 0 deletions events/nats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/brianvoe/gofakeit/v6"
nc "github.com/nats-io/nats.go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -99,6 +100,12 @@ func TestNatsRequestReply(t *testing.T) {
TraceContext: map[string]string{},
}

resp, err := conn.PublishAuthRelationshipRequest(ctx, "test", authRequest)
require.Error(t, err)
require.ErrorIs(t, err, events.ErrRequestNoResponders)
require.ErrorIs(t, err, nc.ErrNoResponders)
require.Nil(t, resp)

reqGot := make(chan events.Message[events.AuthRelationshipRequest], 1)
respGot := make(chan events.Message[events.AuthRelationshipResponse], 1)

Expand Down

0 comments on commit 8f6b649

Please sign in to comment.