feat(surf): branded HTML error pages for browser navigations
A browser navigation (Sec-Fetch-Mode navigate, or GET/HEAD without it; no X-Requested-With; Accept naming text/html above every JSON type; not /api, /_<name> or the admin prefix) that hits a framework-generated 404, 405 or 500 gets a self-contained SummerCMS page: unmatched routes and wrong methods at the ServeMux, failed Where/WhereIn constraints, overlap-family misses, the unpublished uploads route and recovered panics. Every other request keeps the exact stdlib text, wire's opaque JSON 500 or the bare raw-group 500. Details (method, path, X-Request-Id) only with app.debug or outside production. Applications replace the page with a published surf.ErrorPageRenderer; http.error_pages.enabled false turns it off.
This commit is contained in:
@@ -391,6 +391,7 @@ type familyDispatch struct {
|
||||
members []familyMember
|
||||
methods []string // every method in the router's table, for Allow
|
||||
mux *http.ServeMux
|
||||
pages *errorPages // nil keeps the stdlib 404/405 text
|
||||
}
|
||||
|
||||
func (f *familyDispatch) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -421,16 +422,16 @@ func (f *familyDispatch) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
if methodSeen {
|
||||
http.NotFound(w, r)
|
||||
f.pages.notFound(w, r)
|
||||
return
|
||||
}
|
||||
allow := f.allowed(r, vals)
|
||||
if len(allow) == 0 {
|
||||
http.NotFound(w, r)
|
||||
f.pages.notFound(w, r)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Allow", strings.Join(allow, ", "))
|
||||
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
|
||||
f.pages.methodNotAllowed(w, r)
|
||||
}
|
||||
|
||||
// allowed lists the methods ServeMux would put in Allow for this path if the
|
||||
@@ -466,7 +467,7 @@ func (f *familyDispatch) allowed(r *http.Request, vals []string) []string {
|
||||
|
||||
// registerOverlapFamilies registers each family pattern on mux, then checks
|
||||
// that no other route takes a member's requests away from the family.
|
||||
func registerOverlapFamilies(mux *http.ServeMux, routes []route, handlers []http.Handler, fams []overlapFamily) error {
|
||||
func registerOverlapFamilies(mux *http.ServeMux, pages *errorPages, routes []route, handlers []http.Handler, fams []overlapFamily) error {
|
||||
if len(fams) == 0 {
|
||||
return nil
|
||||
}
|
||||
@@ -480,7 +481,7 @@ func registerOverlapFamilies(mux *http.ServeMux, routes []route, handlers []http
|
||||
}
|
||||
sort.Strings(methods)
|
||||
for _, fam := range fams {
|
||||
d := &familyDispatch{pattern: fam.pattern, wild: fam.wild, methods: methods, mux: mux}
|
||||
d := &familyDispatch{pattern: fam.pattern, wild: fam.wild, methods: methods, mux: mux, pages: pages}
|
||||
d.lits = make([]string, len(fam.wild))
|
||||
for _, idx := range fam.members {
|
||||
rt := routes[idx]
|
||||
|
||||
Reference in New Issue
Block a user