mirror of
https://github.com/skidoodle/pastebin
synced 2026-04-28 03:07:40 +02:00
throw error on empty save
This commit is contained in:
+21
-3
@@ -24,6 +24,7 @@ type ViewData struct {
|
|||||||
TimeAgo string
|
TimeAgo string
|
||||||
LineCount int
|
LineCount int
|
||||||
GutterSize int
|
GutterSize int
|
||||||
|
Error string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHandler(store store.Store, maxSize int64, tmplPattern string) *HttpHandler {
|
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) {
|
func (h *HttpHandler) HandleSet(w http.ResponseWriter, r *http.Request) {
|
||||||
r.Body = http.MaxBytesReader(w, r.Body, h.maxSize)
|
r.Body = http.MaxBytesReader(w, r.Body, h.maxSize)
|
||||||
if err := r.ParseForm(); err != nil {
|
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") {
|
if strings.Contains(err.Error(), "request body too large") {
|
||||||
badRequest("content too large", err, w, r)
|
data.Error = "Content too large"
|
||||||
} else {
|
} 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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
content := r.FormValue("content")
|
content := r.FormValue("content")
|
||||||
if 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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -91,6 +91,7 @@ func TestHandleSet(t *testing.T) {
|
|||||||
|
|
||||||
h.HandleSet(rr, req)
|
h.HandleSet(rr, req)
|
||||||
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
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) {
|
t.Run("Too Large", func(t *testing.T) {
|
||||||
@@ -102,6 +103,7 @@ func TestHandleSet(t *testing.T) {
|
|||||||
|
|
||||||
h.HandleSet(rr, req)
|
h.HandleSet(rr, req)
|
||||||
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
||||||
|
assert.Contains(t, rr.Body.String(), "Content too large")
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Malformed Form", func(t *testing.T) {
|
t.Run("Malformed Form", func(t *testing.T) {
|
||||||
@@ -111,6 +113,7 @@ func TestHandleSet(t *testing.T) {
|
|||||||
|
|
||||||
h.HandleSet(rr, req)
|
h.HandleSet(rr, req)
|
||||||
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
||||||
|
assert.Contains(t, rr.Body.String(), "Invalid form data")
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Store Error", func(t *testing.T) {
|
t.Run("Store Error", func(t *testing.T) {
|
||||||
|
|||||||
@@ -224,6 +224,18 @@ code.with-line-numbers {
|
|||||||
background: var(--secondary);
|
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 {
|
.sr-only {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
width: 1px;
|
width: 1px;
|
||||||
|
|||||||
@@ -60,10 +60,15 @@
|
|||||||
</script>
|
</script>
|
||||||
{{ else }}
|
{{ else }}
|
||||||
<div class="content-wrapper">
|
<div class="content-wrapper">
|
||||||
|
{{ if .Error }}
|
||||||
|
<div class="error-banner">
|
||||||
|
{{ .Error }}
|
||||||
|
</div>
|
||||||
|
{{ end }}
|
||||||
<form id="paste-form" action="/" method="post">
|
<form id="paste-form" action="/" method="post">
|
||||||
<label for="content-editor" class="sr-only">Paste Content</label>
|
<label for="content-editor" class="sr-only">Paste Content</label>
|
||||||
<textarea id="content-editor" name="content" placeholder="Paste something interesting here..." autofocus
|
<textarea id="content-editor" name="content" placeholder="Paste something interesting here..." autofocus
|
||||||
autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"></textarea>
|
autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false">{{ .Content }}</textarea>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
<script>
|
<script>
|
||||||
|
|||||||
Reference in New Issue
Block a user