2017-10-08 04:48:58 +00:00
|
|
|
package core
|
|
|
|
|
2017-10-31 16:42:15 +00:00
|
|
|
import (
|
|
|
|
"net/http"
|
2018-02-10 18:08:45 +00:00
|
|
|
|
|
|
|
"github.com/kirsle/blog/core/internal/log"
|
|
|
|
"github.com/kirsle/blog/core/internal/render"
|
2017-10-31 16:42:15 +00:00
|
|
|
)
|
2017-10-08 04:48:58 +00:00
|
|
|
|
2017-10-31 16:42:15 +00:00
|
|
|
// NotFound sends a 404 response.
|
2018-02-10 19:14:42 +00:00
|
|
|
func (b *Blog) NotFound(w http.ResponseWriter, r *http.Request, message string) {
|
|
|
|
if message == "" {
|
|
|
|
message = "The page you were looking for was not found."
|
2017-10-31 16:42:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
2018-02-10 22:05:41 +00:00
|
|
|
err := render.Template(w, r, ".errors/404", map[string]string{
|
|
|
|
"Message": message,
|
2017-10-31 16:42:15 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err.Error())
|
2017-11-03 16:52:40 +00:00
|
|
|
w.Write([]byte("Unrecoverable template error for NotFound()"))
|
2017-10-31 16:42:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-03 16:52:40 +00:00
|
|
|
// Forbidden sends an HTTP 403 Forbidden response.
|
2018-02-10 19:14:42 +00:00
|
|
|
func (b *Blog) Forbidden(w http.ResponseWriter, r *http.Request, message string) {
|
2017-10-31 16:42:15 +00:00
|
|
|
w.WriteHeader(http.StatusForbidden)
|
2018-02-10 22:05:41 +00:00
|
|
|
err := render.Template(w, r, ".errors/403", map[string]string{
|
|
|
|
"Message": message,
|
2017-11-24 19:56:32 +00:00
|
|
|
})
|
2017-10-31 16:42:15 +00:00
|
|
|
if err != nil {
|
2017-11-03 16:52:40 +00:00
|
|
|
log.Error(err.Error())
|
|
|
|
w.Write([]byte("Unrecoverable template error for Forbidden()"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-02 18:47:21 +00:00
|
|
|
// Error sends an HTTP 500 Internal Server Error response.
|
2018-02-10 19:14:42 +00:00
|
|
|
func (b *Blog) Error(w http.ResponseWriter, r *http.Request, message string) {
|
2017-12-02 18:47:21 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2018-02-10 22:05:41 +00:00
|
|
|
err := render.Template(w, r, ".errors/500", map[string]string{
|
|
|
|
"Message": message,
|
2017-12-02 18:47:21 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err.Error())
|
|
|
|
w.Write([]byte("Unrecoverable template error for Error()"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-03 16:52:40 +00:00
|
|
|
// BadRequest sends an HTTP 400 Bad Request.
|
2018-02-10 19:14:42 +00:00
|
|
|
func (b *Blog) BadRequest(w http.ResponseWriter, r *http.Request, message string) {
|
2017-11-03 16:52:40 +00:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
2018-02-10 22:05:41 +00:00
|
|
|
err := render.Template(w, r, ".errors/400", map[string]string{
|
|
|
|
"Message": message,
|
2017-11-03 16:52:40 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err.Error())
|
|
|
|
w.Write([]byte("Unrecoverable template error for BadRequest()"))
|
2017-10-31 16:42:15 +00:00
|
|
|
}
|
|
|
|
}
|