From 7e946fc2709115bef5a72fe78c5d2ef1256526dc Mon Sep 17 00:00:00 2001 From: Chase Pierce Date: Fri, 28 Jun 2024 18:04:56 -0600 Subject: [PATCH] return ErrNoCookie to ensure that errors arent hidden --- cookie.go | 5 ++++- cookie_test.go | 8 ++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/cookie.go b/cookie.go index 60e6e99..033709e 100644 --- a/cookie.go +++ b/cookie.go @@ -15,6 +15,9 @@ const ( CookieTag = "cookie" ) +// ErrNoCookie is returned when a cookie is not found. +var ErrNoCookie = http.ErrNoCookie + // UnsupportedTypeError is returned when a field type is not supported by PopulateFromCookies. type UnsupportedTypeError struct { Type reflect.Type @@ -80,7 +83,7 @@ func PopulateFromCookies(r *http.Request, dest interface{}) error { cookie, err := Get(r, tag) if err != nil { if errors.Is(err, http.ErrNoCookie) { - continue // Skip if the cookie is not found + return ErrNoCookie } return err } diff --git a/cookie_test.go b/cookie_test.go index 87ad24f..fdbba73 100644 --- a/cookie_test.go +++ b/cookie_test.go @@ -316,12 +316,8 @@ func TestPopulateFromCookies_NotFound(t *testing.T) { dest := &MyStruct{} err := PopulateFromCookies(r, dest) - if err != nil { - t.Fatalf("Unexpected error: %v", err) - } - - if dest.StringField != "" { - t.Errorf("Expected StringField to be empty, got %s", dest.StringField) + if err != ErrNoCookie { + t.Errorf("Expected error ErrNoCookie, got %v", err) } }