throw error on empty save

This commit is contained in:
2026-04-21 09:31:12 +02:00
parent d0923ef4ed
commit d9ed72a5af
4 changed files with 42 additions and 4 deletions
+21 -3
View File
@@ -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
}
+3
View File
@@ -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) {