summaryrefslogtreecommitdiff
path: root/kcore
diff options
context:
space:
mode:
authorMartin Kagamino Lehoux <martin@lehoux.net>2025-12-24 19:20:25 +0100
committerMartin Kagamino Lehoux <martin@lehoux.net>2025-12-24 19:20:25 +0100
commitc1b78bf43b1f20706c787061d7238e7acd8b5b2d (patch)
tree936dba9ae768c0fedf4456db4d0392826072ccff /kcore
parent6d05a64a0a40df152b218d76c9247c1aa743c879 (diff)
Implements Must function and enhances Unauthorized handlerv0.7.0
Adds the Must function for error handling, simplifying code by allowing panic on errors. Improves the Unauthorized handler to ensure proper error reporting when writing responses.
Diffstat (limited to 'kcore')
-rw-r--r--kcore/error.go7
-rw-r--r--kcore/http.go8
2 files changed, 15 insertions, 0 deletions
diff --git a/kcore/error.go b/kcore/error.go
index fad5f3d..3e4ee1f 100644
--- a/kcore/error.go
+++ b/kcore/error.go
@@ -29,3 +29,10 @@ func Assert(cond bool, msg string) {
panic(err)
}
}
+
+func Must[T any](val T, err error) T {
+ if err != nil {
+ panic(err)
+ }
+ return val
+}
diff --git a/kcore/http.go b/kcore/http.go
index e5d218b..f1a2b1a 100644
--- a/kcore/http.go
+++ b/kcore/http.go
@@ -26,3 +26,11 @@ func RenderPage(ctx context.Context, page templ.Component, w http.ResponseWriter
_, err = buf.WriteTo(w)
Expect(err, "error writing page to response writer")
}
+
+func Unauthorized(w http.ResponseWriter, err error) {
+ w.WriteHeader(http.StatusUnauthorized)
+ if err != nil {
+ _, err = w.Write([]byte(err.Error()))
+ Expect(err, "error writing response")
+ }
+}