From 1438699b7358a781cefb9438fe102c48bc836083 Mon Sep 17 00:00:00 2001
From: 6543 <6543@obermui.de>
Date: Mon, 7 Nov 2022 23:37:52 +0100
Subject: [PATCH] nice error pages
---
html/404.html | 4 ++--
html/error.go | 13 ++++++++-----
html/error.html | 38 ++++++++++++++++++++++++++++++++++++++
html/html.go | 3 +++
4 files changed, 51 insertions(+), 7 deletions(-)
create mode 100644 html/error.html
diff --git a/html/404.html b/html/404.html
index b7ec96e..ca432fc 100644
--- a/html/404.html
+++ b/html/404.html
@@ -3,7 +3,7 @@
- %status
+ %status%
@@ -26,7 +26,7 @@
Page not found!
- Sorry, but this page couldn't be found or is inaccessible (%status).
+ Sorry, but this page couldn't be found or is inaccessible (%status%).
We hope this isn't a problem on our end ;) - Make sure to check the troubleshooting section in the Docs !
diff --git a/html/error.go b/html/error.go
index a0d0ae6..4194533 100644
--- a/html/error.go
+++ b/html/error.go
@@ -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))
}
diff --git a/html/error.html b/html/error.html
new file mode 100644
index 0000000..c84e12d
--- /dev/null
+++ b/html/error.html
@@ -0,0 +1,38 @@
+
+
+
+
+
+ %status%
+
+
+
+
+
+
+
+
+
+
+ %status%!
+
+
+ Sorry, but this page couldn't be served.
+ We got an "%message%" error.
+ We hope this isn't a problem on our end ;) - Make sure to check the troubleshooting section in the Docs !
+
+
+
+ Static pages made easy - Codeberg Pages
+
+
+
diff --git a/html/html.go b/html/html.go
index 5e3842a..a76ce59 100644
--- a/html/html.go
+++ b/html/html.go
@@ -4,3 +4,6 @@ import _ "embed"
//go:embed 404.html
var NotFoundPage string
+
+//go:embed error.html
+var ErrorPage string