Skip to content

Commit

Permalink
return ErrNoCookie to ensure that errors arent hidden
Browse files Browse the repository at this point in the history
  • Loading branch information
syntaqx committed Jun 29, 2024
1 parent b9e8333 commit 7e946fc
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
5 changes: 4 additions & 1 deletion cookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 2 additions & 6 deletions cookie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down

0 comments on commit 7e946fc

Please sign in to comment.