Skip to content

Commit

Permalink
feat: replace pkg/errors with cockroachdb/errors for improved wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
psmarcin committed Dec 29, 2020
1 parent 9084d86 commit 4dc5947
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 33 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ require (
cloud.google.com/go/firestore v1.4.0
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace v0.13.1-0.20201210214614-d8551be9a708
github.com/caarlos0/env v3.5.0+incompatible
github.com/cockroachdb/errors v1.8.2
github.com/eduncan911/podcast v1.4.2
github.com/gofiber/fiber/v2 v2.3.0
github.com/gofiber/helmet/v2 v2.1.0
github.com/gofiber/template v1.6.6
github.com/joho/godotenv v1.3.0
github.com/kkdai/youtube v1.2.4
github.com/pkg/errors v0.9.1
github.com/psmarcin/fiber-opentelemetry v0.3.0
github.com/sirupsen/logrus v1.7.0
github.com/stretchr/testify v1.6.1
Expand Down
117 changes: 117 additions & 0 deletions go.sum

Large diffs are not rendered by default.

13 changes: 6 additions & 7 deletions internal/adapters/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package adapters

import (
"context"
"errors"
"github.com/cockroachdb/errors"
"time"

"cloud.google.com/go/firestore"
Expand Down Expand Up @@ -35,9 +35,8 @@ func NewCacheRepository() (Cache, error) {
store, err := firestore.NewClient(ctx, firestore.DetectProjectID)
if err != nil {
logrus.WithError(err).Errorf("can't connect to Firebase")
return cache, err
return cache, errors.Wrap(err, "can't connect to Firebase")
}
//defer store.Close() // Close client when done.

cache.firestore = store
cache.collection = store.Collection(config.Cfg.FirestoreCollection)
Expand All @@ -63,7 +62,7 @@ func (c *Cache) SetKey(ctx context.Context, key, value string, exp time.Duration
"exp": exp.String(),
}).Errorf("set failed for %s", key)
span.RecordError(err)
return err
return errors.Wrapf(err, "can't set document: %s with value: %s, ctx: %s", key, value, ctx)
}

return nil
Expand All @@ -79,22 +78,22 @@ func (c *Cache) GetKey(ctx context.Context, key string) (string, error) {
if err != nil {
l.WithError(err).Warnf("can't get document %s", key)
span.RecordError(err)
return "", err
return "", errors.Wrapf(err, "can't get document from database key: %s with context %s", key, ctx)
}

var e CacheEntity
err = raw.DataTo(&e)
if err != nil {
l.WithError(err).Errorf("can't parse document %s", key)
span.RecordError(err)
return "", err
return "", errors.Wrap(err, "can't parse raw data")
}

timeDiff := time.Now().Sub(raw.UpdateTime)
if timeDiff > e.Ttl {
l.Debugf("key expires, updated at %s, expires in %s", raw.UpdateTime.Format(time.RFC3339), e.Ttl.String())
span.RecordError(err)
return "", errors.New("Cache expires for " + key)
return "", errors.Newf("Cache expires for %s", key)
}

l.Debugf("got key %s", key)
Expand Down
2 changes: 1 addition & 1 deletion internal/adapters/youtube-videos.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strconv"
"time"

"github.com/pkg/errors"
"github.com/cockroachdb/errors"
"github.com/psmarcin/youtubegoespodcast/internal/app"
"go.opentelemetry.io/otel/label"
)
Expand Down
18 changes: 9 additions & 9 deletions internal/adapters/youtube.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"net/url"
"time"

"github.com/pkg/errors"
"github.com/cockroachdb/errors"
"github.com/psmarcin/youtubegoespodcast/internal/app"
feedDomain "github.com/psmarcin/youtubegoespodcast/internal/domain/feed"
"go.opentelemetry.io/otel/label"
Expand All @@ -25,8 +25,8 @@ func NewYouTube() (*youtube.Service, error) {
ctx := context.Background()
youtubeService, err := youtube.NewService(ctx)
if err != nil {
l.WithError(err).Errorf("Can't create youtube service")
return youtubeService, err
l.WithError(err).Errorf("can't create youtube service")
return youtubeService, errors.Wrap(err, "can't create youtube service")
}
return youtubeService, nil
}
Expand All @@ -50,17 +50,17 @@ func (yt YouTubeAPIRepository) GetChannel(ctx context.Context, id string) (app.Y
if err != nil {
l.WithError(err).Errorf("youtube api request failed")
span.RecordError(err)
return channel, err
return channel, errors.Wrap(err,"can't make youtube api request to get channel")
}

for _, item := range response.Items {
channel, err = mapChannelToYouTubeChannel(item)
if err != nil {
span.RecordError(err)
return channel, err
return channel, errors.Wrap(err, "can't parse youtube api response item to channel")
}
}
return channel, err
return channel, nil
}

func (yt YouTubeAPIRepository) ListChannel(ctx context.Context, query string) ([]app.YouTubeChannel, error) {
Expand All @@ -80,19 +80,19 @@ func (yt YouTubeAPIRepository) ListChannel(ctx context.Context, query string) ([
if err != nil {
l.WithError(err).Errorf("youtube api request failed")
span.RecordError(err)
return channels, err
return channels, errors.Wrap(err, "can't make youtube api request to list channel")
}

for _, item := range response.Items {
channel, err := mapSearchItemToYouTubeChannel(item)
if err != nil {
span.RecordError(err)
return channels, err
return channels, errors.Wrap(err, "can't parse youtube api response search item to channel")
}

channels = append(channels, channel)
}
return channels, err
return channels, nil
}

func mapChannelToYouTubeChannel(item *youtube.Channel) (app.YouTubeChannel, error) {
Expand Down
6 changes: 3 additions & 3 deletions internal/app/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/json"
"time"

"github.com/pkg/errors"
"github.com/cockroachdb/errors"
"github.com/sirupsen/logrus"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/label"
Expand Down Expand Up @@ -48,7 +48,7 @@ func (c *CacheService) Set(ctx context.Context, key string, value interface{}) e
if err != nil {
l.WithError(err).WithField("value", value).WithField("key", key).Errorf("can't marshal")
span.RecordError(err)
return err
return errors.Wrapf(err, "can't serialize value: %+v for key: %s", value, key)
}
err = c.cache.SetKey(ctx, key, string(marshaled), CacheTTL)
if err != nil {
Expand Down Expand Up @@ -93,7 +93,7 @@ func (c *CacheService) MarshalAndSet(ctx context.Context, key string, value inte
if err != nil {
l.WithError(err).WithField("value", value).WithField("key", key).Errorf("can't marshal")
span.RecordError(err)
return err
return errors.Wrapf(err, "can't serialize value: %+v for key: %s", value, key)
}
err = c.Set(tCtx, key, string(marshaled))
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/app/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"
"time"

"github.com/pkg/errors"
"github.com/cockroachdb/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/app/feed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"time"

"github.com/eduncan911/podcast"
"github.com/pkg/errors"
"github.com/cockroachdb/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
Expand Down
14 changes: 7 additions & 7 deletions internal/app/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package app

import (
"context"
"errors"
"github.com/cockroachdb/errors"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -36,25 +36,25 @@ func (f FileService) GetDetails(ctx context.Context, videoId string) (Details, e
defer span.End()

yt := youtube.NewYoutube(true, true)

err := yt.DecodeURL(YoutubeVideoBaseURL + videoId)
fullUrl := YoutubeVideoBaseURL + videoId
err := yt.DecodeURL(fullUrl)
if err != nil {
return details, err
return details, errors.Wrapf(err, "can't decode url: %s", fullUrl)
}

audioStream, err := getAudioStream(yt.GetStreamInfo().Streams)
if err != nil {
return details, err
return details, errors.Wrapf(err, "can't get audio stream url: %s", fullUrl)
}

audioStreamRawUrl, err := getStreamUrl(videoId, audioStream)
if err != nil {
return details, err
return details, errors.Wrapf(err, "can't get stream url for stream: %+v, url: %s", audioStream, fullUrl)
}

audioStreamUrl, err := url.Parse(audioStreamRawUrl)
if err != nil {
return details, err
return details, errors.Wrapf(err, "can't parse audio stream url: %s", audioStreamRawUrl)
}
details.Url = *audioStreamUrl

Expand Down
4 changes: 1 addition & 3 deletions internal/app/youtube.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import (
"net/url"
"time"

"github.com/pkg/errors"
"github.com/cockroachdb/errors"
"go.opentelemetry.io/otel/label"
)

const (
CacheGetChannelPrefix = "youtube-channel-"
CacheGetChannelTtl = time.Hour * 24 * 31
CacheListChannelPrefix = "youtube-channels-q-"
CacheListChannelTtl = time.Hour * 24
)

type youTubeRepository interface {
Expand Down

0 comments on commit 4dc5947

Please sign in to comment.