filebrowser/routes/errors/errors.go

52 lines
1.0 KiB
Go
Raw Normal View History

2016-03-12 13:03:31 +00:00
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()
}
2016-06-16 16:01:41 +00:00
if r.Method != http.MethodGet {
return server.RespondJSON(w, page, code, err)
}
2016-03-12 13:03:31 +00:00
2016-06-16 16:01:41 +00:00
functions := template.FuncMap{
"Defined": variables.Defined,
}
2016-03-12 13:03:31 +00:00
2016-06-16 16:01:41 +00:00
var tpl *template.Template
tpl, err = templates.Get(r, functions, "error")
2016-03-12 13:03:31 +00:00
2016-06-16 16:01:41 +00:00
if err != nil {
return http.StatusInternalServerError, err
}
2016-03-12 13:03:31 +00:00
2016-06-16 16:01:41 +00:00
err = tpl.Execute(w, page)
2016-03-12 13:03:31 +00:00
2016-06-16 16:01:41 +00:00
if err != nil {
return http.StatusInternalServerError, err
2016-03-12 13:03:31 +00:00
}
2016-06-16 16:01:41 +00:00
return 0, page.err
2016-03-12 13:03:31 +00:00
}