From d9ed72a5afbb85baf7264c6275c0fe34dfe513c3 Mon Sep 17 00:00:00 2001 From: skidoodle Date: Tue, 21 Apr 2026 09:31:12 +0200 Subject: [PATCH] throw error on empty save --- handler/http.go | 24 +++++++++++++++++++++--- handler/http_test.go | 3 +++ view/static/style.css | 12 ++++++++++++ view/templates/bin.html | 7 ++++++- 4 files changed, 42 insertions(+), 4 deletions(-) diff --git a/handler/http.go b/handler/http.go index fb6b628..3e2b47c 100644 --- a/handler/http.go +++ b/handler/http.go @@ -24,6 +24,7 @@ type ViewData struct { TimeAgo string LineCount int GutterSize int + Error string } func NewHandler(store store.Store, maxSize int64, tmplPattern string) *HttpHandler { @@ -43,17 +44,34 @@ func NewHandler(store store.Store, maxSize int64, tmplPattern string) *HttpHandl func (h *HttpHandler) HandleSet(w http.ResponseWriter, r *http.Request) { r.Body = http.MaxBytesReader(w, r.Body, h.maxSize) if err := r.ParseForm(); err != nil { + data := ViewData{ + Title: "pastebin", + IsPreview: false, + Content: r.FormValue("content"), + } if strings.Contains(err.Error(), "request body too large") { - badRequest("content too large", err, w, r) + data.Error = "Content too large" } else { - badRequest("invalid form data", err, w, r) + data.Error = "Invalid form data" + } + w.WriteHeader(http.StatusBadRequest) + if err := h.templates.ExecuteTemplate(w, "base", data); err != nil { + internal("could not render template", err, w, r) } return } content := r.FormValue("content") if content == "" { - badRequest("bin cannot be empty", nil, w, r) + data := ViewData{ + Title: "pastebin", + IsPreview: false, + Error: "Bin cannot be empty", + } + w.WriteHeader(http.StatusBadRequest) + if err := h.templates.ExecuteTemplate(w, "base", data); err != nil { + internal("could not render template", err, w, r) + } return } diff --git a/handler/http_test.go b/handler/http_test.go index 6aa2b71..9d85def 100644 --- a/handler/http_test.go +++ b/handler/http_test.go @@ -91,6 +91,7 @@ func TestHandleSet(t *testing.T) { h.HandleSet(rr, req) assert.Equal(t, http.StatusBadRequest, rr.Code) + assert.Contains(t, rr.Body.String(), "Bin cannot be empty") }) t.Run("Too Large", func(t *testing.T) { @@ -102,6 +103,7 @@ func TestHandleSet(t *testing.T) { h.HandleSet(rr, req) assert.Equal(t, http.StatusBadRequest, rr.Code) + assert.Contains(t, rr.Body.String(), "Content too large") }) t.Run("Malformed Form", func(t *testing.T) { @@ -111,6 +113,7 @@ func TestHandleSet(t *testing.T) { h.HandleSet(rr, req) assert.Equal(t, http.StatusBadRequest, rr.Code) + assert.Contains(t, rr.Body.String(), "Invalid form data") }) t.Run("Store Error", func(t *testing.T) { diff --git a/view/static/style.css b/view/static/style.css index 6a302b2..fa271d6 100644 --- a/view/static/style.css +++ b/view/static/style.css @@ -224,6 +224,18 @@ code.with-line-numbers { background: var(--secondary); } +.error-banner { + background-color: rgba(248, 81, 73, 0.1); + border: 1px solid rgba(248, 81, 73, 0.4); + color: #f85149; + padding: 0.75rem 1rem; + border-radius: 6px; + margin-bottom: 1rem; + font-size: 0.85rem; + display: flex; + align-items: center; +} + .sr-only { position: absolute; width: 1px; diff --git a/view/templates/bin.html b/view/templates/bin.html index 5a2f051..0a33de5 100644 --- a/view/templates/bin.html +++ b/view/templates/bin.html @@ -60,10 +60,15 @@ {{ else }}
+ {{ if .Error }} +
+ {{ .Error }} +
+ {{ end }}
+ autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false">{{ .Content }}