mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-04-25 14:26:58 +00:00
nice error pages
This commit is contained in:
parent
b473639103
commit
1438699b73
4 changed files with 51 additions and 7 deletions
|
@ -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))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue