1
0
mirror of https://github.com/mxpv/podsync.git synced 2024-05-11 05:55:04 +00:00

Implement catch all handler for feeds, reword error responses

This commit is contained in:
Maksym Pavlenko
2017-08-22 01:37:26 -07:00
parent 68d20820ae
commit 520352df7a
5 changed files with 23 additions and 14 deletions

View File

@ -2,6 +2,12 @@ package api
import (
"time"
"github.com/pkg/errors"
)
var (
ErrNotFound = errors.New("not found")
)
type Provider string

View File

@ -125,7 +125,7 @@ func (yt *YouTubeBuilder) queryFeed(feed *api.Feed) (*itunes.Podcast, string, er
if feed.LinkType == api.Channel || feed.LinkType == api.User {
channel, err := yt.listChannels(feed.LinkType, feed.ItemId)
if err != nil {
return nil, "", errors.Wrap(err, "failed to query channel")
return nil, "", err
}
itemId := channel.ContentDetails.RelatedPlaylists.Uploads

View File

@ -79,12 +79,7 @@ func (s *service) GetFeed(hashId string) (*itunes.Podcast, error) {
}
func (s *service) GetMetadata(hashId string) (*api.Feed, error) {
feed, err := s.storage.GetFeed(hashId)
if err != nil {
return nil, errors.Wrapf(err, "failed to query feed: %s", hashId)
}
return feed, nil
return s.storage.GetFeed(hashId)
}
type feedOption func(*service)

View File

@ -14,7 +14,6 @@ import (
itunes "github.com/mxpv/podcast"
"github.com/mxpv/podsync/pkg/api"
"github.com/mxpv/podsync/pkg/config"
"github.com/pkg/errors"
"golang.org/x/oauth2"
)
@ -198,16 +197,21 @@ func MakeHandlers(feed feed, cfg *config.AppConfig) http.Handler {
c.JSON(http.StatusOK, gin.H{"id": hashId})
})
r.GET("/api/feed/:hashId", func(c *gin.Context) {
hashId := c.Param("hashId")
r.NoRoute(func(c *gin.Context) {
hashId := c.Request.URL.Path[1:]
if hashId == "" || len(hashId) > 12 {
c.JSON(badRequest(errors.New("invalid feed id")))
c.String(http.StatusBadRequest, "invalid feed id")
return
}
podcast, err := feed.GetFeed(hashId)
if err != nil {
c.JSON(internalError(err))
code := http.StatusInternalServerError
if err == api.ErrNotFound {
code = http.StatusNotFound
}
c.String(code, err.Error())
return
}
@ -217,13 +221,13 @@ func MakeHandlers(feed feed, cfg *config.AppConfig) http.Handler {
r.GET("/api/metadata/:hashId", func(c *gin.Context) {
hashId := c.Param("hashId")
if hashId == "" || len(hashId) > 12 {
c.JSON(badRequest(errors.New("invalid feed id")))
c.String(http.StatusBadRequest, "invalid feed id")
return
}
feed, err := feed.GetMetadata(hashId)
if err != nil {
c.JSON(internalError(err))
c.String(http.StatusInternalServerError, err.Error())
return
}

View File

@ -61,6 +61,10 @@ func (r *RedisStorage) GetFeed(hashId string) (*api.Feed, error) {
return nil, errors.Wrapf(err, "failed to query feed with id %s", hashId)
}
if len(result) == 0 {
return nil, api.ErrNotFound
}
// Expire after 3 month if no use
if err := r.client.Expire(hashId, expiration).Err(); err != nil {
return nil, errors.Wrap(err, "failed query update feed")