Skip to content

Commit

Permalink
preserve NATS error type (#147)
Browse files Browse the repository at this point in the history
Clients of events (in this case specifically Flasher) rely on the error
type returned from this library for error handling. This change wraps
the NATS error with a library message instead of using the NATS error to
enrich a stock library error. This facilitates a test of `errors.Is(err,
nats.ErrTimeout)` for the case where we want to ignore an error.

An alternative could be to detect the NATS error and rewrite it
conditionally into native events errors, but that is additional work for
no additional operation benefit.
  • Loading branch information
DoctorVin authored Jul 17, 2023
1 parent c3044c6 commit 575e936
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion events/nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ func (n *NatsJetstream) PullMsg(_ context.Context, batch int) ([]Message, error)

subMsgs, err := subscription.Fetch(batch)
if err != nil {
return nil, errors.Wrap(ErrNatsMsgPull, err.Error())
return nil, errors.Wrap(err, ErrNatsMsgPull.Error())
}
msgs = append(msgs, msgIfFromNats(subMsgs...)...)
}
Expand Down
4 changes: 4 additions & 0 deletions events/nats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ func TestPublishAndSubscribe(t *testing.T) {
require.NoError(t, err)
require.Equal(t, 1, len(msgs))
require.Equal(t, payload, msgs[0].Data())

msgs, err = njs.PullMsg(context.TODO(), 1)
require.Error(t, err)
require.ErrorIs(t, err, nats.ErrTimeout)
}

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

0 comments on commit 575e936

Please sign in to comment.