mirror of
https://github.com/wneessen/go-mail.git
synced 2024-11-22 13:50:49 +01:00
Refactor error handling for non-TLS SMTP connections
Introduce a global error variable for non-TLS connections and update corresponding error handling across the codebase. This enhances readability and maintainability of the error management logic.
This commit is contained in:
parent
5058fd5222
commit
15b9ddf067
2 changed files with 12 additions and 2 deletions
|
@ -750,7 +750,13 @@ func (c *Client) tls() error {
|
||||||
}
|
}
|
||||||
tlsConnState, err := c.smtpClient.GetTLSConnectionState()
|
tlsConnState, err := c.smtpClient.GetTLSConnectionState()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get TLS connection state: %w", err)
|
switch {
|
||||||
|
case errors.Is(err, smtp.ErrNonTLSConnection):
|
||||||
|
c.isEncrypted = false
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("failed to get TLS connection state: %w", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
c.isEncrypted = tlsConnState.HandshakeComplete
|
c.isEncrypted = tlsConnState.HandshakeComplete
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,6 +36,10 @@ import (
|
||||||
"github.com/wneessen/go-mail/log"
|
"github.com/wneessen/go-mail/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrNonTLSConnection = errors.New("connection is not using TLS")
|
||||||
|
)
|
||||||
|
|
||||||
// A Client represents a client connection to an SMTP server.
|
// A Client represents a client connection to an SMTP server.
|
||||||
type Client struct {
|
type Client struct {
|
||||||
// Text is the textproto.Conn used by the Client. It is exported to allow for clients to add extensions.
|
// Text is the textproto.Conn used by the Client. It is exported to allow for clients to add extensions.
|
||||||
|
@ -572,7 +576,7 @@ func (c *Client) GetTLSConnectionState() (*tls.ConnectionState, error) {
|
||||||
defer c.mutex.RUnlock()
|
defer c.mutex.RUnlock()
|
||||||
|
|
||||||
if !c.tls {
|
if !c.tls {
|
||||||
return nil, errors.New("smtp: connection is not using TLS")
|
return nil, ErrNonTLSConnection
|
||||||
}
|
}
|
||||||
if c.conn == nil {
|
if c.conn == nil {
|
||||||
return nil, errors.New("smtp: connection is not established")
|
return nil, errors.New("smtp: connection is not established")
|
||||||
|
|
Loading…
Reference in a new issue