mirror of https://github.com/gophish/gophish
Storing the underlying connection error after the max connection attempts have been reached. Fixes #963
parent
eb2f0e38c7
commit
2ff0c3d95c
|
@ -2,7 +2,7 @@ package mailer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/textproto"
|
"net/textproto"
|
||||||
|
@ -16,7 +16,18 @@ var MaxReconnectAttempts = 10
|
||||||
|
|
||||||
// ErrMaxConnectAttempts is thrown when the maximum number of reconnect attempts
|
// ErrMaxConnectAttempts is thrown when the maximum number of reconnect attempts
|
||||||
// is reached.
|
// 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
|
// Logger is the logger for the worker
|
||||||
var Logger = log.New(os.Stdout, " ", log.Ldate|log.Ltime|log.Lshortfile)
|
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++
|
sendAttempt++
|
||||||
if sendAttempt == MaxReconnectAttempts {
|
if sendAttempt == MaxReconnectAttempts {
|
||||||
err = ErrMaxConnectAttempts
|
err = &ErrMaxConnectAttempts{
|
||||||
|
underlyingError: err,
|
||||||
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,9 +53,13 @@ func (ms *MailerSuite) TestDialHost() {
|
||||||
md := newMockDialer()
|
md := newMockDialer()
|
||||||
md.setDial(md.unreachableDial)
|
md.setDial(md.unreachableDial)
|
||||||
_, err := dialHost(ctx, md)
|
_, err := dialHost(ctx, md)
|
||||||
if err != ErrMaxConnectAttempts {
|
if _, ok := err.(*ErrMaxConnectAttempts); !ok {
|
||||||
ms.T().Fatalf("Didn't receive expected ErrMaxConnectAttempts. Got: %s", err)
|
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 {
|
if md.dialCount != MaxReconnectAttempts {
|
||||||
ms.T().Fatalf("Unexpected number of reconnect attempts. Expected %d, Got %d", MaxReconnectAttempts, md.dialCount)
|
ms.T().Fatalf("Unexpected number of reconnect attempts. Expected %d, Got %d", MaxReconnectAttempts, md.dialCount)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue