Browse Source

Storing the underlying connection error after the max connection attempts have been reached. Fixes #963

986-custom-envelope-sender
Jordan Wright 3 years ago
parent
commit
2ff0c3d95c
No known key found for this signature in database GPG Key ID: 138D5AD2331B3C11
2 changed files with 21 additions and 4 deletions
  1. +16
    -3
      mailer/mailer.go
  2. +5
    -1
      mailer/mailer_test.go

+ 16
- 3
mailer/mailer.go View File

@ -2,7 +2,7 @@ package mailer
import (
"context"
"errors"
"fmt"
"io"
"log"
"net/textproto"
@ -16,7 +16,18 @@ var MaxReconnectAttempts = 10
// ErrMaxConnectAttempts is thrown when the maximum number of reconnect attempts
// is reached.
var ErrMaxConnectAttempts = errors.New("max connection attempts reached")
type ErrMaxConnectAttempts struct {
underlyingError error
}
// Error returns the wrapped error response
func (e *ErrMaxConnectAttempts) Error() string {
errString := "Max connection attempts exceeded"
if e.underlyingError != nil {
errString = fmt.Sprintf("%s - %s", errString, e.underlyingError.Error())
}
return errString
}
// Logger is the logger for the worker
var Logger = log.New(os.Stdout, " ", log.Ldate|log.Ltime|log.Lshortfile)
@ -115,7 +126,9 @@ func dialHost(ctx context.Context, dialer Dialer) (Sender, error) {
}
sendAttempt++
if sendAttempt == MaxReconnectAttempts {
err = ErrMaxConnectAttempts
err = &ErrMaxConnectAttempts{
underlyingError: err,
}
break
}
}

+ 5
- 1
mailer/mailer_test.go View File

@ -53,9 +53,13 @@ func (ms *MailerSuite) TestDialHost() {
md := newMockDialer()
md.setDial(md.unreachableDial)
_, err := dialHost(ctx, md)
if err != ErrMaxConnectAttempts {
if _, ok := err.(*ErrMaxConnectAttempts); !ok {
ms.T().Fatalf("Didn't receive expected ErrMaxConnectAttempts. Got: %s", err)
}
e := err.(*ErrMaxConnectAttempts)
if e.underlyingError != errHostUnreachable {
ms.T().Fatalf("Got invalid underlying error. Expected %s Got %s\n", e.underlyingError, errHostUnreachable)
}
if md.dialCount != MaxReconnectAttempts {
ms.T().Fatalf("Unexpected number of reconnect attempts. Expected %d, Got %d", MaxReconnectAttempts, md.dialCount)
}

Loading…
Cancel
Save