Skip to content

Commit

Permalink
Log common network-related errors on debug level
Browse files Browse the repository at this point in the history
  • Loading branch information
boreq committed Nov 14, 2023
1 parent 673e88a commit 3bafba3
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 7 deletions.
67 changes: 60 additions & 7 deletions service/adapters/relay_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,10 @@ func NewRelayConnection(address domain.RelayAddress, logger logging.Logger) *Rel
func (r *RelayConnection) Run(ctx context.Context) {
for {
if err := r.run(ctx); err != nil {
var l logging.Entry
if !errors.Is(err, websocket.ErrBadHandshake) {
l = r.logger.Error()
} else {
l := r.logger.Error()
if r.errorIsCommonAndShouldNotBeLoggedOnErrorLevel(err) {
l = r.logger.Debug()
}

l.WithError(err).Message("encountered an error")
}

Expand All @@ -57,6 +54,18 @@ func (r *RelayConnection) Run(ctx context.Context) {
}
}

func (r *RelayConnection) errorIsCommonAndShouldNotBeLoggedOnErrorLevel(err error) bool {
if errors.Is(err, DialError{}) {
return true
}

if errors.Is(err, ReadMessageError{}) {
return true
}

return false
}

func (r *RelayConnection) GetEvents(ctx context.Context, publicKey domain.PublicKey, eventKinds []domain.EventKind, maxAge *time.Duration) <-chan app.EventOrEndOfSavedEvents {
r.subscriptionsMutex.Lock()
defer r.subscriptionsMutex.Unlock()
Expand Down Expand Up @@ -117,7 +126,7 @@ func (r *RelayConnection) run(ctx context.Context) error {

conn, _, err := websocket.DefaultDialer.DialContext(ctx, r.address.String(), nil)
if err != nil {
return errors.Wrap(err, "error dialing the relay")
return NewDialError(err)
}

r.setState(app.RelayConnectionStateConnected)
Expand All @@ -143,7 +152,7 @@ func (r *RelayConnection) run(ctx context.Context) error {
for {
_, messageBytes, err := conn.ReadMessage()
if err != nil {
return errors.Wrap(err, "error reading a message")
return NewReadMessageError(err)
}

if err := r.handleMessage(messageBytes); err != nil {
Expand Down Expand Up @@ -308,3 +317,47 @@ type subscription struct {
eventKinds []domain.EventKind
maxAge *time.Duration
}

type DialError struct {
underlying error
}

func NewDialError(underlying error) DialError {
return DialError{underlying: underlying}
}

func (t DialError) Error() string {
return fmt.Sprintf("error dialing the relay: %s", t.underlying)
}

func (t DialError) Unwrap() error {
return t.underlying
}

func (t DialError) Is(target error) bool {
_, ok1 := target.(DialError)
_, ok2 := target.(*DialError)
return ok1 || ok2
}

type ReadMessageError struct {
underlying error
}

func NewReadMessageError(underlying error) ReadMessageError {
return ReadMessageError{underlying: underlying}
}

func (t ReadMessageError) Error() string {
return fmt.Sprintf("error reading a message from websocket: %s", t.underlying)
}

func (t ReadMessageError) Unwrap() error {
return t.underlying
}

func (t ReadMessageError) Is(target error) bool {
_, ok1 := target.(ReadMessageError)
_, ok2 := target.(*ReadMessageError)
return ok1 || ok2
}
18 changes: 18 additions & 0 deletions service/adapters/relay_connection_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package adapters_test

import (
"testing"

"github.com/boreq/errors"
"github.com/planetary-social/nos-crossposting-service/internal/fixtures"
"github.com/planetary-social/nos-crossposting-service/service/adapters"
"github.com/stretchr/testify/require"
)

func TestDialErrorIs(t *testing.T) {
err := adapters.NewDialError(fixtures.SomeError())
require.ErrorIs(t, err, adapters.DialError{})
require.ErrorIs(t, err, &adapters.DialError{})
require.ErrorIs(t, errors.Wrap(err, "wrapped"), adapters.DialError{})
require.ErrorIs(t, errors.Wrap(err, "wrapped"), &adapters.DialError{})
}

0 comments on commit 3bafba3

Please sign in to comment.