Debian Codegraph @

pkg/services/email/smtp/smtp_failures.go

raw
1 package smtp
2
3 import (
4 "github.com/nicholas-fedor/shoutrrr/internal/failures"
5 )
6
7 const (
8 // FailUnknown is the default [failures.FailureID].
9 FailUnknown failures.FailureID = iota
10 // FailGetSMTPClient is returned when a SMTP client could not be created.
11 FailGetSMTPClient
12 // FailEnableStartTLS is returned when failing to enable StartTLS.
13 FailEnableStartTLS
14 // FailAuthType is returned when the [Config.Auth] method could not be identified.
15 FailAuthType
16 // FailAuthenticating is returned when the authentication fails.
17 FailAuthenticating
18 // FailSendRecipient is returned when sending to a recipient fails.
19 FailSendRecipient
20 // FailClosingSession is returned when the server doesn't accept the QUIT command.
21 FailClosingSession
22 // FailPlainHeader is returned when the text/plain multipart header could not be set.
23 FailPlainHeader
24 // FailHTMLHeader is returned when the text/html multipart header could not be set.
25 FailHTMLHeader
26 // FailMultiEndHeader is returned when the multipart end header could not be set.
27 FailMultiEndHeader
28 // FailMessageTemplate is returned when the message template could not be written to the stream.
29 FailMessageTemplate
30 // FailMessageRaw is returned when a non-templated message could not be written to the stream.
31 FailMessageRaw
32 // FailSetSender is returned when the server did not accept MAIL FROM.
33 FailSetSender
34 // FailSetRecipient is returned when the server didn't accept the recipient address.
35 FailSetRecipient
36 // FailOpenDataStream is returned when the server did not accept DATA.
37 FailOpenDataStream
38 // FailWriteHeaders is returned when the headers could not be written to the data stream.
39 FailWriteHeaders
40 // FailCloseDataStream is returned when the server didn't accept the data stream contents.
41 FailCloseDataStream
42 // FailConnectToServer is returned when the TCP connection to the server failed.
43 FailConnectToServer
44 // FailCreateSMTPClient is returned when SMTP client initialization failed.
45 FailCreateSMTPClient
46 // FailApplySendParams is returned when updating the send [Config] failed.
47 FailApplySendParams
48 // FailHandshake is returned when the initial HELLO handshake returned an error.
49 FailHandshake
50 // FailResetSession is returned when the server rejects RSET between recipients.
51 FailResetSession
52 )
53
54 // fail creates an SMTP-specific failure with a descriptive message and ID.
55 //
56 // The message is selected from failureID.
57 // Optional args are interpolated into the message when it contains format verbs.
58 //
59 // Parameters:
60 // - failureID: Identifier selecting the failure message ([failures.FailureID]).
61 // - err: The underlying error to wrap, which may be nil.
62 // - args: Optional values interpolated into the failure message.
63 //
64 // Returns:
65 // - A failure wrapping err with the selected message and ID.
66 func fail(failureID failures.FailureID, err error, args ...any) failures.Failure {
67 var msg string
68
69 switch failureID {
70 case FailGetSMTPClient:
71 msg = "error getting SMTP client"
72 case FailConnectToServer:
73 msg = "error connecting to server"
74 case FailCreateSMTPClient:
75 msg = "error creating smtp client"
76 case FailEnableStartTLS:
77 msg = "error enabling StartTLS"
78 case FailAuthenticating:
79 msg = "error authenticating"
80 case FailAuthType:
81 msg = "invalid authentication method '%s'"
82 case FailSendRecipient:
83 msg = "error sending message to recipient %q"
84 case FailClosingSession:
85 msg = "error closing session"
86 case FailPlainHeader:
87 msg = "error writing plain header"
88 case FailHTMLHeader:
89 msg = "error writing HTML header"
90 case FailMultiEndHeader:
91 msg = "error writing multipart end header"
92 case FailMessageTemplate:
93 msg = "error applying message template"
94 case FailMessageRaw:
95 msg = "error writing message"
96 case FailSetSender:
97 msg = "error setting MAIL FROM"
98 case FailSetRecipient:
99 msg = "error setting RCPT"
100 case FailOpenDataStream:
101 msg = "error starting DATA"
102 case FailWriteHeaders:
103 msg = "error writing message headers"
104 case FailCloseDataStream:
105 msg = "error closing message stream"
106 case FailApplySendParams:
107 msg = "error applying params to send config"
108 case FailHandshake:
109 msg = "server did not accept the handshake"
110 case FailResetSession:
111 msg = "error resetting session between recipients"
112 // case FailUnknown:
113 default:
114 msg = "an unknown error occurred"
115 }
116
117 return failures.Wrap(msg, failureID, err, args...)
118 }