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) }