feat(cookies): UnmarshalIfRequest helper/alias function
This commit is contained in:
@@ -2,7 +2,6 @@ package pages
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"errors"
|
||||
"log"
|
||||
"strconv"
|
||||
|
||||
@@ -32,15 +31,10 @@ func (p *Dashboard) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
hasCookie := true
|
||||
|
||||
var c DashboardCookie
|
||||
if err := cookies.UnmarshalRequest(r, &c); errors.Is(err, cookies.ErrNoCookie{}) {
|
||||
hasCookie = false
|
||||
c = DashboardCookie{Hello: "Hello world", Bool: true, Test: 69420}
|
||||
} else if err != nil {
|
||||
c := DashboardCookie{"hello world", true, 0}
|
||||
if _, err := cookies.UnmarshalIfRequest(r, &c); err != nil {
|
||||
rerrors.InternalError(err).ServeHTTP(w, r)
|
||||
return
|
||||
} else {
|
||||
hasCookie = true
|
||||
}
|
||||
|
||||
log.Print(hasCookie, c)
|
||||
|
||||
@@ -73,6 +73,18 @@ func UnmarshalRequest(r *http.Request, v any) error {
|
||||
return Unmarshal(c, v)
|
||||
}
|
||||
|
||||
func UnmarshalIfRequest(r *http.Request, v any) (bool, error) {
|
||||
if err := UnmarshalRequest(r, v); err != nil {
|
||||
if _, ok := err.(ErrNoCookie); ok {
|
||||
return false, nil
|
||||
} else {
|
||||
return true, err
|
||||
}
|
||||
} else {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
|
||||
func RerrUnmarshalCookie(err error) rerrors.RouteError {
|
||||
if e, ok := err.(ErrNoCookie); ok {
|
||||
return rerrors.MissingCookies([]string{e.name})
|
||||
|
||||
Reference in New Issue
Block a user