From 2ff0c3d95c59193b209de6d478e67a204e535550 Mon Sep 17 00:00:00 2001 From: Jordan Wright Date: Thu, 22 Mar 2018 22:05:24 -0500 Subject: [PATCH] Storing the underlying connection error after the max connection attempts have been reached. Fixes #963 --- mailer/mailer.go | 19 ++++++++++++++++--- mailer/mailer_test.go | 6 +++++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/mailer/mailer.go b/mailer/mailer.go index f652ecfc..981d90d3 100644 --- a/mailer/mailer.go +++ b/mailer/mailer.go @@ -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 } } diff --git a/mailer/mailer_test.go b/mailer/mailer_test.go index 5ba6dea9..24354a20 100644 --- a/mailer/mailer_test.go +++ b/mailer/mailer_test.go @@ -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) }