small pixel drawing of a pufferfish vore

better error handling
Jes Olson j3s@c3f.net
Sun, 19 Mar 2023 06:44:54 -0700
commit

320f419e71eda610c0ba65dd441ff608b2ef39bc

parent

b90bd4babbf66e0d26d028c45fc96ae1dd1e2dd3

3 files changed, 33 insertions(+), 12 deletions(-)

jump to
M reaper/reaper.goreaper/reaper.go

@@ -99,7 +99,15 @@ result = append(result, r.feeds[i])

} } } + + r.SortFeeds(result) return result +} + +func (r *Reaper) SortFeeds(f []rss.Feed) { + sort.Slice(f, func(i, j int) bool { + return f[i].Title < f[j].Title + }) } func (r *Reaper) SortFeedItems(f []rss.Feed) []rss.Item {
M site.gosite.go

@@ -2,6 +2,7 @@ package main

import ( "fmt" + "io" "net/http" "strings"

@@ -169,10 +170,9 @@ return

} feeds := s.reaper.GetUserFeeds(s.username(r)) - if len(feeds) > 0 { - fmt.Fprintf(w, `<pre>you are subscribed to %d feeds</pre>`, len(feeds)) - for _, feed := range feeds { - fmt.Fprintf(w, ` + fmt.Fprintf(w, `<pre>you are subscribed to %d feeds</pre>`, len(feeds)) + for _, feed := range feeds { + fmt.Fprintf(w, ` <details> <summary>%s</summary> <pre>

@@ -181,18 +181,17 @@ url: %s

posts: %d </pre> </details>`, feed.Title, feed.Title, feed.UpdateURL, len(feed.Items)) - } - fmt.Fprintf(w, `<pre>add/remove feed URLs to this box to change your subscriptions</pre> + } + fmt.Fprintf(w, `<pre>add/remove feed URLs to this box to change your subscriptions</pre> <form method="POST" action="/feeds/submit"> <textarea name="submit" rows="10" cols="72">`) - for _, feed := range feeds { - fmt.Fprintf(w, "%s\n", feed.UpdateURL) - } - fmt.Fprintf(w, `</textarea> + for _, feed := range feeds { + fmt.Fprintf(w, "%s\n", feed.UpdateURL) + } + fmt.Fprintf(w, `</textarea> <br> <input type="submit" value="update feeds"> </form>`) - } // TODO: textbox with feed.URL // TODO: validate button }

@@ -213,6 +212,7 @@ }

// TODO: validate user input moar feeds := strings.Split(inputData, "\r\n") + s.db.UnsubscribeAll(s.username(r)) for _, feed := range feeds { // TODO: show diff before submission (like tf plan) // TODO: check if feed exists in db already?

@@ -221,9 +221,15 @@ if feed == "" {

continue } err := s.reaper.Add(feed) + if err == io.EOF { + http.Error(w, "400 bad request: could not fetch "+feed, 400) + fmt.Println(err) + return + } if err != nil { + http.Error(w, "400 bad request: "+err.Error(), 400) fmt.Println(err) - continue + return } s.db.WriteFeed(feed) s.db.Subscribe(s.username(r), feed)
M sqlite/sql.gosqlite/sql.go

@@ -111,6 +111,13 @@ panic(err)

} } +func (s *DB) UnsubscribeAll(username string) { + _, err := s.sql.Exec("DELETE FROM subscribe WHERE user_id=?", s.GetUserID(username)) + if err != nil { + panic(err) + } +} + func (s *DB) UserExists(username string) bool { var result string err := s.sql.QueryRow("SELECT username FROM user WHERE username=?", username).Scan(&result)