From 590dfe97e77c2627c6889d59a99d812f082c79e5 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Fri, 16 Aug 2024 10:16:53 +0200 Subject: [PATCH 1/2] Refactor error handling for nil template pointers Replace fmt.Errorf with errors.New for consistency in error handling. This change ensures a more streamlined and uniform error reporting approach across the codebase. Also updated a test case from t.Errorf to t.Error for better readability. --- b64linebreaker.go | 4 ++-- msg.go | 12 ++++++------ msgwriter_test.go | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/b64linebreaker.go b/b64linebreaker.go index f5fb967..088b38e 100644 --- a/b64linebreaker.go +++ b/b64linebreaker.go @@ -5,7 +5,7 @@ package mail import ( - "fmt" + "errors" "io" ) @@ -26,7 +26,7 @@ var newlineBytes = []byte(SingleNewLine) // line length is reached func (l *Base64LineBreaker) Write(data []byte) (numBytes int, err error) { if l.out == nil { - err = fmt.Errorf(ErrNoOutWriter) + err = errors.New(ErrNoOutWriter) return } if l.used+len(data) < MaxBodyLength { diff --git a/msg.go b/msg.go index c0333f2..a909d04 100644 --- a/msg.go +++ b/msg.go @@ -743,7 +743,7 @@ func (m *Msg) SetBodyWriter( // The content type will be set to text/html automatically func (m *Msg) SetBodyHTMLTemplate(tpl *ht.Template, data interface{}, opts ...PartOption) error { if tpl == nil { - return fmt.Errorf(errTplPointerNil) + return errors.New(errTplPointerNil) } buffer := bytes.Buffer{} if err := tpl.Execute(&buffer, data); err != nil { @@ -758,7 +758,7 @@ func (m *Msg) SetBodyHTMLTemplate(tpl *ht.Template, data interface{}, opts ...Pa // The content type will be set to text/plain automatically func (m *Msg) SetBodyTextTemplate(tpl *tt.Template, data interface{}, opts ...PartOption) error { if tpl == nil { - return fmt.Errorf(errTplPointerNil) + return errors.New(errTplPointerNil) } buf := bytes.Buffer{} if err := tpl.Execute(&buf, data); err != nil { @@ -790,7 +790,7 @@ func (m *Msg) AddAlternativeWriter( // The content type will be set to text/html automatically func (m *Msg) AddAlternativeHTMLTemplate(tpl *ht.Template, data interface{}, opts ...PartOption) error { if tpl == nil { - return fmt.Errorf(errTplPointerNil) + return errors.New(errTplPointerNil) } buffer := bytes.Buffer{} if err := tpl.Execute(&buffer, data); err != nil { @@ -805,7 +805,7 @@ func (m *Msg) AddAlternativeHTMLTemplate(tpl *ht.Template, data interface{}, opt // The content type will be set to text/plain automatically func (m *Msg) AddAlternativeTextTemplate(tpl *tt.Template, data interface{}, opts ...PartOption) error { if tpl == nil { - return fmt.Errorf(errTplPointerNil) + return errors.New(errTplPointerNil) } buffer := bytes.Buffer{} if err := tpl.Execute(&buffer, data); err != nil { @@ -1314,7 +1314,7 @@ func fileFromReadSeeker(name string, reader io.ReadSeeker) *File { // fileFromHTMLTemplate returns a File pointer form a given html/template.Template func fileFromHTMLTemplate(name string, tpl *ht.Template, data interface{}) (*File, error) { if tpl == nil { - return nil, fmt.Errorf(errTplPointerNil) + return nil, errors.New(errTplPointerNil) } buffer := bytes.Buffer{} if err := tpl.Execute(&buffer, data); err != nil { @@ -1326,7 +1326,7 @@ func fileFromHTMLTemplate(name string, tpl *ht.Template, data interface{}) (*Fil // fileFromTextTemplate returns a File pointer form a given text/template.Template func fileFromTextTemplate(name string, tpl *tt.Template, data interface{}) (*File, error) { if tpl == nil { - return nil, fmt.Errorf(errTplPointerNil) + return nil, errors.New(errTplPointerNil) } buffer := bytes.Buffer{} if err := tpl.Execute(&buffer, data); err != nil { diff --git a/msgwriter_test.go b/msgwriter_test.go index e6582fb..a41e5d3 100644 --- a/msgwriter_test.go +++ b/msgwriter_test.go @@ -122,7 +122,7 @@ func TestMsgWriter_writeMsg(t *testing.T) { em += fmt.Sprintf("* incorrect %q field", ea[e]) } em += fmt.Sprintf("\n\nFull message:\n%s", ms) - t.Errorf(em) + t.Error(em) } } From e288bcae5e1f3dcd16d0dd2be86ce4b585086fa5 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Fri, 16 Aug 2024 10:17:31 +0200 Subject: [PATCH 2/2] Update GolangCI config to go 1.23 and exclude examples Upgraded the Go version in GolangCI configuration from 1.22 to 1.23. Added the "examples" directory to the exclusion list for linting. --- .golangci.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.golangci.toml b/.golangci.toml index 2501c6a..223dc0b 100644 --- a/.golangci.toml +++ b/.golangci.toml @@ -3,8 +3,9 @@ ## SPDX-License-Identifier: MIT [run] -go = "1.22" +go = "1.23" tests = true +exclude-dirs = ["examples"] [linters] enable = ["stylecheck", "whitespace", "containedctx", "contextcheck", "decorder",