Skip to content

Commit

Permalink
style: propagate errors
Browse files Browse the repository at this point in the history
  • Loading branch information
TypicalAM committed Sep 1, 2023
1 parent 43e27ba commit 2b2fbb9
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 89 deletions.
17 changes: 11 additions & 6 deletions internal/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package backend

import (
"errors"
"fmt"
"log"
"sort"

Expand All @@ -25,12 +26,12 @@ func New(urlPath, cacheDir string, resetCache bool) (*Backend, error) {
log.Println("Creating new backend")
store, err := cache.New(cacheDir)
if err != nil {
return nil, err
return nil, fmt.Errorf("backend.New: %w", err)
}

readStatus, err := cache.NewReadStatus(cacheDir)
if err != nil {
return nil, err
return nil, fmt.Errorf("backend.New: %w", err)
}

if !resetCache {
Expand All @@ -45,7 +46,7 @@ func New(urlPath, cacheDir string, resetCache bool) (*Backend, error) {

rss, err := rss.New(urlPath)
if err != nil {
return nil, err
return nil, fmt.Errorf("backend.New: %w", err)
}

if err = rss.Load(); err != nil {
Expand Down Expand Up @@ -131,14 +132,18 @@ func (b Backend) DownloadItem(feedName string, index int) tea.Cmd {
// Close closes the backend and saves its components.
func (b Backend) Close() error {
if err := b.Rss.Save(); err != nil {
return err
return fmt.Errorf("backend.Close: %w", err)
}

if err := b.Cache.Save(); err != nil {
return err
return fmt.Errorf("backend.Close: %w", err)
}

return b.ReadStatus.Save()
if err := b.ReadStatus.Save(); err != nil {
return fmt.Errorf("backend.Close: %w", err)
}

return nil
}

// articlesToSuccessMsg converts a list of items to a FetchArticleSuccessMsg.
Expand Down
39 changes: 18 additions & 21 deletions internal/backend/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
Expand Down Expand Up @@ -58,7 +59,7 @@ func New(dir string) (*Cache, error) {
if dir == "" {
defaultDir, err := getDefaultDir()
if err != nil {
return nil, err
return nil, fmt.Errorf("cache.New: %w", err)
}

dir = defaultDir
Expand All @@ -74,21 +75,17 @@ func New(dir string) (*Cache, error) {
// Load reads the cache from disk
func (c *Cache) Load() error {
log.Println("Loading cache from", c.filePath)
if _, err := os.Stat(c.filePath); err != nil {
data, err := os.ReadFile(c.filePath)
if err != nil {
if os.IsNotExist(err) {
return nil
}

return err
}

data, err := os.ReadFile(c.filePath)
if err != nil {
return err
return fmt.Errorf("cache.Load: %w", err)
}

if err = json.Unmarshal(data, &c); err != nil {
return err
return fmt.Errorf("cache.Load: %w", err)
}

log.Println("Loaded cache entries: ", len(c.Content))
Expand All @@ -106,17 +103,17 @@ func (c *Cache) Save() error {

cacheData, err := json.Marshal(c)
if err != nil {
return err
return fmt.Errorf("cache.Save: %w", err)
}

// Try to write the data to the file
if err = os.WriteFile(c.filePath, cacheData, 0600); err != nil {
if err = os.MkdirAll(filepath.Dir(c.filePath), 0755); err != nil {
return err
return fmt.Errorf("cache.Save: %w", err)
}

if err = os.WriteFile(c.filePath, cacheData, 0600); err != nil {
return err
return fmt.Errorf("cache.Save: %w", err)
}
}

Expand All @@ -137,12 +134,12 @@ func (c *Cache) GetArticles(url string, ignoreCache bool) (SortableArticles, err
}

if c.OfflineMode {
return nil, fmt.Errorf("offline mode")
return nil, errors.New("offline mode")
}

articles, err := fetchArticles(url)
if err != nil {
return nil, err
return nil, fmt.Errorf("cache.GetArticles: %w", err)
}

c.Content[url] = Entry{time.Now().Add(DefaultCacheDuration), articles}
Expand Down Expand Up @@ -180,7 +177,7 @@ func (c *Cache) AddToDownloaded(item gofeed.Item) {
// RemoveFromDownloaded removes an item from the downloaded list
func (c *Cache) RemoveFromDownloaded(index int) error {
if index < 0 || index >= len(c.Downloaded) {
return fmt.Errorf("index out of range")
return errors.New("index out of range")
}

c.Downloaded = append(c.Downloaded[:index], c.Downloaded[index+1:]...)
Expand All @@ -192,7 +189,7 @@ func fetchArticles(url string) (SortableArticles, error) {
log.Println("Fetching articles from", url)
feed, err := parseFeed(url)
if err != nil {
return nil, err
return nil, fmt.Errorf("cache.fetchArticles: %w", err)
}

items := make(SortableArticles, len(feed.Items))
Expand All @@ -208,7 +205,7 @@ func fetchArticles(url string) (SortableArticles, error) {
func parseFeed(url string) (*gofeed.Feed, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
return nil, fmt.Errorf("cache.parseFeed: %w", err)
}
req.Header.Set("User-Agent", "goread (by /u/TypicalAM)")

Expand All @@ -224,7 +221,7 @@ func parseFeed(url string) (*gofeed.Feed, error) {

resp, err := client.Do(req.WithContext(ctx))
if err != nil {
return nil, err
return nil, fmt.Errorf("cache.parseFeed: %w", err)
}

if resp.StatusCode < 200 || resp.StatusCode >= 300 {
Expand All @@ -236,11 +233,11 @@ func parseFeed(url string) (*gofeed.Feed, error) {

feed, err := gofeed.NewParser().Parse(resp.Body)
if err != nil {
return nil, err
return nil, fmt.Errorf("cache.parseFeed: %w", err)
}

if err := resp.Body.Close(); err != nil {
return nil, err
return nil, fmt.Errorf("cache.parseFeed: %w", err)
}

return feed, nil
Expand All @@ -250,7 +247,7 @@ func parseFeed(url string) (*gofeed.Feed, error) {
func getDefaultDir() (string, error) {
dir, err := os.UserCacheDir()
if err != nil {
return "", err
return "", fmt.Errorf("cache.getDefaultDir: %w", err)
}

return filepath.Join(dir, "goread"), nil
Expand Down
19 changes: 10 additions & 9 deletions internal/backend/cache/read_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cache
import (
"encoding/binary"
"errors"
"fmt"
"log"
"os"
"path/filepath"
Expand All @@ -24,7 +25,7 @@ func NewReadStatus(dir string) (*ReadStatus, error) {
if dir == "" {
defaultDir, err := getDefaultDir()
if err != nil {
return nil, err
return nil, fmt.Errorf("cache.New: %w", err)
}

dir = defaultDir
Expand All @@ -39,21 +40,21 @@ func NewReadStatus(dir string) (*ReadStatus, error) {
// Load reads the cache from disk
func (rs *ReadStatus) Load() error {
log.Println("Loading read status from", rs.filePath)
if _, err := os.Stat(rs.filePath); err != nil {
data, err := os.ReadFile(rs.filePath)
if err != nil {
if os.IsNotExist(err) {
return nil
}

return err
return fmt.Errorf("cache.Load: %w", err)
}

data, err := os.ReadFile(rs.filePath)
rs.set, err = unmarshal(data)
if err != nil {
return err
return fmt.Errorf("cache.Load: %w", err)
}

rs.set, err = unmarshal(data)
return err
return nil
}

// Save writes the cache to disk
Expand All @@ -64,11 +65,11 @@ func (rs ReadStatus) Save() error {
// Try to write the data to the file
if err := os.WriteFile(rs.filePath, data, 0600); err != nil {
if err = os.MkdirAll(filepath.Dir(rs.filePath), 0755); err != nil {
return err
return fmt.Errorf("cache.Save: %w", err)
}

if err = os.WriteFile(rs.filePath, data, 0600); err != nil {
return err
return fmt.Errorf("cache.Save: %w", err)
}
}

Expand Down
Loading

0 comments on commit 2b2fbb9

Please sign in to comment.