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

pull/986/head^2
Jordan Wright 2018-03-22 22:05:24 -05:00
parent eb2f0e38c7
commit 2ff0c3d95c
No known key found for this signature in database
GPG Key ID: 138D5AD2331B3C11
2 changed files with 21 additions and 4 deletions

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

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