Skip to content

Commit

Permalink
fix: Error wrapping
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Aug 16, 2022
1 parent 0c8b5d8 commit 440a5a8
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 47 deletions.
4 changes: 2 additions & 2 deletions pkg/ketchup/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ func (a App) generateToken(ctx context.Context) (securityPayload, error) {

token, err := uuid.New()
if err != nil {
return securityPayload{}, fmt.Errorf("generate uuid: %s", err)
return securityPayload{}, fmt.Errorf("generate uuid: %w", err)
}

id := questionID.Int64()

if err := a.redisApp.Store(ctx, tokenKey(token), fmt.Sprintf("%d", id), time.Minute*5); err != nil {
return securityPayload{}, fmt.Errorf("store token: %s", err)
return securityPayload{}, fmt.Errorf("store token: %w", err)
}

return securityPayload{
Expand Down
4 changes: 2 additions & 2 deletions pkg/model/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ func (r *RepositoryKind) UnmarshalJSON(b []byte) error {
var strValue string
err := json.Unmarshal(b, &strValue)
if err != nil {
return fmt.Errorf("unmarshal event type: %s", err)
return fmt.Errorf("unmarshal event type: %w", err)
}

value, err := ParseRepositoryKind(strValue)
if err != nil {
return fmt.Errorf("parse event type: %s", err)
return fmt.Errorf("parse event type: %w", err)
}

*r = value
Expand Down
2 changes: 1 addition & 1 deletion pkg/model/semver.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func PreparePatternMatching(patterns []string) (map[string]semver.Version, map[s
for _, pattern := range patterns {
p, err := semver.ParsePattern(pattern)
if err != nil {
return nil, nil, fmt.Errorf("parse pattern: %s", err)
return nil, nil, fmt.Errorf("parse pattern: %w", err)
}

versions[pattern] = semver.Version{}
Expand Down
14 changes: 7 additions & 7 deletions pkg/notifier/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (a App) Notify(ctx context.Context) error {
}

if err := a.sendNotification(ctx, "ketchup", ketchupsToNotify); err != nil {
return fmt.Errorf("send notification: %s", err)
return fmt.Errorf("send notification: %w", err)
}

if len(a.pushURL) != 0 {
Expand Down Expand Up @@ -116,7 +116,7 @@ func (a App) updateRepositories(ctx context.Context, releases []model.Release) e
for _, release := range releases {
if release.Repository.ID != repo.ID {
if err := a.repositoryService.Update(ctx, repo); err != nil {
return fmt.Errorf("update repository `%s`: %s", repo.Name, err)
return fmt.Errorf("update repository `%s`: %w", repo.Name, err)
}

repo = release.Repository
Expand All @@ -126,7 +126,7 @@ func (a App) updateRepositories(ctx context.Context, releases []model.Release) e
}

if err := a.repositoryService.Update(ctx, repo); err != nil {
return fmt.Errorf("update repository `%s`: %s", repo.Name, err)
return fmt.Errorf("update repository `%s`: %w", repo.Name, err)
}

return nil
Expand Down Expand Up @@ -270,7 +270,7 @@ func (a App) sendNotification(ctx context.Context, template string, ketchupToNot
mr = mr.WithSubject(subject)

if err := a.mailerApp.Send(ctx, mr); err != nil {
return fmt.Errorf("send email to %s: %s", ketchupUser.Email, err)
return fmt.Errorf("send email to %s: %w", ketchupUser.Email, err)
}
}

Expand All @@ -281,12 +281,12 @@ func (a App) sendNotification(ctx context.Context, template string, ketchupToNot
func (a App) Remind(ctx context.Context) error {
usersToRemind, err := a.userService.ListReminderUsers(ctx)
if err != nil {
return fmt.Errorf("get reminder users: %s", err)
return fmt.Errorf("get reminder users: %w", err)
}

remindKetchups, err := a.ketchupService.ListOutdated(ctx, usersToRemind...)
if err != nil {
return fmt.Errorf("get daily ketchups to remind: %s", err)
return fmt.Errorf("get daily ketchups to remind: %w", err)
}

if len(remindKetchups) == 0 {
Expand All @@ -297,7 +297,7 @@ func (a App) Remind(ctx context.Context) error {
a.appendKetchupsToUser(ctx, usersToNotify, remindKetchups)

if err := a.sendNotification(ctx, "ketchup_remind", usersToNotify); err != nil {
return fmt.Errorf("send remind notification: %s", err)
return fmt.Errorf("send remind notification: %w", err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/notifier/releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (a App) getNewStandardReleases(ctx context.Context) ([]model.Release, uint6
for {
repositories, _, err := a.repositoryService.ListByKinds(ctx, pageSize, last, model.Github, model.Docker, model.NPM, model.Pypi)
if err != nil {
return nil, count, fmt.Errorf("fetch standard repositories: %s", err)
return nil, count, fmt.Errorf("fetch standard repositories: %w", err)
}

repoCount := len(repositories)
Expand Down Expand Up @@ -126,7 +126,7 @@ func (a App) getNewHelmReleases(ctx context.Context) ([]model.Release, uint64, e
for {
repositories, _, err := a.repositoryService.ListByKinds(ctx, pageSize, last, model.Helm)
if err != nil {
return nil, count, fmt.Errorf("fetch Helm repositories: %s", err)
return nil, count, fmt.Errorf("fetch Helm repositories: %w", err)
}

repoCount := len(repositories)
Expand Down
14 changes: 7 additions & 7 deletions pkg/provider/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ func New(config Config) App {
func (a App) LatestVersions(ctx context.Context, repository string, patterns []string) (map[string]semver.Version, error) {
versions, compiledPatterns, err := model.PreparePatternMatching(patterns)
if err != nil {
return nil, fmt.Errorf("prepare pattern matching: %s", err)
return nil, fmt.Errorf("prepare pattern matching: %w", err)
}

registry, repository, auth, err := a.getImageDetails(ctx, repository)
if err != nil {
return nil, fmt.Errorf("compute image details: %s", err)
return nil, fmt.Errorf("compute image details: %w", err)
}

tagsURL := fmt.Sprintf("%s/v2/%s/tags/list", registry, repository)
Expand All @@ -78,7 +78,7 @@ func (a App) LatestVersions(ctx context.Context, repository string, patterns []s

resp, err := req.Send(ctx, nil)
if err != nil {
return nil, fmt.Errorf("fetch tags: %s", err)
return nil, fmt.Errorf("fetch tags: %w", err)
}

if err := browseRegistryTagsList(resp.Body, versions, compiledPatterns); err != nil {
Expand Down Expand Up @@ -109,7 +109,7 @@ func (a App) getImageDetails(ctx context.Context, repository string) (string, st

bearerToken, err := a.login(ctx, repository)
if err != nil {
return "", "", "", fmt.Errorf("authenticate to docker hub: %s", err)
return "", "", "", fmt.Errorf("authenticate to docker hub: %w", err)
}

return registryURL, repository, fmt.Sprintf("Bearer %s", bearerToken), nil
Expand All @@ -126,12 +126,12 @@ func (a App) login(ctx context.Context, repository string) (string, error) {

resp, err := request.Post(authURL).Form(ctx, values)
if err != nil {
return "", fmt.Errorf("authenticate to `%s`: %s", authURL, err)
return "", fmt.Errorf("authenticate to `%s`: %w", authURL, err)
}

var authContent authResponse
if err := httpjson.Read(resp, &authContent); err != nil {
return "", fmt.Errorf("read auth token: %s", err)
return "", fmt.Errorf("read auth token: %w", err)
}

return authContent.AccessToken, nil
Expand All @@ -155,7 +155,7 @@ func browseRegistryTagsList(body io.ReadCloser, versions map[string]semver.Versi
}()

if err := httpjson.Stream(body, versionsStream, "tags", true); err != nil {
return fmt.Errorf("read tags: %s", err)
return fmt.Errorf("read tags: %w", err)
}

<-done
Expand Down
10 changes: 5 additions & 5 deletions pkg/provider/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,20 @@ func (a app) Start(registerer prometheus.Registerer, done <-chan struct{}) {
func (a app) LatestVersions(ctx context.Context, repository string, patterns []string) (map[string]semver.Version, error) {
versions, compiledPatterns, err := model.PreparePatternMatching(patterns)
if err != nil {
return nil, fmt.Errorf("prepare pattern matching: %s", err)
return nil, fmt.Errorf("prepare pattern matching: %w", err)
}

page := 1
req := a.newClient()
for {
resp, err := req.Get(fmt.Sprintf("%s/repos/%s/tags?per_page=100&page=%d", apiURL, repository, page)).Send(ctx, nil)
if err != nil {
return nil, fmt.Errorf("list page %d of tags: %s", page, err)
return nil, fmt.Errorf("list page %d of tags: %w", page, err)
}

var tags []Tag
if err := httpjson.Read(resp, &tags); err != nil {
return nil, fmt.Errorf("read tags page #%d: %s", page, err)
return nil, fmt.Errorf("read tags page #%d: %w", page, err)
}

for _, tag := range tags {
Expand All @@ -149,12 +149,12 @@ func (a app) LatestVersions(ctx context.Context, repository string, patterns []s
func (a app) getRateLimit(ctx context.Context) (uint64, error) {
resp, err := a.newClient().Get(fmt.Sprintf("%s/rate_limit", apiURL)).Send(ctx, nil)
if err != nil {
return 0, fmt.Errorf("get rate limit: %s", err)
return 0, fmt.Errorf("get rate limit: %w", err)
}

var rateLimits RateLimitResponse
if err := httpjson.Read(resp, &rateLimits); err != nil {
return 0, fmt.Errorf("read rate limit: %s", err)
return 0, fmt.Errorf("read rate limit: %w", err)
}

return rateLimits.Resources["core"].Remaining, nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/provider/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (a App) FetchIndex(ctx context.Context, url string, chartsPatterns map[stri

var index charts
if err := yaml.NewDecoder(resp.Body).Decode(&index); err != nil && err != io.EOF {
return nil, fmt.Errorf("parse yaml index: %s", err)
return nil, fmt.Errorf("parse yaml index: %w", err)
}

output := make(map[string]map[string]semver.Version, len(index.Entries))
Expand All @@ -59,7 +59,7 @@ func (a App) FetchIndex(ctx context.Context, url string, chartsPatterns map[stri

versions, compiledPatterns, err := model.PreparePatternMatching(patterns)
if err != nil {
return nil, fmt.Errorf("prepare pattern matching for `%s`: %s", key, err)
return nil, fmt.Errorf("prepare pattern matching for `%s`: %w", key, err)
}

for _, chart := range charts {
Expand Down
6 changes: 3 additions & 3 deletions pkg/provider/npm/npm.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ func New() App {
func (a App) LatestVersions(ctx context.Context, name string, patterns []string) (map[string]semver.Version, error) {
versions, compiledPatterns, err := model.PreparePatternMatching(patterns)
if err != nil {
return nil, fmt.Errorf("prepare pattern matching: %s", err)
return nil, fmt.Errorf("prepare pattern matching: %w", err)
}

resp, err := request.Get(fmt.Sprintf("%s/%s", registryURL, name)).Header("Accept", "application/vnd.npm.install-v1+json").Send(ctx, nil)
if err != nil {
return nil, fmt.Errorf("fetch registry: %s", err)
return nil, fmt.Errorf("fetch registry: %w", err)
}

var content packageResp
if err := httpjson.Read(resp, &content); err != nil {
return nil, fmt.Errorf("read versions: %s", err)
return nil, fmt.Errorf("read versions: %w", err)
}

for _, version := range content.Versions {
Expand Down
6 changes: 3 additions & 3 deletions pkg/provider/pypi/pypi.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ func New() App {
func (a App) LatestVersions(ctx context.Context, name string, patterns []string) (map[string]semver.Version, error) {
versions, compiledPatterns, err := model.PreparePatternMatching(patterns)
if err != nil {
return nil, fmt.Errorf("prepare pattern matching: %s", err)
return nil, fmt.Errorf("prepare pattern matching: %w", err)
}

resp, err := request.Get(fmt.Sprintf("%s/%s/json", registryURL, name)).Send(ctx, nil)
if err != nil {
return nil, fmt.Errorf("fetch registry: %s", err)
return nil, fmt.Errorf("fetch registry: %w", err)
}

var content packageResp
if err := httpjson.Read(resp, &content); err != nil {
return nil, fmt.Errorf("read versions: %s", err)
return nil, fmt.Errorf("read versions: %w", err)
}

for version := range content.Versions {
Expand Down
2 changes: 1 addition & 1 deletion pkg/semver/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func ParsePattern(pattern string) (Pattern, error) {

version, err := Parse(pattern[1:])
if err != nil {
return Pattern{}, fmt.Errorf("parse version in pattern: %s", err)
return Pattern{}, fmt.Errorf("parse version in pattern: %w", err)
}

constraintVersionSuffix := ""
Expand Down
22 changes: 11 additions & 11 deletions pkg/service/ketchup/ketchup.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func New(ketchupStore model.KetchupStore, repositoryService model.RepositoryServ
func (a App) List(ctx context.Context, pageSize uint, last string) ([]model.Ketchup, uint64, error) {
list, total, err := a.ketchupStore.List(ctx, pageSize, last)
if err != nil {
return nil, 0, httpModel.WrapInternal(fmt.Errorf("list: %s", err))
return nil, 0, httpModel.WrapInternal(fmt.Errorf("list: %w", err))
}

enrichedList := enrichKetchupsWithSemver(list)
Expand All @@ -48,7 +48,7 @@ func (a App) ListForRepositories(ctx context.Context, repositories []model.Repos

list, err := a.ketchupStore.ListByRepositoriesIDAndFrequencies(ctx, ids, frequencies...)
if err != nil {
return nil, httpModel.WrapInternal(fmt.Errorf("list by ids: %s", err))
return nil, httpModel.WrapInternal(fmt.Errorf("list by ids: %w", err))
}

return enrichKetchupsWithSemver(list), nil
Expand All @@ -63,7 +63,7 @@ func (a App) ListOutdated(ctx context.Context, users ...model.User) ([]model.Ket

list, err := a.ketchupStore.ListOutdated(ctx, usersIds...)
if err != nil {
return nil, httpModel.WrapInternal(fmt.Errorf("list outdated by frequency: %s", err))
return nil, httpModel.WrapInternal(fmt.Errorf("list outdated by frequency: %w", err))
}

return list, nil
Expand All @@ -86,7 +86,7 @@ func (a App) Create(ctx context.Context, item model.Ketchup) (model.Ketchup, err
}

if _, err := a.ketchupStore.Create(ctx, item); err != nil {
return httpModel.WrapInternal(fmt.Errorf("create: %s", err))
return httpModel.WrapInternal(fmt.Errorf("create: %w", err))
}

output = item
Expand All @@ -102,7 +102,7 @@ func (a App) UpdateAll(ctx context.Context) error {
return a.ketchupStore.UpdateAll(ctx)
})
if err != nil {
return fmt.Errorf("update all ketchup: %s", err)
return fmt.Errorf("update all ketchup: %w", err)
}

return nil
Expand All @@ -115,7 +115,7 @@ func (a App) Update(ctx context.Context, oldPattern string, item model.Ketchup)
err := a.ketchupStore.DoAtomic(ctx, func(ctx context.Context) error {
old, err := a.ketchupStore.GetByRepository(ctx, item.Repository.ID, oldPattern, true)
if err != nil {
return httpModel.WrapInternal(fmt.Errorf("fetch: %s", err))
return httpModel.WrapInternal(fmt.Errorf("fetch: %w", err))
}

if old.Repository.IsZero() {
Expand All @@ -138,14 +138,14 @@ func (a App) Update(ctx context.Context, oldPattern string, item model.Ketchup)
if old.Pattern != item.Pattern {
repo, err := a.repositoryService.GetOrCreate(ctx, old.Repository.Kind, old.Repository.Name, old.Repository.Part, item.Pattern)
if err != nil {
return httpModel.WrapInternal(fmt.Errorf("get repository version: %s", err))
return httpModel.WrapInternal(fmt.Errorf("get repository version: %w", err))
}

current.Repository = repo
}

if err := a.ketchupStore.Update(ctx, current, old.Pattern); err != nil {
return httpModel.WrapInternal(fmt.Errorf("update: %s", err))
return httpModel.WrapInternal(fmt.Errorf("update: %w", err))
}

output = current
Expand Down Expand Up @@ -175,7 +175,7 @@ func (a App) Delete(ctx context.Context, item model.Ketchup) (err error) {
return a.ketchupStore.DoAtomic(ctx, func(ctx context.Context) error {
old, err := a.ketchupStore.GetByRepository(ctx, item.Repository.ID, item.Pattern, true)
if err != nil {
return httpModel.WrapInternal(fmt.Errorf("fetch current: %s", err))
return httpModel.WrapInternal(fmt.Errorf("fetch current: %w", err))
}

if old.Repository.IsZero() {
Expand All @@ -187,7 +187,7 @@ func (a App) Delete(ctx context.Context, item model.Ketchup) (err error) {
}

if err = a.ketchupStore.Delete(ctx, old); err != nil {
return httpModel.WrapInternal(fmt.Errorf("delete: %s", err))
return httpModel.WrapInternal(fmt.Errorf("delete: %w", err))
}

return nil
Expand All @@ -208,7 +208,7 @@ func (a App) check(ctx context.Context, old, new model.Ketchup) error {
if len(strings.TrimSpace(new.Pattern)) == 0 {
output = append(output, errors.New("pattern is required"))
} else if _, err := semver.ParsePattern(new.Pattern); err != nil {
output = append(output, fmt.Errorf("pattern is invalid: %s", err))
output = append(output, fmt.Errorf("pattern is invalid: %w", err))
}

if len(strings.TrimSpace(new.Version)) == 0 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/store/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (a App) List(ctx context.Context, pageSize uint, last string) ([]model.Repo
if len(last) != 0 {
lastID, err := strconv.ParseUint(last, 10, 64)
if err != nil {
return nil, 0, fmt.Errorf("invalid last key: %s", err)
return nil, 0, fmt.Errorf("invalid last key: %w", err)
}

queryArgs = append(queryArgs, lastID)
Expand Down

0 comments on commit 440a5a8

Please sign in to comment.