wneessen / go-mail

📧 Easy to use, yet comprehensive library for sending mails with Go
https://go-mail.dev
MIT License
571 stars 44 forks source link

Introduction of new error type for sending errors #91

Closed wneessen closed 1 year ago

wneessen commented 1 year ago

This PR introduces the SendError type which implements the error interface as solution for #90.

A new senderror field has been added to the Msg as well, so introduce this type to it.

I've also added different error variables that indicate the different things that can go wrong during mail delivery. These variables can be checked for, for each Msg using the errors.Is and errors.As methods. Additionally, the SendError has a isTemp field that indicates if the delivery error is of temporary nature and can be accessed via the SendError.IsTemp() method.

The Error() method of SendError will return a detailed error string on why the Msg could not be delivered.

Additionally, HasSendError(), SendErrorIsTemp() and SendError() methods have been added to Msg. While HasSendError() simply returns a bool in case a Msg failed during delivery and SendErrorIsTemp() returns true if it's a temporary error, the SendError() will return the full SendError error interface.

As proposed by @iwittkau the SendError type has a IsTemp() method as well indicating to the user if the delivery error is retryable or not.

As suggested, we want to use it in the error response from the Client functions like Send or DialAndSend we need to return the SendError type not only as part of the *Msg but also as return value for these methods. Hence, the changes made for #85 have been overhauled to return the new error type instead. In the pre Go1.20 version of the Send() method we need to return an accumulated version of the SendError type, since we don't have errors.Join() and therefore, if more than one error occurred during the delivery we return an ambiguous error reason since we can't tell which of the captured errors is main error. For more details the user can always check the *Msg.SendError

codecov-commenter commented 1 year ago

Codecov Report

Merging #91 (5897bdd) into main (f454ae8) will increase coverage by 0.02%. The diff coverage is 78.81%.

@@            Coverage Diff             @@
##             main      #91      +/-   ##
==========================================
+ Coverage   83.12%   83.15%   +0.02%     
==========================================
  Files          14       15       +1     
  Lines        1452     1549      +97     
==========================================
+ Hits         1207     1288      +81     
- Misses        170      186      +16     
  Partials       75       75              
Impacted Files Coverage Δ
client_119.go 54.94% <54.90%> (-0.80%) :arrow_down:
msg.go 86.54% <90.00%> (+0.05%) :arrow_up:
senderror.go 98.24% <98.24%> (ø)
client.go 78.04% <0.00%> (ø)

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more

wneessen commented 1 year ago

@iwittkau thanks for comments and review. Appreciated!