Skip to content

Commit

Permalink
Use std lib errors
Browse files Browse the repository at this point in the history
  • Loading branch information
norkans7 committed May 30, 2024
1 parent a62ff8b commit c2c4d9e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
6 changes: 3 additions & 3 deletions daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package indexer
import (
"context"
"database/sql"
"fmt"
"log/slog"
"sync"
"time"

"github.com/nyaruka/gocommon/analytics"
"github.com/nyaruka/rp-indexer/v9/indexers"
"github.com/pkg/errors"
)

type Daemon struct {
Expand Down Expand Up @@ -148,12 +148,12 @@ func (d *Daemon) reportStats(includeLag bool) {
func (d *Daemon) calculateLag(ctx context.Context, ix indexers.Indexer) (time.Duration, error) {
esLastModified, err := ix.GetESLastModified(ix.Name())
if err != nil {
return 0, errors.Wrap(err, "error getting ES last modified")
return 0, fmt.Errorf("error getting ES last modified: %w", err)

Check warning on line 151 in daemon.go

View check run for this annotation

Codecov / codecov/patch

daemon.go#L151

Added line #L151 was not covered by tests
}

dbLastModified, err := ix.GetDBLastModified(ctx, d.db)
if err != nil {
return 0, errors.Wrap(err, "error getting DB last modified")
return 0, fmt.Errorf("error getting DB last modified: %w", err)

Check warning on line 156 in daemon.go

View check run for this annotation

Codecov / codecov/patch

daemon.go#L156

Added line #L156 was not covered by tests
}

return dbLastModified.Sub(esLastModified), nil
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require (
github.com/lib/pq v1.10.9
github.com/nyaruka/ezconf v0.3.0
github.com/nyaruka/gocommon v1.55.0
github.com/pkg/errors v0.9.1
github.com/samber/slog-multi v1.0.2
github.com/samber/slog-sentry v1.2.2
github.com/stretchr/testify v1.9.0
Expand All @@ -25,6 +24,7 @@ require (
github.com/nyaruka/librato v1.1.1 // indirect
github.com/nyaruka/null/v2 v2.0.3 // indirect
github.com/nyaruka/phonenumbers v1.3.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/samber/lo v1.39.0 // indirect
github.com/shopspring/decimal v1.4.0 // indirect
Expand Down
12 changes: 5 additions & 7 deletions indexers/contacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
_ "embed"
"fmt"
"time"

"github.com/pkg/errors"
)

//go:embed contacts.index.json
Expand Down Expand Up @@ -51,15 +49,15 @@ func (i *ContactIndexer) Index(db *sql.DB, rebuild, cleanup bool) (string, error
if physicalIndex == "" || rebuild {
physicalIndex, err = i.createNewIndex(i.definition)
if err != nil {
return "", errors.Wrap(err, "error creating new index")
return "", fmt.Errorf("error creating new index: %w", err)

Check warning on line 52 in indexers/contacts.go

View check run for this annotation

Codecov / codecov/patch

indexers/contacts.go#L52

Added line #L52 was not covered by tests
}
i.log().Info("created new physical index", "index", physicalIndex)
remapAlias = true
}

lastModified, err := i.GetESLastModified(physicalIndex)
if err != nil {
return "", errors.Wrap(err, "error finding last modified")
return "", fmt.Errorf("error finding last modified: %w", err)

Check warning on line 60 in indexers/contacts.go

View check run for this annotation

Codecov / codecov/patch

indexers/contacts.go#L60

Added line #L60 was not covered by tests
}

i.log().Debug("indexing newer than last modified", "index", physicalIndex, "last_modified", lastModified)
Expand All @@ -68,7 +66,7 @@ func (i *ContactIndexer) Index(db *sql.DB, rebuild, cleanup bool) (string, error
start := time.Now()
indexed, deleted, err := i.indexModified(ctx, db, physicalIndex, lastModified.Add(-5*time.Second), rebuild)
if err != nil {
return "", errors.Wrap(err, "error indexing documents")
return "", fmt.Errorf("error indexing documents: %w", err)

Check warning on line 69 in indexers/contacts.go

View check run for this annotation

Codecov / codecov/patch

indexers/contacts.go#L69

Added line #L69 was not covered by tests
}

i.recordComplete(indexed, deleted, time.Since(start))
Expand All @@ -77,7 +75,7 @@ func (i *ContactIndexer) Index(db *sql.DB, rebuild, cleanup bool) (string, error
if remapAlias {
err := i.updateAlias(physicalIndex)
if err != nil {
return "", errors.Wrap(err, "error updating alias")
return "", fmt.Errorf("error updating alias: %w", err)

Check warning on line 78 in indexers/contacts.go

View check run for this annotation

Codecov / codecov/patch

indexers/contacts.go#L78

Added line #L78 was not covered by tests
}
remapAlias = false
}
Expand All @@ -86,7 +84,7 @@ func (i *ContactIndexer) Index(db *sql.DB, rebuild, cleanup bool) (string, error
if cleanup {
err := i.cleanupIndexes()
if err != nil {
return "", errors.Wrap(err, "error cleaning up old indexes")
return "", fmt.Errorf("error cleaning up old indexes: %w", err)

Check warning on line 87 in indexers/contacts.go

View check run for this annotation

Codecov / codecov/patch

indexers/contacts.go#L87

Added line #L87 was not covered by tests
}
}

Expand Down

0 comments on commit c2c4d9e

Please sign in to comment.