github.com/go-email-validator/go-email-validator@v0.0.0-20230409163946-b8b9e6a0552e/pkg/ev/evsmtp/error.go (about)

     1  package evsmtp
     2  
     3  import (
     4  	"crypto/tls"
     5  	"crypto/x509"
     6  	"errors"
     7  	"fmt"
     8  	"github.com/vmihailenco/msgpack"
     9  	"net"
    10  	"net/textproto"
    11  	"net/url"
    12  	"reflect"
    13  )
    14  
    15  // Used 0 because of https://github.com/msgpack/msgpack/blob/master/spec.md#extension-types
    16  var registerExtID int8 = 0
    17  
    18  // ExtID returns register extent id, used for msgpack.RegisterExt
    19  func ExtID() int8 {
    20  	registerExtID++
    21  	return registerExtID - 1
    22  }
    23  
    24  // SetExtID sets register extent id, used for msgpack.RegisterExt
    25  func SetExtID(rExt int8) {
    26  	registerExtID = rExt
    27  }
    28  
    29  // Import different error types from packages, used in smtp.Client
    30  func init() {
    31  	msgpack.RegisterExt(ExtID(), new(DefaultError))
    32  	msgpack.RegisterExt(ExtID(), new(ASMTPError))
    33  
    34  	msgpack.RegisterExt(ExtID(), new(textproto.Error))
    35  	msgpack.RegisterExt(ExtID(), new(textproto.ProtocolError))
    36  
    37  	msgpack.RegisterExt(ExtID(), new(net.AddrError))
    38  	msgpack.RegisterExt(ExtID(), new(net.DNSConfigError))
    39  	msgpack.RegisterExt(ExtID(), new(net.DNSError))
    40  	msgpack.RegisterExt(ExtID(), new(net.InvalidAddrError))
    41  	msgpack.RegisterExt(ExtID(), new(net.OpError))
    42  	msgpack.RegisterExt(ExtID(), new(net.ParseError))
    43  	msgpack.RegisterExt(ExtID(), new(net.UnknownNetworkError))
    44  
    45  	msgpack.RegisterExt(ExtID(), new(url.Error))
    46  	msgpack.RegisterExt(ExtID(), new(url.EscapeError))
    47  	msgpack.RegisterExt(ExtID(), new(url.InvalidHostError))
    48  
    49  	msgpack.RegisterExt(ExtID(), new(tls.RecordHeaderError))
    50  	msgpack.RegisterExt(ExtID(), new(x509.CertificateInvalidError))
    51  	msgpack.RegisterExt(ExtID(), new(x509.HostnameError))
    52  	msgpack.RegisterExt(ExtID(), new(x509.UnknownAuthorityError))
    53  	msgpack.RegisterExt(ExtID(), new(x509.SystemRootsError))
    54  	msgpack.RegisterExt(ExtID(), new(x509.InsecureAlgorithmError))
    55  	msgpack.RegisterExt(ExtID(), new(x509.ConstraintViolationError))
    56  
    57  	msgpack.Register(errors.New(""), func(e *msgpack.Encoder, v reflect.Value) error {
    58  		if v.IsNil() {
    59  			return e.EncodeNil()
    60  		}
    61  		return e.EncodeString(v.Interface().(error).Error())
    62  	}, nil)
    63  }
    64  
    65  // Error is interface of Checker errors
    66  type Error interface {
    67  	error
    68  	Stage() SendMailStage
    69  	Unwrap() error
    70  }
    71  
    72  // AliasError is alias to fix msgpack
    73  type AliasError error
    74  
    75  // ASMTPError isa abstract struct for Checker errors
    76  type ASMTPError struct {
    77  	stage SendMailStage
    78  	err   error
    79  }
    80  
    81  // Stage returns stage of error
    82  func (a *ASMTPError) Stage() SendMailStage {
    83  	return a.stage
    84  }
    85  
    86  func (a *ASMTPError) Unwrap() error {
    87  	return a.err
    88  }
    89  
    90  func (a *ASMTPError) Error() string {
    91  	return fmt.Sprintf("%v happened on stage \"%v\"", errors.Unwrap(a).Error(), a.Stage())
    92  }
    93  
    94  // EncodeMsgpack implements encoder for msgpack
    95  func (a *ASMTPError) EncodeMsgpack(enc *msgpack.Encoder) error {
    96  	return enc.EncodeMulti(a.stage, a.err)
    97  }
    98  
    99  // DecodeMsgpack implements decoder for msgpack
   100  func (a *ASMTPError) DecodeMsgpack(dec *msgpack.Decoder) error {
   101  	return dec.DecodeMulti(&a.stage, &a.err)
   102  }
   103  
   104  // NewError is constructor for DefaultError
   105  func NewError(stage SendMailStage, err error) Error {
   106  	return &DefaultError{ASMTPError{stage, err}}
   107  }
   108  
   109  // DefaultError is default error
   110  type DefaultError struct {
   111  	ASMTPError
   112  }
   113  
   114  // Convert []AliasError to []error
   115  func _(Errs []AliasError) (errs []error) {
   116  	errs = make([]error, len(Errs))
   117  	for i, Err := range Errs {
   118  		errs[i] = Err
   119  	}
   120  
   121  	return
   122  }
   123  
   124  // ErrorsToEVSMTPErrors converts []error to []AliasError
   125  // It is used like fix of msgpack problems https://github.com/vmihailenco/msgpack/issues/294
   126  func ErrorsToEVSMTPErrors(errs []error) (Errs []AliasError) {
   127  	Errs = make([]AliasError, len(errs))
   128  	for i, err := range errs {
   129  		Errs[i] = err
   130  	}
   131  
   132  	return
   133  }