summary refs log tree commit diff
path: root/vendor/go.mau.fi/util/exhttp/handleerrors.go
diff options
context:
space:
mode:
authorEmile <git@emile.space>2024-10-25 15:55:50 +0200
committerEmile <git@emile.space>2024-10-25 15:55:50 +0200
commitc90f36e3dd179d2de96f4f5fe38d8dc9a9de6dfe (patch)
tree89e9afb41c5bf76f48cfb09305a2d3db8d302b06 /vendor/go.mau.fi/util/exhttp/handleerrors.go
parent98bbb0f559a8883bc47bae80607dbe326a448e61 (diff)
vendor HEAD main
Diffstat (limited to 'vendor/go.mau.fi/util/exhttp/handleerrors.go')
-rw-r--r--vendor/go.mau.fi/util/exhttp/handleerrors.go58
1 files changed, 58 insertions, 0 deletions
diff --git a/vendor/go.mau.fi/util/exhttp/handleerrors.go b/vendor/go.mau.fi/util/exhttp/handleerrors.go
new file mode 100644
index 0000000..d2d37b1
--- /dev/null
+++ b/vendor/go.mau.fi/util/exhttp/handleerrors.go
@@ -0,0 +1,58 @@
+package exhttp
+
+import "net/http"
+
+type ErrorBodyGenerators struct {
+	NotFound         func() []byte
+	MethodNotAllowed func() []byte
+}
+
+func HandleErrors(next http.Handler, gen ErrorBodyGenerators) http.Handler {
+	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+		next.ServeHTTP(&bodyOverrider{
+			ResponseWriter:                      w,
+			statusNotFoundBodyGenerator:         gen.NotFound,
+			statusMethodNotAllowedBodyGenerator: gen.MethodNotAllowed,
+		}, r)
+	})
+}
+
+type bodyOverrider struct {
+	http.ResponseWriter
+
+	code     int
+	override bool
+
+	statusNotFoundBodyGenerator         func() []byte
+	statusMethodNotAllowedBodyGenerator func() []byte
+}
+
+var _ http.ResponseWriter = (*bodyOverrider)(nil)
+
+func (b *bodyOverrider) WriteHeader(code int) {
+	if b.Header().Get("Content-Type") == "text/plain; charset=utf-8" {
+		b.Header().Set("Content-Type", "application/json")
+
+		b.override = true
+	}
+
+	b.code = code
+	b.ResponseWriter.WriteHeader(code)
+}
+
+func (b *bodyOverrider) Write(body []byte) (int, error) {
+	if b.override {
+		switch b.code {
+		case http.StatusNotFound:
+			if b.statusNotFoundBodyGenerator != nil {
+				body = b.statusNotFoundBodyGenerator()
+			}
+		case http.StatusMethodNotAllowed:
+			if b.statusMethodNotAllowedBodyGenerator != nil {
+				body = b.statusMethodNotAllowedBodyGenerator()
+			}
+		}
+	}
+
+	return b.ResponseWriter.Write(body)
+}