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:
@@ -68,6 +68,9 @@ type Router struct {
|
||||
app *backpack.App
|
||||
adminPrefix string
|
||||
adminMounted bool
|
||||
// pages writes the HTML error page for browser navigations; nil keeps
|
||||
// every error response as it was (errorpage.go).
|
||||
pages *errorPages
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -381,10 +384,10 @@ func (r *Router) compile() (http.Handler, error) {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if err := registerOverlapFamilies(mux, r.routes, handlers, families); err != nil {
|
||||
if err := registerOverlapFamilies(mux, r.pages, r.routes, handlers, families); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
h := r.stripLocalePrefix(pathScopedCORS(r.corsCfg, mux))
|
||||
h := r.stripLocalePrefix(pathScopedCORS(r.corsCfg, r.pages.fallback(mux)))
|
||||
if accessLogEnabled(r.app) {
|
||||
h = accessLog(r.app, h)
|
||||
}
|
||||
@@ -405,10 +408,10 @@ func handleRoute(mux *http.ServeMux, rt route, h http.Handler) (err error) {
|
||||
func (r *Router) wrap(rt route) (http.Handler, error) {
|
||||
if rt.preflight {
|
||||
// Preflights carry no credentials: no auth, throttle, body or locale.
|
||||
h := recoverBare(constrain(rt.handler, rt.constraints))
|
||||
h := recoverBare(r.pages, constrain(r.pages, rt.handler, rt.constraints))
|
||||
return recordPattern(rt.method+" "+rt.path, h), nil
|
||||
}
|
||||
h := constrain(rt.handler, rt.constraints)
|
||||
h := constrain(r.pages, rt.handler, rt.constraints)
|
||||
limit, err := routeBodyLimit(rt, r.defaultBytes)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("surf: plugin %q: %w", rt.pluginID, err)
|
||||
@@ -473,9 +476,9 @@ func (r *Router) wrap(rt route) (http.Handler, error) {
|
||||
}
|
||||
h = r.locale(h)
|
||||
if rt.raw {
|
||||
h = recoverBare(h)
|
||||
h = recoverBare(r.pages, h)
|
||||
} else {
|
||||
h = recoverJSON(h)
|
||||
h = recoverJSON(r.pages, h)
|
||||
}
|
||||
// The CORS profile wraps recovery, so a refusal from a guard listed
|
||||
// before it, a 413 from the body limit and a recovered 500 all carry
|
||||
@@ -502,6 +505,7 @@ func Assemble(app *backpack.App, plugins []party.Plugin) (http.Handler, error) {
|
||||
func BuildRouter(app *backpack.App, plugins []party.Plugin) (*Router, error) {
|
||||
r := New(corsOrigins(app))
|
||||
r.app = app
|
||||
r.pages = newErrorPages(app)
|
||||
trusted := TrustedProxies(nil)
|
||||
if app != nil {
|
||||
trusted = TrustedProxies(app.Config)
|
||||
@@ -645,6 +649,7 @@ func BuildRouter(app *backpack.App, plugins []party.Plugin) (*Router, error) {
|
||||
return nil, err
|
||||
}
|
||||
r.adminPrefix = admin.Prefix
|
||||
r.pages.setAdminPrefix(admin.Prefix)
|
||||
r.adminMounted = true
|
||||
}
|
||||
for _, rt := range r.routes {
|
||||
@@ -775,12 +780,19 @@ func corsOrigins(app *backpack.App) []string {
|
||||
}
|
||||
}
|
||||
|
||||
func recoverJSON(next http.Handler) http.Handler {
|
||||
// recoverJSON turns a panic into wire's opaque JSON 500, or the HTML error
|
||||
// page for a browser navigation (pages permitting). The panic value is never
|
||||
// written.
|
||||
func recoverJSON(pages *errorPages, next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
buffered := newBufferedResponse()
|
||||
defer func() {
|
||||
if rec := recover(); rec != nil {
|
||||
markPanicked(r)
|
||||
if pages.wants(r) {
|
||||
pages.write(w, r, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
wire.WriteOpaque500(w)
|
||||
return
|
||||
}
|
||||
@@ -790,12 +802,18 @@ func recoverJSON(next http.Handler) http.Handler {
|
||||
})
|
||||
}
|
||||
|
||||
func recoverBare(next http.Handler) http.Handler {
|
||||
// recoverBare turns a panic into a bare 500, or the HTML error page for a
|
||||
// browser navigation (pages permitting).
|
||||
func recoverBare(pages *errorPages, next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
buffered := newBufferedResponse()
|
||||
defer func() {
|
||||
if rec := recover(); rec != nil {
|
||||
markPanicked(r)
|
||||
if pages.wants(r) {
|
||||
pages.write(w, r, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user