Skip to content

Commit

Permalink
Fix key not found error #120
Browse files Browse the repository at this point in the history
  • Loading branch information
mxpv committed Apr 16, 2020
1 parent 40dd2f5 commit 67478ac
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 38 deletions.
2 changes: 1 addition & 1 deletion cmd/podsync/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func main() {
if err := updater.Update(ctx, feed); err != nil {
log.WithError(err).Errorf("failed to update feed: %s", feed.URL)
} else {
log.Infof("Next update of %s: %s", feed.ID, c.Entry(m[feed.ID]).Next)
log.Infof("next update of %s: %s", feed.ID, c.Entry(m[feed.ID]).Next)
}
case <-ctx.Done():
return ctx.Err()
Expand Down
9 changes: 4 additions & 5 deletions cmd/podsync/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,19 @@ func (u *Updater) Update(ctx context.Context, feedConfig *config.Feed) error {
started := time.Now()

if err := u.updateFeed(ctx, feedConfig); err != nil {
return err
return errors.Wrap(err, "update failed")
}

if err := u.downloadEpisodes(ctx, feedConfig); err != nil {
return err
return errors.Wrap(err, "download failed")
}

if err := u.buildXML(ctx, feedConfig); err != nil {
return err
return errors.Wrap(err, "xml build failed")
}

if err := u.buildOPML(ctx); err != nil {
return err
return errors.Wrap(err, "opml build failed")
}

if err := u.cleanup(ctx, feedConfig); err != nil {
Expand Down Expand Up @@ -263,7 +263,6 @@ func (u *Updater) buildXML(ctx context.Context, feedConfig *config.Feed) error {
}

func (u *Updater) buildOPML(ctx context.Context) error {

// Build OPML with data received from builder
log.Debug("building podcast OPML")
opml, err := feed.BuildOPML(ctx, u.config, u.db, u.fs)
Expand Down
10 changes: 7 additions & 3 deletions pkg/db/badger.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func NewBadger(config *config.Database) (*Badger, error) {
storage := &Badger{db: db}

if err := db.Update(func(txn *badger.Txn) error {
if err := storage.setObj(txn, []byte(versionPath), CurrentVersion, false); err != nil && err != ErrAlreadyExists {
if err := storage.setObj(txn, []byte(versionPath), CurrentVersion, false); err != nil && err != model.ErrAlreadyExists {
return err
}
return nil
Expand Down Expand Up @@ -100,7 +100,7 @@ func (b *Badger) AddFeed(_ context.Context, feedID string, feed *model.Feed) err
for _, episode := range feed.Episodes {
episodeKey := b.getKey(episodePath, feedID, episode.ID)
err := b.setObj(txn, episodeKey, episode, false)
if err == nil || err == ErrAlreadyExists {
if err == nil || err == model.ErrAlreadyExists {
// Do nothing
} else {
return errors.Wrapf(err, "failed to save episode %q", feedID)
Expand Down Expand Up @@ -261,7 +261,7 @@ func (b *Badger) setObj(txn *badger.Txn, key []byte, obj interface{}, overwrite
// Overwrites are not allowed, make sure there is no object with the given key
_, err := txn.Get(key)
if err == nil {
return ErrAlreadyExists
return model.ErrAlreadyExists
} else if err == badger.ErrKeyNotFound {
// Key not found, do nothing
} else {
Expand All @@ -280,6 +280,10 @@ func (b *Badger) setObj(txn *badger.Txn, key []byte, obj interface{}, overwrite
func (b *Badger) getObj(txn *badger.Txn, key []byte, out interface{}) error {
item, err := txn.Get(key)
if err != nil {
if err == badger.ErrKeyNotFound {
return model.ErrNotFound
}

return err
}

Expand Down
5 changes: 0 additions & 5 deletions pkg/db/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package db

import (
"context"
"errors"

"github.com/mxpv/podsync/pkg/model"
)
Expand All @@ -13,10 +12,6 @@ const (
CurrentVersion = 1
)

var (
ErrAlreadyExists = errors.New("object already exists")
)

type Storage interface {
Close() error
Version() (int, error)
Expand Down
5 changes: 0 additions & 5 deletions pkg/feed/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ import (
"github.com/mxpv/podsync/pkg/model"
)

var (
ErrNotFound = errors.New("resource not found")
ErrQuotaExceeded = errors.New("query limit is exceeded")
)

type Builder interface {
Build(ctx context.Context, cfg *config.Feed) (*model.Feed, error)
}
Expand Down
36 changes: 22 additions & 14 deletions pkg/feed/opml.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (

"github.com/gilliek/go-opml/opml"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"

"github.com/mxpv/podsync/pkg/config"
"github.com/mxpv/podsync/pkg/model"
)

func BuildOPML(ctx context.Context, config *config.Config, db feedProvider, provider urlProvider) (string, error) {
Expand All @@ -17,25 +19,31 @@ func BuildOPML(ctx context.Context, config *config.Config, db feedProvider, prov

for _, feed := range config.Feeds {
f, err := db.GetFeed(ctx, feed.ID)
if err != nil {
return "", err
if err == model.ErrNotFound {
// As we update OPML on per-feed basis, some feeds may not yet be populated in database.
log.Debugf("can't find configuration for feed %q, ignoring opml", feed.ID)
continue
} else if err != nil {
return "", errors.Wrapf(err, "failed to query feed %q", feed.ID)
}

if feed.OPML {
downloadURL, err := provider.URL(ctx, "", fmt.Sprintf("%s.xml", feed.ID))
if err != nil {
return "", errors.Wrapf(err, "failed to get feed URL for %q", feed.ID)
}
if !feed.OPML {
continue
}

outline := opml.Outline{
Title: f.Title,
Text: f.Description,
Type: "rss",
XMLURL: downloadURL,
}
downloadURL, err := provider.URL(ctx, "", fmt.Sprintf("%s.xml", feed.ID))
if err != nil {
return "", errors.Wrapf(err, "failed to get feed URL for %q", feed.ID)
}

doc.Body.Outlines = append(doc.Body.Outlines, outline)
outline := opml.Outline{
Title: f.Title,
Text: f.Description,
Type: "rss",
XMLURL: downloadURL,
}

doc.Body.Outlines = append(doc.Body.Outlines, outline)
}

out, err := doc.XML()
Expand Down
6 changes: 3 additions & 3 deletions pkg/feed/vimeo.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (v *VimeoBuilder) queryChannel(feed *model.Feed) error {
ch, resp, err := v.client.Channels.Get(channelID)
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
return ErrNotFound
return model.ErrNotFound
}

return errors.Wrapf(err, "failed to query channel with id %q", channelID)
Expand All @@ -64,7 +64,7 @@ func (v *VimeoBuilder) queryGroup(feed *model.Feed) error {
gr, resp, err := v.client.Groups.Get(groupID)
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
return ErrNotFound
return model.ErrNotFound
}

return errors.Wrapf(err, "failed to query group with id %q", groupID)
Expand All @@ -87,7 +87,7 @@ func (v *VimeoBuilder) queryUser(feed *model.Feed) error {
user, resp, err := v.client.Users.Get(userID)
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
return ErrNotFound
return model.ErrNotFound
}

return errors.Wrapf(err, "failed to query user with id %q", userID)
Expand Down
4 changes: 2 additions & 2 deletions pkg/feed/youtube.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (yt *YouTubeBuilder) listChannels(ctx context.Context, linkType link.Type,
}

if len(resp.Items) == 0 {
return nil, ErrNotFound
return nil, model.ErrNotFound
}

item := resp.Items[0]
Expand All @@ -81,7 +81,7 @@ func (yt *YouTubeBuilder) listPlaylists(ctx context.Context, id, channelID strin
}

if len(resp.Items) == 0 {
return nil, ErrNotFound
return nil, model.ErrNotFound
}

item := resp.Items[0]
Expand Down
11 changes: 11 additions & 0 deletions pkg/model/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package model

import (
"errors"
)

var (
ErrAlreadyExists = errors.New("object already exists")
ErrNotFound = errors.New("not found")
ErrQuotaExceeded = errors.New("query limit is exceeded")
)

0 comments on commit 67478ac

Please sign in to comment.