Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use std lib errors #100

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 39 additions & 40 deletions archives/archives.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"github.com/lib/pq"
"github.com/nyaruka/gocommon/analytics"
"github.com/nyaruka/gocommon/dates"
"github.com/pkg/errors"
)

// ArchiveType is the type for the archives
Expand Down Expand Up @@ -104,7 +103,7 @@

rows, err := db.QueryxContext(ctx, sqlLookupActiveOrgs)
if err != nil {
return nil, errors.Wrapf(err, "error fetching active orgs")
return nil, fmt.Errorf("error fetching active orgs: %w", err)

Check warning on line 106 in archives/archives.go

View check run for this annotation

Codecov / codecov/patch

archives/archives.go#L106

Added line #L106 was not covered by tests
}
defer rows.Close()

Expand All @@ -113,7 +112,7 @@
org := Org{RetentionPeriod: conf.RetentionPeriod}
err = rows.StructScan(&org)
if err != nil {
return nil, errors.Wrapf(err, "error scanning active org")
return nil, fmt.Errorf("error scanning active org: %w", err)

Check warning on line 115 in archives/archives.go

View check run for this annotation

Codecov / codecov/patch

archives/archives.go#L115

Added line #L115 was not covered by tests
}
orgs = append(orgs, org)
}
Expand All @@ -135,7 +134,7 @@
archives := make([]*Archive, 0, 1)
err := db.SelectContext(ctx, &archives, sqlLookupOrgArchives, org.ID, archiveType)
if err != nil && err != sql.ErrNoRows {
return nil, errors.Wrapf(err, "error selecting current archives for org: %d and type: %s", org.ID, archiveType)
return nil, fmt.Errorf("error selecting current archives for org: %d and type: %s: %w", org.ID, archiveType, err)

Check warning on line 137 in archives/archives.go

View check run for this annotation

Codecov / codecov/patch

archives/archives.go#L137

Added line #L137 was not covered by tests
}

return archives, nil
Expand All @@ -155,7 +154,7 @@
archives := make([]*Archive, 0, 1)
err := db.SelectContext(ctx, &archives, sqlLookupArchivesNeedingDeletion, org.ID, archiveType)
if err != nil && err != sql.ErrNoRows {
return nil, errors.Wrapf(err, "error selecting archives needing deletion for org: %d and type: %s", org.ID, archiveType)
return nil, fmt.Errorf("error selecting archives needing deletion for org: %d and type: %s: %w", org.ID, archiveType, err)

Check warning on line 157 in archives/archives.go

View check run for this annotation

Codecov / codecov/patch

archives/archives.go#L157

Added line #L157 was not covered by tests
}

return archives, nil
Expand All @@ -175,7 +174,7 @@

err := db.GetContext(ctx, &archiveCount, sqlCountOrgArchives, org.ID, archiveType)
if err != nil {
return 0, errors.Wrapf(err, "error querying archive count for org: %d and type: %s", org.ID, archiveType)
return 0, fmt.Errorf("error querying archive count for org: %d and type: %s: %w", org.ID, archiveType, err)

Check warning on line 177 in archives/archives.go

View check run for this annotation

Codecov / codecov/patch

archives/archives.go#L177

Added line #L177 was not covered by tests
}

return archiveCount, nil
Expand All @@ -197,7 +196,7 @@

err := db.SelectContext(ctx, &existingArchives, sqlLookupOrgDailyArchivesForDateRange, org.ID, archiveType, DayPeriod, startDate, endDate)
if err != nil && err != sql.ErrNoRows {
return nil, errors.Wrapf(err, "error selecting daily archives for org: %d and type: %s", org.ID, archiveType)
return nil, fmt.Errorf("error selecting daily archives for org: %d and type: %s: %w", org.ID, archiveType, err)

Check warning on line 199 in archives/archives.go

View check run for this annotation

Codecov / codecov/patch

archives/archives.go#L199

Added line #L199 was not covered by tests
}

return existingArchives, nil
Expand Down Expand Up @@ -241,7 +240,7 @@

rows, err := db.QueryxContext(ctx, sqlLookupMissingDailyArchive, startDate, endDate, org.ID, DayPeriod, archiveType)
if err != nil {
return nil, errors.Wrapf(err, "error getting missing daily archives for org: %d and type: %s", org.ID, archiveType)
return nil, fmt.Errorf("error getting missing daily archives for org: %d and type: %s: %w", org.ID, archiveType, err)

Check warning on line 243 in archives/archives.go

View check run for this annotation

Codecov / codecov/patch

archives/archives.go#L243

Added line #L243 was not covered by tests
}
defer rows.Close()

Expand All @@ -250,7 +249,7 @@

err = rows.Scan(&missingDay)
if err != nil {
return nil, errors.Wrapf(err, "error scanning missing daily archive for org: %d and type: %s", org.ID, archiveType)
return nil, fmt.Errorf("error scanning missing daily archive for org: %d and type: %s: %w", org.ID, archiveType, err)

Check warning on line 252 in archives/archives.go

View check run for this annotation

Codecov / codecov/patch

archives/archives.go#L252

Added line #L252 was not covered by tests
}
archive := Archive{
Org: org,
Expand Down Expand Up @@ -295,7 +294,7 @@

rows, err := db.QueryxContext(ctx, sqlLookupMissingMonthlyArchive, startDate, endDate, org.ID, MonthPeriod, archiveType)
if err != nil {
return nil, errors.Wrapf(err, "error getting missing monthly archive for org: %d and type: %s", org.ID, archiveType)
return nil, fmt.Errorf("error getting missing monthly archive for org: %d and type: %s: %w", org.ID, archiveType, err)

Check warning on line 297 in archives/archives.go

View check run for this annotation

Codecov / codecov/patch

archives/archives.go#L297

Added line #L297 was not covered by tests
}
defer rows.Close()

Expand All @@ -304,7 +303,7 @@

err = rows.Scan(&missingMonth)
if err != nil {
return nil, errors.Wrapf(err, "error scanning missing monthly archive for org: %d and type: %s", org.ID, archiveType)
return nil, fmt.Errorf("error scanning missing monthly archive for org: %d and type: %s: %w", org.ID, archiveType, err)

Check warning on line 306 in archives/archives.go

View check run for this annotation

Codecov / codecov/patch

archives/archives.go#L306

Added line #L306 was not covered by tests
}
archive := Archive{
Org: org,
Expand Down Expand Up @@ -349,7 +348,7 @@
filename := fmt.Sprintf("%s_%d_%s_%d_%02d_", monthlyArchive.ArchiveType, monthlyArchive.Org.ID, monthlyArchive.Period, monthlyArchive.StartDate.Year(), monthlyArchive.StartDate.Month())
file, err := os.CreateTemp(conf.TempDir, filename)
if err != nil {
return errors.Wrapf(err, "error creating temp file: %s", filename)
return fmt.Errorf("error creating temp file: %s: %w", filename, err)

Check warning on line 351 in archives/archives.go

View check run for this annotation

Codecov / codecov/patch

archives/archives.go#L351

Added line #L351 was not covered by tests
}
writerHash := md5.New()
gzWriter := gzip.NewWriter(io.MultiWriter(file, writerHash))
Expand Down Expand Up @@ -378,21 +377,21 @@

reader, err := GetS3File(ctx, s3Client, daily.URL)
if err != nil {
return errors.Wrapf(err, "error reading S3 URL: %s", daily.URL)
return fmt.Errorf("error reading S3 URL: %s: %w", daily.URL, err)

Check warning on line 380 in archives/archives.go

View check run for this annotation

Codecov / codecov/patch

archives/archives.go#L380

Added line #L380 was not covered by tests
}

// set up our reader to calculate our hash along the way
readerHash := md5.New()
teeReader := io.TeeReader(reader, readerHash)
gzipReader, err := gzip.NewReader(teeReader)
if err != nil {
return errors.Wrapf(err, "error creating gzip reader")
return fmt.Errorf("error creating gzip reader: %w", err)

Check warning on line 388 in archives/archives.go

View check run for this annotation

Codecov / codecov/patch

archives/archives.go#L388

Added line #L388 was not covered by tests
}

// copy this daily file (uncompressed) to our new monthly file
_, err = io.Copy(writer, gzipReader)
if err != nil {
return errors.Wrapf(err, "error copying from s3 to disk for URL: %s", daily.URL)
return fmt.Errorf("error copying from s3 to disk for URL: %s: %w", daily.URL, err)

Check warning on line 394 in archives/archives.go

View check run for this annotation

Codecov / codecov/patch

archives/archives.go#L394

Added line #L394 was not covered by tests
}

reader.Close()
Expand Down Expand Up @@ -422,7 +421,7 @@
monthlyArchive.Hash = hex.EncodeToString(writerHash.Sum(nil))
stat, err := file.Stat()
if err != nil {
return errors.Wrapf(err, "error statting file: %s", file.Name())
return fmt.Errorf("error statting file: %s: %w", file.Name(), err)

Check warning on line 424 in archives/archives.go

View check run for this annotation

Codecov / codecov/patch

archives/archives.go#L424

Added line #L424 was not covered by tests
}
monthlyArchive.Size = stat.Size()
monthlyArchive.RecordCount = recordCount
Expand All @@ -444,7 +443,7 @@
if os.IsNotExist(err) {
return os.MkdirAll(path, 0700)
} else if err != nil {
return errors.Wrapf(err, "error statting temp dir: %s", path)
return fmt.Errorf("error statting temp dir: %s: %w", path, err)

Check warning on line 446 in archives/archives.go

View check run for this annotation

Codecov / codecov/patch

archives/archives.go#L446

Added line #L446 was not covered by tests
}

// is path a directory
Expand Down Expand Up @@ -482,7 +481,7 @@
filename := fmt.Sprintf("%s_%d_%s%d%02d%02d_", archive.ArchiveType, archive.Org.ID, archive.Period, archive.StartDate.Year(), archive.StartDate.Month(), archive.StartDate.Day())
file, err := os.CreateTemp(archivePath, filename)
if err != nil {
return errors.Wrapf(err, "error creating temp file: %s", filename)
return fmt.Errorf("error creating temp file: %s: %w", filename, err)

Check warning on line 484 in archives/archives.go

View check run for this annotation

Codecov / codecov/patch

archives/archives.go#L484

Added line #L484 was not covered by tests
}

defer func() {
Expand Down Expand Up @@ -513,24 +512,24 @@
}

if err != nil {
return errors.Wrapf(err, "error writing archive")
return fmt.Errorf("error writing archive: %w", err)
}

err = writer.Flush()
if err != nil {
return errors.Wrapf(err, "error flushing archive file")
return fmt.Errorf("error flushing archive file: %w", err)

Check warning on line 520 in archives/archives.go

View check run for this annotation

Codecov / codecov/patch

archives/archives.go#L520

Added line #L520 was not covered by tests
}

err = gzWriter.Close()
if err != nil {
return errors.Wrapf(err, "error closing archive gzip writer")
return fmt.Errorf("error closing archive gzip writer: %w", err)

Check warning on line 525 in archives/archives.go

View check run for this annotation

Codecov / codecov/patch

archives/archives.go#L525

Added line #L525 was not covered by tests
}

// calculate our size and hash
archive.Hash = hex.EncodeToString(hash.Sum(nil))
stat, err := file.Stat()
if err != nil {
return errors.Wrapf(err, "error calculating archive hash")
return fmt.Errorf("error calculating archive hash: %w", err)

Check warning on line 532 in archives/archives.go

View check run for this annotation

Codecov / codecov/patch

archives/archives.go#L532

Added line #L532 was not covered by tests
}

archive.ArchiveFile = file.Name()
Expand Down Expand Up @@ -571,7 +570,7 @@

err := UploadToS3(ctx, s3Client, bucket, archivePath, archive)
if err != nil {
return errors.Wrapf(err, "error uploading archive to S3")
return fmt.Errorf("error uploading archive to S3: %w", err)

Check warning on line 573 in archives/archives.go

View check run for this annotation

Codecov / codecov/patch

archives/archives.go#L573

Added line #L573 was not covered by tests
}

archive.NeedsDeletion = true
Expand Down Expand Up @@ -603,20 +602,20 @@

tx, err := db.BeginTxx(ctx, nil)
if err != nil {
return errors.Wrapf(err, "error starting transaction")
return fmt.Errorf("error starting transaction: %w", err)

Check warning on line 605 in archives/archives.go

View check run for this annotation

Codecov / codecov/patch

archives/archives.go#L605

Added line #L605 was not covered by tests
}

rows, err := tx.NamedQuery(sqlInsertArchive, archive)
if err != nil {
tx.Rollback()
return errors.Wrapf(err, "error inserting archive")
return fmt.Errorf("error inserting archive: %w", err)

Check warning on line 611 in archives/archives.go

View check run for this annotation

Codecov / codecov/patch

archives/archives.go#L611

Added line #L611 was not covered by tests
}

rows.Next()
err = rows.Scan(&archive.ID)
if err != nil {
tx.Rollback()
return errors.Wrapf(err, "error reading new archive id")
return fmt.Errorf("error reading new archive id: %w", err)

Check warning on line 618 in archives/archives.go

View check run for this annotation

Codecov / codecov/patch

archives/archives.go#L618

Added line #L618 was not covered by tests
}
rows.Close()

Expand All @@ -631,12 +630,12 @@
result, err := tx.ExecContext(ctx, `UPDATE archives_archive SET rollup_id = $1 WHERE id = ANY($2)`, archive.ID, pq.Array(childIDs))
if err != nil {
tx.Rollback()
return errors.Wrapf(err, "error updating rollup ids")
return fmt.Errorf("error updating rollup ids: %w", err)

Check warning on line 633 in archives/archives.go

View check run for this annotation

Codecov / codecov/patch

archives/archives.go#L633

Added line #L633 was not covered by tests
}
affected, err := result.RowsAffected()
if err != nil {
tx.Rollback()
return errors.Wrapf(err, "error getting number of rollup ids updated")
return fmt.Errorf("error getting number of rollup ids updated: %w", err)

Check warning on line 638 in archives/archives.go

View check run for this annotation

Codecov / codecov/patch

archives/archives.go#L638

Added line #L638 was not covered by tests
}
if int(affected) != len(childIDs) {
tx.Rollback()
Expand All @@ -647,7 +646,7 @@
err = tx.Commit()
if err != nil {
tx.Rollback()
return errors.Wrapf(err, "error committing new archive transaction")
return fmt.Errorf("error committing new archive transaction: %w", err)

Check warning on line 649 in archives/archives.go

View check run for this annotation

Codecov / codecov/patch

archives/archives.go#L649

Added line #L649 was not covered by tests
}
return nil
}
Expand All @@ -661,7 +660,7 @@
err := os.Remove(archive.ArchiveFile)

if err != nil {
return errors.Wrapf(err, "error deleting temp archive file: %s", archive.ArchiveFile)
return fmt.Errorf("error deleting temp archive file: %s: %w", archive.ArchiveFile, err)

Check warning on line 663 in archives/archives.go

View check run for this annotation

Codecov / codecov/patch

archives/archives.go#L663

Added line #L663 was not covered by tests
}

slog.Debug("deleted temporary archive file",
Expand All @@ -679,7 +678,7 @@
func CreateOrgArchives(ctx context.Context, now time.Time, config *Config, db *sqlx.DB, s3Client s3iface.S3API, org Org, archiveType ArchiveType) ([]*Archive, []*Archive, []*Archive, []*Archive, error) {
archiveCount, err := GetCurrentArchiveCount(ctx, db, org, archiveType)
if err != nil {
return nil, nil, nil, nil, errors.Wrapf(err, "error getting current archive count")
return nil, nil, nil, nil, fmt.Errorf("error getting current archive count: %w", err)

Check warning on line 681 in archives/archives.go

View check run for this annotation

Codecov / codecov/patch

archives/archives.go#L681

Added line #L681 was not covered by tests
}

var dailiesCreated, dailiesFailed, monthliesCreated, monthliesFailed []*Archive
Expand All @@ -688,7 +687,7 @@
if archiveCount == 0 {
archives, err := GetMissingMonthlyArchives(ctx, db, now, org, archiveType)
if err != nil {
return nil, nil, nil, nil, errors.Wrapf(err, "error getting missing monthly archives")
return nil, nil, nil, nil, fmt.Errorf("error getting missing monthly archives: %w", err)

Check warning on line 690 in archives/archives.go

View check run for this annotation

Codecov / codecov/patch

archives/archives.go#L690

Added line #L690 was not covered by tests
}

// we first create monthly archives
Expand All @@ -698,7 +697,7 @@
// then add in daily archives taking into account the monthly that have been built
daily, err := GetMissingDailyArchives(ctx, db, now, org, archiveType)
if err != nil {
return nil, nil, nil, nil, errors.Wrapf(err, "error getting missing daily archives")
return nil, nil, nil, nil, fmt.Errorf("error getting missing daily archives: %w", err)

Check warning on line 700 in archives/archives.go

View check run for this annotation

Codecov / codecov/patch

archives/archives.go#L700

Added line #L700 was not covered by tests
}

// we then create missing daily archives
Expand All @@ -712,7 +711,7 @@
func createArchive(ctx context.Context, db *sqlx.DB, config *Config, s3Client s3iface.S3API, archive *Archive) error {
err := CreateArchiveFile(ctx, db, archive, config.TempDir)
if err != nil {
return errors.Wrap(err, "error writing archive file")
return fmt.Errorf("error writing archive file: %w", err)
}

defer func() {
Expand All @@ -727,13 +726,13 @@
if config.UploadToS3 {
err = UploadArchive(ctx, s3Client, config.S3Bucket, archive)
if err != nil {
return errors.Wrap(err, "error writing archive to s3")
return fmt.Errorf("error writing archive to s3: %w", err)

Check warning on line 729 in archives/archives.go

View check run for this annotation

Codecov / codecov/patch

archives/archives.go#L729

Added line #L729 was not covered by tests
}
}

err = WriteArchiveToDB(ctx, db, archive)
if err != nil {
return errors.Wrap(err, "error writing record to db")
return fmt.Errorf("error writing record to db: %w", err)

Check warning on line 735 in archives/archives.go

View check run for this annotation

Codecov / codecov/patch

archives/archives.go#L735

Added line #L735 was not covered by tests
}

return nil
Expand Down Expand Up @@ -883,7 +882,7 @@

dailiesCreated, dailiesFailed, monthliesCreated, monthliesFailed, err := CreateOrgArchives(ctx, now, cfg, db, s3Client, org, archiveType)
if err != nil {
return nil, nil, nil, nil, nil, errors.Wrapf(err, "error creating archives")
return nil, nil, nil, nil, nil, fmt.Errorf("error creating archives: %w", err)

Check warning on line 885 in archives/archives.go

View check run for this annotation

Codecov / codecov/patch

archives/archives.go#L885

Added line #L885 was not covered by tests
}

if len(dailiesCreated) > 0 {
Expand All @@ -894,7 +893,7 @@

rollupsCreated, rollupsFailed, err := RollupOrgArchives(ctx, now, cfg, db, s3Client, org, archiveType)
if err != nil {
return nil, nil, nil, nil, nil, errors.Wrapf(err, "error rolling up archives")
return nil, nil, nil, nil, nil, fmt.Errorf("error rolling up archives: %w", err)

Check warning on line 896 in archives/archives.go

View check run for this annotation

Codecov / codecov/patch

archives/archives.go#L896

Added line #L896 was not covered by tests
}

monthliesCreated = append(monthliesCreated, rollupsCreated...)
Expand All @@ -906,7 +905,7 @@
if cfg.Delete {
deleted, err = DeleteArchivedOrgRecords(ctx, now, cfg, db, s3Client, org, archiveType)
if err != nil {
return dailiesCreated, dailiesFailed, monthliesCreated, monthliesFailed, nil, errors.Wrapf(err, "error deleting archived records")
return dailiesCreated, dailiesFailed, monthliesCreated, monthliesFailed, nil, fmt.Errorf("error deleting archived records: %w", err)

Check warning on line 908 in archives/archives.go

View check run for this annotation

Codecov / codecov/patch

archives/archives.go#L908

Added line #L908 was not covered by tests
}
}

Expand All @@ -923,7 +922,7 @@
cancel()

if err != nil {
return errors.Wrap(err, "error getting active orgs")
return fmt.Errorf("error getting active orgs: %w", err)

Check warning on line 925 in archives/archives.go

View check run for this annotation

Codecov / codecov/patch

archives/archives.go#L925

Added line #L925 was not covered by tests
}

totalRunsRecordsArchived, totalMsgsRecordsArchived := 0, 0
Expand Down
Loading
Loading