httpx: Add new redirect
This commit is contained in:
parent
41d41035e3
commit
8e2d2cce3e
5 changed files with 123 additions and 35 deletions
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue