httpx: Add new redirect

This commit is contained in:
Tom Wiesing 2022-11-24 14:19:17 +01:00
parent 41d41035e3
commit 8e2d2cce3e
No known key found for this signature in database
5 changed files with 123 additions and 35 deletions

View file

@ -12,27 +12,13 @@ type HTMLHandler[T any] struct {
TemplateName string // name of template to render, defaults to root
}
var htmlInternalServerErr = []byte(`<!DOCTYPE HTML><title>Internal Server Error</title>Internal Server Error`)
var htmlNotFound = []byte(`<!DOCTYPE HTML><title>Not Found</title>Not Found`)
// ServeHTTP calls j(r) and returns json
func (h HTMLHandler[T]) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// call the function
result, err := h.Handler(r)
// entity not found
if err == ErrNotFound {
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusNotFound)
w.Write(htmlNotFound)
return
}
// handle other errors
if err != nil {
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusInternalServerError)
w.Write(htmlInternalServerErr)
// intercept any errors
if htmlInterceptor.Intercept(w, r, err) {
return
}