small pixel drawing of a pufferfish vore

make fetch error handling more robust
Jes Olson j3s@c3f.net
Mon, 20 Mar 2023 20:27:26 -0700
commit

41d20ecc7b16c5e5de13d65bd2a9463d096e2f18

parent

8a4a8a6fcfc3b5e56e1c453f2c81770161746f0f

2 files changed, 30 insertions(+), 7 deletions(-)

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

@@ -86,7 +86,15 @@ // updateFeed triggers a fetch on the given feed,

// and sets a fetch error in the db if there is one. func (r *Reaper) updateFeed(f *rss.Feed) { err := f.Update() - r.db.SetFeedFetchError(f.UpdateURL, err) + var errStr string + if err != nil { + fmt.Printf("[err] reaper: feed update %s\n", err) + errStr = err.Error() + } + err = r.db.SetFeedFetchError(f.UpdateURL, errStr) + if err != nil { + fmt.Printf("[err] reaper: set fetch_error %s\n", err) + } } // Have checks whether a given url is represented
M sqlite/sql.gosqlite/sql.go

@@ -209,13 +209,28 @@ }

// WriteFeed writes an rss feed to the database for permanent storage // if the given feed already exists, WriteFeed does nothing. -func (s *DB) SetFeedFetchError(url string, fetchErr error) { - var errStr string - if fetchErr != nil { - errStr = fetchErr.Error() +func (s *DB) SetFeedFetchError(url string, fetchErr string) error { + storedErr, err := s.GetFeedFetchError(url) + if err != nil { + return err } - _, err := s.sql.Exec(`UPDATE feed SET fetch_error=? WHERE url=?`, errStr, url) + if fetchErr == storedErr { + return nil + } + _, err = s.sql.Exec("UPDATE feed SET fetch_error=? WHERE url=?", fetchErr, url) + return err +} + +// WriteFeed writes an rss feed to the database for permanent storage +// if the given feed already exists, WriteFeed does nothing. +func (s *DB) GetFeedFetchError(url string) (string, error) { + var result sql.NullString + err := s.sql.QueryRow("SELECT fetch_error FROM feed WHERE url=?", url).Scan(&result) if err != nil { - panic(err) + return "", err } + if result.Valid { + return result.String, nil + } + return "", nil }