Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions golink.go
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,10 @@ func (s *Server) serveDetail(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r)
return
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if short != link.Short {
// redirect to canonical short name
http.Redirect(w, r, "/.detail/"+link.Short, http.StatusFound)
Expand Down Expand Up @@ -1194,6 +1198,10 @@ func (s *Server) serveDelete(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r)
return
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

if !s.canEditLink(r.Context(), link, cu) {
http.Error(w, fmt.Sprintf("cannot delete link owned by %q", link.Owner), http.StatusForbidden)
Expand Down
24 changes: 24 additions & 0 deletions golink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1166,3 +1166,27 @@ func TestServeHTTPReturnsServeError(t *testing.T) {
t.Fatal("serveHTTP did not return after the listener closed")
}
}

func TestServeDetailDeleteDBError(t *testing.T) {
s := newTestServer(t)
s.db.Save(&Link{Short: "foo", Owner: "foo@example.com"})
s.db.db.Close() // make every query fail with an error other than fs.ErrNotExist

for _, tt := range []struct {
name string
handler http.HandlerFunc
path string
}{
{"detail", s.serveDetail, "/.detail/foo"},
{"delete", s.serveDelete, "/.delete/foo"},
} {
t.Run(tt.name, func(t *testing.T) {
r := httptest.NewRequest("POST", tt.path, nil)
w := httptest.NewRecorder()
tt.handler(w, r)
if w.Code != http.StatusInternalServerError {
t.Errorf("status = %d, want %d", w.Code, http.StatusInternalServerError)
}
})
}
}