Browse Source

Finished stylizing generic error page

main
Artemis 2 years ago
parent
commit
b8071792dc
  1. 3
      config.go
  2. 21
      handlers.go
  3. 26
      templates/http-error.html

3
config.go

@ -4,9 +4,6 @@ import (
"github.com/spf13/viper"
)
// TODO remove
var NotFoundErr = "This document could not be found. Maybe it expired?"
// ProjectName is the binary name
var ProjectName = "paste"

21
handlers.go

@ -38,10 +38,15 @@ var PasteSubmitSchema = map[string][]validator.Rule{
"time": {rules.Required{}, rules.Choice{Choices: []string{"1h", "6h", "12h", "24h"}}},
}
var genericErrors = map[int]string{
http.StatusBadRequest: "Bad request",
http.StatusNotFound: "File not found",
http.StatusInternalServerError: "Internal server error",
type GenericError struct {
Title string
Description string
}
var genericErrors = map[int]GenericError{
http.StatusBadRequest: {"Bad request", "You sent a request the server cannot handle."},
http.StatusNotFound: {"Page not found", "This document could not be found. Maybe it expired?"},
http.StatusInternalServerError: {"Internal server error", "The server crashed during the request."},
}
// endregion
@ -186,7 +191,7 @@ func (h Handlers) handleShowPaste(w http.ResponseWriter, req *http.Request) {
h.GenericHTTPError(500, err.Error())(w, req)
return
} else if res == "" {
h.GenericHTTPError(404, NotFoundErr)(w, req)
h.GenericHTTPError(404, nil)(w, req)
return
}
@ -204,7 +209,7 @@ func (h Handlers) handleShowRaw(w http.ResponseWriter, req *http.Request) {
h.GenericHTTPError(500, err.Error())(w, req)
return
} else if res == "" {
h.GenericHTTPError(404, NotFoundErr)(w, req)
h.GenericHTTPError(404, nil)(w, req)
return
}
@ -222,13 +227,13 @@ func (h Handlers) handleShowRaw(w http.ResponseWriter, req *http.Request) {
// - writes the generic template `http-error` with the status code and message
func (h *Handlers) GenericHTTPError(
statusCode int,
error string,
error interface{},
) func(w http.ResponseWriter, req *http.Request) {
ErrIf(error)
return func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(statusCode)
ErrIf(h.Templates.ExecuteTemplate(w, "http-error", map[string]string{
ErrIf(h.Templates.ExecuteTemplate(w, "http-error", map[string]interface{}{
"Error": genericErrors[statusCode],
}))
}

26
templates/http-error.html

@ -2,13 +2,31 @@
<!DOCTYPE html>
<html lang="en">
<head>
{{template "header"}}
{{template "header"}}
<style>
main.error {
margin-top: 10em;
text-align: center;
font-family: monospace, monospace;
}
h1::before {
content: '';
}
@media screen and (min-width: 700px) {
main.error {
width: 700px;
margin: 10em auto auto auto;
}
}
</style>
</head>
<body>
<main class="error">
{{/* TODO generic error */}}
<h1>500 Internal Server Error</h1>
<p>The server crashed during the request</p>
<h1 class="text-3xl">{{.Error.Title}}</h1>
<p>{{.Error.Description}}</p>
<a href="/" class="underline font-medium text-blue-500">Back to home page</a>
</main>
</body>
</html>

Loading…
Cancel
Save