nice error pages

This commit is contained in:
6543 2022-11-07 23:37:52 +01:00
parent b473639103
commit 1438699b73
No known key found for this signature in database
GPG key ID: B8BE6D610E61C862
4 changed files with 51 additions and 7 deletions

View file

@ -11,12 +11,15 @@ import (
// ReturnErrorPage sets the response status code and writes NotFoundPage to the response body, with "%status" replaced
// with the provided status code.
func ReturnErrorPage(ctx *context.Context, msg string, code int) {
func ReturnErrorPage(ctx *context.Context, msg string, statusCode int) {
ctx.RespWriter.Header().Set("Content-Type", "text/html; charset=utf-8")
ctx.RespWriter.WriteHeader(code)
ctx.RespWriter.WriteHeader(statusCode)
if msg == "" {
msg = errorBody(code)
msg = errorBody(statusCode)
} else {
// TODO: use template engine
msg = strings.ReplaceAll(strings.ReplaceAll(ErrorPage, "%message%", msg), "%status%", http.StatusText(statusCode))
}
_, _ = io.Copy(ctx.RespWriter, strings.NewReader(msg))
@ -35,9 +38,9 @@ func errorMessage(statusCode int) string {
return message
}
// TODO: use template engine?
// TODO: use template engine
func errorBody(statusCode int) string {
return strings.ReplaceAll(NotFoundPage,
"%status",
"%status%",
strconv.Itoa(statusCode)+" "+errorMessage(statusCode))
}