From 6fca911c8f6e65712f758702585c6f9d5d04de64 Mon Sep 17 00:00:00 2001 From: "Gustavo \"Guz\" L de Mello" Date: Wed, 30 Jul 2025 19:14:42 -0300 Subject: [PATCH] feat(smalltrip,problem): handler template --- smalltrip/problem/handlers.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/smalltrip/problem/handlers.go b/smalltrip/problem/handlers.go index 033317f..b80c700 100644 --- a/smalltrip/problem/handlers.go +++ b/smalltrip/problem/handlers.go @@ -18,6 +18,20 @@ func HandlerAll(p Problem) http.Handler { ProblemMediaTypeJSON: HandlerJSON, }, HandlerJSON) return h(p) + +func HandlerTemplate(template Template) Handler { + return func(p Problem) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(p.Status()) + if err := template.Execute(w, p); err != nil { + _, _ = w.Write(fmt.Appendf([]byte{}, "Failed to execute problem template: %s\n\nPlease report this error to the site's admin.", err.Error())) + } + }) + } +} + +type Template interface { + Execute(w io.Writer, data any) error } func HandlerContentType(handlers map[string]Handler, fallback ...Handler) Handler {