2020-07-31 19:02:47 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2021-03-10 10:29:20 +00:00
|
|
|
|
2021-06-18 12:32:03 +00:00
|
|
|
ct "github.com/elnormous/contenttype"
|
2021-06-28 20:17:18 +00:00
|
|
|
"go.goblog.app/app/pkgs/contenttype"
|
2020-07-31 19:02:47 +00:00
|
|
|
)
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (a *goBlog) serve404(w http.ResponseWriter, r *http.Request) {
|
2022-11-27 14:06:43 +00:00
|
|
|
a.serveError(w, r, fmt.Sprintf("%s was not found", r.URL.RequestURI()), http.StatusNotFound)
|
2020-12-24 09:09:34 +00:00
|
|
|
}
|
|
|
|
|
2021-07-23 07:53:35 +00:00
|
|
|
func (a *goBlog) serve410(w http.ResponseWriter, r *http.Request) {
|
2022-11-27 14:06:43 +00:00
|
|
|
a.serveError(w, r, fmt.Sprintf("%s doesn't exist anymore", r.URL.RequestURI()), http.StatusGone)
|
2021-07-23 07:53:35 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (a *goBlog) serveNotAllowed(w http.ResponseWriter, r *http.Request) {
|
|
|
|
a.serveError(w, r, "", http.StatusMethodNotAllowed)
|
2021-03-22 14:57:29 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (a *goBlog) serveError(w http.ResponseWriter, r *http.Request, message string, status int) {
|
2021-06-19 06:37:16 +00:00
|
|
|
// Init the first time
|
|
|
|
if len(a.errorCheckMediaTypes) == 0 {
|
|
|
|
a.errorCheckMediaTypes = append(a.errorCheckMediaTypes, ct.NewMediaType(contenttype.HTML))
|
2020-12-24 09:09:34 +00:00
|
|
|
}
|
2021-06-19 06:37:16 +00:00
|
|
|
// Check message
|
2020-12-24 09:09:34 +00:00
|
|
|
if message == "" {
|
|
|
|
message = http.StatusText(status)
|
|
|
|
}
|
2021-06-19 06:37:16 +00:00
|
|
|
// Check if request accepts HTML
|
|
|
|
if mt, _, err := ct.GetAcceptableMediaType(r, a.errorCheckMediaTypes); err != nil || mt.String() != a.errorCheckMediaTypes[0].String() {
|
|
|
|
// Request doesn't accept HTML
|
|
|
|
http.Error(w, message, status)
|
|
|
|
return
|
|
|
|
}
|
2022-01-30 15:40:53 +00:00
|
|
|
a.renderWithStatusCode(w, r, status, a.renderError, &renderData{
|
2022-01-20 17:22:10 +00:00
|
|
|
Data: &errorRenderData{
|
2021-06-19 06:37:16 +00:00
|
|
|
Title: fmt.Sprintf("%d %s", status, http.StatusText(status)),
|
2020-12-24 09:09:34 +00:00
|
|
|
Message: message,
|
2020-10-12 16:47:23 +00:00
|
|
|
},
|
2020-07-31 19:02:47 +00:00
|
|
|
})
|
|
|
|
}
|