Skip to content

Commit

Permalink
errors
Browse files Browse the repository at this point in the history
  • Loading branch information
gavincabbage committed Oct 1, 2019
1 parent 7869254 commit 92204d8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
34 changes: 18 additions & 16 deletions chiv.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ func (a *Archiver) ArchiveWithContext(ctx context.Context, table, bucket string,

rows, err := b.query(ctx, table)
if err != nil {
return err
return fmt.Errorf("chiv: querying '%s': %w", table, err)
}
defer func() {
if e := rows.Close(); e != nil && err == nil {
err = e
err = fmt.Errorf("chiv: closing rows: %w", e)
}
}()

Expand Down Expand Up @@ -136,18 +136,18 @@ func (a *Archiver) archive(ctx context.Context, rows Rows, table, bucket string)
func (a *Archiver) download(ctx context.Context, rows Rows, w io.WriteCloser) (err error) {
defer func() {
if e := w.Close(); e != nil && err == nil {
err = e
err = fmt.Errorf("chiv: downloading: closing writer: %w", e)
}
}()

columns, err := interfaced(rows.ColumnTypes())
if err != nil {
return err
return fmt.Errorf("chiv: downloading: getting column types: %w", err)
}

f, err := a.format(w, columns)
formatter, err := a.format(w, columns)
if err != nil {
return err
return fmt.Errorf("chiv: downloading: opening formatter: %w", err)
}

var (
Expand All @@ -166,7 +166,7 @@ func (a *Archiver) download(ctx context.Context, rows Rows, w io.WriteCloser) (e
default:
err = rows.Scan(scanned...)
if err != nil {
return err
return fmt.Errorf("chiv: downloading: scanning row: %w", err)
}

for i, raw := range rawBytes {
Expand All @@ -177,18 +177,18 @@ func (a *Archiver) download(ctx context.Context, rows Rows, w io.WriteCloser) (e
}
}

if err := f.Format(record); err != nil {
return err
if err := formatter.Format(record); err != nil {
return fmt.Errorf("chiv: downloading: formatting row: %w", err)
}
}
}

if err := rows.Err(); err != nil {
return err
return fmt.Errorf("chiv: downloading: scanning rows: %w", err)
}

if err := f.Close(); err != nil {
return err
if err := formatter.Close(); err != nil {
return fmt.Errorf("chiv: downloading: closing formatter: %w", err)
}

return nil
Expand All @@ -214,7 +214,7 @@ func (a *Archiver) query(ctx context.Context, table string) (*sql.Rows, error) {
func (a *Archiver) upload(ctx context.Context, r io.ReadCloser, table string, bucket string) (err error) {
defer func() {
if e := r.Close(); e != nil && err == nil {
err = e
err = fmt.Errorf("chiv: uploading: closing reader: %w", e)
}
}()

Expand All @@ -226,13 +226,15 @@ func (a *Archiver) upload(ctx context.Context, r io.ReadCloser, table string, bu
}
}

_, err = a.s3.UploadWithContext(ctx, &s3manager.UploadInput{
if _, err := a.s3.UploadWithContext(ctx, &s3manager.UploadInput{
Body: r,
Bucket: aws.String(bucket),
Key: aws.String(a.key),
})
}); err != nil {
return fmt.Errorf("chiv: uploading: %w", err)
}

return err
return nil
}

func interfaced(in []*sql.ColumnType, err error) ([]Column, error) {
Expand Down
14 changes: 7 additions & 7 deletions chiv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestArchiveRows(t *testing.T) {
scan: [][]string{{"first", "second"}},
columnTypesErr: errors.New("column types"),
},
expectedErr: "column types",
expectedErr: "chiv: downloading: getting column types: column types",
uploader: &uploader{},
formatter: &formatter{},
},
Expand All @@ -73,7 +73,7 @@ func TestArchiveRows(t *testing.T) {
scan: [][]string{{"first", "second"}},
},
formatErr: errors.New("formatter func"),
expectedErr: "formatter func",
expectedErr: "chiv: downloading: opening formatter: formatter func",
uploader: &uploader{},
formatter: &formatter{},
},
Expand All @@ -84,7 +84,7 @@ func TestArchiveRows(t *testing.T) {
scan: [][]string{{"first", "second"}},
scanErr: errors.New("scanning"),
},
expectedErr: "scanning",
expectedErr: "chiv: downloading: scanning row: scanning",
uploader: &uploader{},
formatter: &formatter{},
},
Expand All @@ -94,7 +94,7 @@ func TestArchiveRows(t *testing.T) {
columns: []string{"first_column", "second_column"},
scan: [][]string{{"first", "second"}},
},
expectedErr: "formatting",
expectedErr: "chiv: downloading: formatting row: formatting",
uploader: &uploader{},
formatter: &formatter{
formatErr: errors.New("formatting"),
Expand All @@ -107,7 +107,7 @@ func TestArchiveRows(t *testing.T) {
scan: [][]string{{"first", "second"}},
errErr: errors.New("database"),
},
expectedErr: "database",
expectedErr: "chiv: downloading: scanning rows: database",
uploader: &uploader{},
formatter: &formatter{},
},
Expand All @@ -117,7 +117,7 @@ func TestArchiveRows(t *testing.T) {
columns: []string{"first_column", "second_column"},
scan: [][]string{{"first", "second"}},
},
expectedErr: "closing formatter",
expectedErr: "chiv: downloading: closing formatter: closing formatter",
uploader: &uploader{},
formatter: &formatter{
closeErr: errors.New("closing formatter"),
Expand All @@ -129,7 +129,7 @@ func TestArchiveRows(t *testing.T) {
columns: []string{"first_column", "second_column"},
scan: [][]string{{"first", "second"}},
},
expectedErr: "uploading",
expectedErr: "chiv: uploading: uploading",
uploader: &uploader{
uploadErr: errors.New("uploading"),
},
Expand Down

0 comments on commit 92204d8

Please sign in to comment.