feat: page not found

This commit is contained in:
Teajey 2024-11-21 16:33:34 +09:00
parent b48140e9c3
commit 20761920d3
Signed by: Teajey
GPG Key ID: 970E790FE834A713
4 changed files with 8 additions and 14 deletions

View File

@ -11,9 +11,9 @@ type pageProps struct {
} }
func Page(w http.ResponseWriter, publicMessage string, status int, err error) { func Page(w http.ResponseWriter, publicMessage string, status int, err error) {
w.WriteHeader(status)
if err != nil { if err != nil {
log.Printf("%s --- %s\n", publicMessage, err) log.Printf("%s --- %s\n", publicMessage, err)
} }
templates.Execute(w, "error_page.gotmpl", pageProps{publicMessage}) templates.Execute(w, "error_page.gotmpl", pageProps{publicMessage})
http.Error(w, "", http.StatusInternalServerError)
} }

View File

@ -50,6 +50,7 @@ func main() {
r.Html.Private.Handle("POST /list/{userReference}", route.ExpectUser(route.ForeignWishlistPost)) r.Html.Private.Handle("POST /list/{userReference}", route.ExpectUser(route.ForeignWishlistPost))
r.Html.Private.Handle("GET /group/{groupReference}", route.ExpectUser(route.GroupPage)) r.Html.Private.Handle("GET /group/{groupReference}", route.ExpectUser(route.GroupPage))
r.Html.Private.HandleFunc("POST /logout", route.LogoutPost) r.Html.Private.HandleFunc("POST /logout", route.LogoutPost)
r.Html.Private.HandleFunc("GET /", routing.NotFound)
r.Json.Public.HandleFunc("GET /", routing.NotFoundJson) r.Json.Public.HandleFunc("GET /", routing.NotFoundJson)

View File

@ -1,13 +0,0 @@
package routing
import (
"encoding/json"
"net/http"
)
func decodeJsonParams(r *http.Request, v any) error {
dec := json.NewDecoder(r.Body)
dec.DisallowUnknownFields()
err := dec.Decode(&v)
return err
}

View File

@ -2,6 +2,8 @@ package routing
import ( import (
"net/http" "net/http"
"lishwist/error"
) )
func NotFoundJson(w http.ResponseWriter, r *http.Request) { func NotFoundJson(w http.ResponseWriter, r *http.Request) {
@ -9,3 +11,7 @@ func NotFoundJson(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
_, _ = w.Write([]byte(`{"GeneralError":"Not Found"}`)) _, _ = w.Write([]byte(`{"GeneralError":"Not Found"}`))
} }
func NotFound(w http.ResponseWriter, r *http.Request) {
error.Page(w, "404 -- Page not found", http.StatusNotFound, nil)
}