better error pages
This commit is contained in:
51
routes/errors/errors.go
Normal file
51
routes/errors/errors.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package errors
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"text/template"
|
||||
|
||||
"github.com/hacdias/caddy-hugo/tools/server"
|
||||
"github.com/hacdias/caddy-hugo/tools/templates"
|
||||
"github.com/hacdias/caddy-hugo/tools/variables"
|
||||
)
|
||||
|
||||
type errorInformation struct {
|
||||
Title string `json:"title"`
|
||||
Message string `json:"message"`
|
||||
err error
|
||||
}
|
||||
|
||||
// ServeHTTP is used to serve the content of GIT API.
|
||||
func ServeHTTP(w http.ResponseWriter, r *http.Request, code int, err error) (int, error) {
|
||||
page := new(errorInformation)
|
||||
page.Title = strconv.Itoa(code) + " " + http.StatusText(code)
|
||||
page.err = err
|
||||
|
||||
if err != nil {
|
||||
page.Message = err.Error()
|
||||
}
|
||||
|
||||
switch r.Method {
|
||||
case "GET":
|
||||
functions := template.FuncMap{
|
||||
"Defined": variables.Defined,
|
||||
}
|
||||
|
||||
tpl, err := templates.Get(r, functions, "error")
|
||||
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
err = tpl.Execute(w, page)
|
||||
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
return 0, page.err
|
||||
default:
|
||||
return server.RespondJSON(w, page, code, err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user