Skip to content

Commit

Permalink
Wrap all errors with fmt.Errorf
Browse files Browse the repository at this point in the history
  • Loading branch information
stoewer committed Oct 12, 2023
1 parent 0c86ab1 commit dfe8aa8
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 55 deletions.
12 changes: 6 additions & 6 deletions cmd/parquet-cli/cmd_info.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package main

import (
"github.com/stoewer/parquet-cli/pkg/inspect"
"fmt"
"os"

"github.com/parquet-go/parquet-go"
"github.com/pkg/errors"

"github.com/stoewer/parquet-cli/pkg/inspect"
"github.com/stoewer/parquet-cli/pkg/output"
)

Expand All @@ -18,22 +18,22 @@ type info struct {
func (i *info) Run() error {
file, err := os.Open(i.File)
if err != nil {
return errors.Wrapf(err, "unable to open file '%s'", i.File)
return fmt.Errorf("unable to open file '%s': %w", i.File, err)
}

stat, err := file.Stat()
if err != nil {
return errors.Wrapf(err, "unable to get stat for '%s'", i.File)
return fmt.Errorf("unable to get stat for '%s': %w", i.File, err)
}

pfile, err := parquet.OpenFile(file, stat.Size())
if err != nil {
return errors.Wrapf(err, "unable to read file '%s'", i.File)
return fmt.Errorf("unable to read file '%s': %w", i.File, err)
}

fileInfo, err := inspect.NewFileInfo(file, pfile)
if err != nil {
return errors.Wrapf(err, "unable to read file info for '%s'", i.File)
return fmt.Errorf("unable to read file info for '%s': %w", i.File, err)
}

return output.PrintTable(os.Stdout, i.Output, fileInfo)
Expand Down
8 changes: 4 additions & 4 deletions cmd/parquet-cli/helper.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
package main

import (
"fmt"
"os"

"github.com/parquet-go/parquet-go"
"github.com/pkg/errors"
)

func openParquetFile(filename string) (*parquet.File, error) {
file, err := os.Open(filename)
if err != nil {
return nil, errors.Wrapf(err, "unable to open file '%s'", filename)
return nil, fmt.Errorf("unable to open file '%s': %w", filename, err)
}

info, err := file.Stat()
if err != nil {
return nil, errors.Wrapf(err, "unable to get stat for '%s'", filename)
return nil, fmt.Errorf("unable to get stat for '%s': %w", filename, err)
}

pfile, err := parquet.OpenFile(file, info.Size())
if err != nil {
return nil, errors.Wrapf(err, "unable to read file '%s'", filename)
return nil, fmt.Errorf("unable to read file '%s': %w", filename, err)
}

return pfile, nil
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.21
require (
github.com/alecthomas/kong v0.8.0
github.com/parquet-go/parquet-go v0.17.0
github.com/pkg/errors v0.9.1
github.com/segmentio/encoding v0.3.6
github.com/stretchr/testify v1.8.4
)
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
github.com/alecthomas/assert/v2 v2.1.0 h1:tbredtNcQnoSd3QBhQWI7QZ3XHOVkw1Moklp2ojoH/0=
github.com/alecthomas/assert/v2 v2.1.0/go.mod h1:b/+1DI2Q6NckYi+3mXyH3wFb8qG37K/DuK80n7WefXA=
github.com/alecthomas/kong v0.8.0 h1:ryDCzutfIqJPnNn0omnrgHLbAggDQM2VWHikE1xqK7s=
github.com/alecthomas/kong v0.8.0/go.mod h1:n1iCIO2xS46oE8ZfYCNDqdR0b0wZNrXAIAqro/2132U=
github.com/alecthomas/repr v0.1.0 h1:ENn2e1+J3k09gyj2shc0dHr/yjaWSHRlrJ4DPMevDqE=
github.com/alecthomas/repr v0.1.0/go.mod h1:2kn6fqh/zIyPLmm3ugklbEi5hg5wS435eygvNfaDQL8=
github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs=
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I=
github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
Expand All @@ -20,8 +23,6 @@ github.com/parquet-go/parquet-go v0.17.0 h1:CCtKYt02AkjmGchk5pw4uWzzN0XRWU7f8sKU
github.com/parquet-go/parquet-go v0.17.0/go.mod h1:6pu/Ca02WRyWyF6jbY1KceESGBZMsRMSijjLbajXaG8=
github.com/pierrec/lz4/v4 v4.1.18 h1:xaKrnTkyoqfh1YItXl56+6KJNVYWlEEPuAQW9xsplYQ=
github.com/pierrec/lz4/v4 v4.1.18/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
Expand All @@ -36,6 +37,7 @@ golang.org/x/sys v0.0.0-20211110154304-99a53858aa08/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
1 change: 1 addition & 0 deletions golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ linters:
enable:
- cyclop
- errcheck
- errorlint
- goconst
- gofmt
- goimports
Expand Down
16 changes: 8 additions & 8 deletions pkg/inspect/aggregate.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package inspect

import (
"errors"
"fmt"
"io"
"sort"

"github.com/parquet-go/parquet-go"
"github.com/pkg/errors"
"github.com/stoewer/parquet-cli/pkg/output"
)

Expand Down Expand Up @@ -49,7 +49,7 @@ func NewAggregateCalculator(file *parquet.File, options AggregateOptions) (*Aggr

idx := options.GroupByColumn
if idx >= len(all) {
return nil, errors.Errorf("group by column index expectd to be lower than %d but was %d", idx, len(all))
return nil, fmt.Errorf("group by column index expected to be lower than %d but was %d", idx, len(all))
}
groupByColumn := all[idx]

Expand All @@ -64,11 +64,11 @@ func NewAggregateCalculator(file *parquet.File, options AggregateOptions) (*Aggr
columns = make([]*parquet.Column, 0, len(options.Columns))
for _, idx := range options.Columns {
if idx >= len(all) {
return nil, errors.Errorf("column index expectd be lower than %d but was %d", idx, len(all))
return nil, fmt.Errorf("column index expectd be lower than %d but was %d", idx, len(all))
}
col := all[idx]
if col.MaxDefinitionLevel() < groupByColumn.MaxDefinitionLevel() {
return nil, errors.Errorf(
return nil, fmt.Errorf(
"column max definition level expected to be greater or equal than %d but was %d",
groupByColumn.MaxDefinitionLevel(), col.MaxRepetitionLevel())
}
Expand All @@ -85,7 +85,7 @@ func NewAggregateCalculator(file *parquet.File, options AggregateOptions) (*Aggr
c := AggregateCalculator{header: header}
err := c.calculateResults(groupByColumn, columns)
if err != nil {
return nil, errors.Wrap(err, "unable to calculate results")
return nil, fmt.Errorf("unable to calculate results: %w", err)
}

return &c, nil
Expand All @@ -103,7 +103,7 @@ func (c *AggregateCalculator) Header() []any {

func (c *AggregateCalculator) NextRow() (output.TableRow, error) {
if c.resultIdx >= len(c.result) {
return nil, errors.Wrap(io.EOF, "no more aggregate results")
return nil, fmt.Errorf("no more aggregate results: %w", io.EOF)
}

r := c.result[c.resultIdx]
Expand All @@ -116,14 +116,14 @@ func (c *AggregateCalculator) calculateResults(groupByColumn *parquet.Column, co
// setup column iterators
groupByIter, err := newGroupingColumnIterator(groupByColumn, groupByColumn, Pagination{})
if err != nil {
return errors.Wrapf(err, "unable to create aggregate calculator")
return fmt.Errorf("unable to create aggregate calculator: %w", err)
}

var columnIter []*groupingColumnIterator
for _, col := range columns {
it, err := newGroupingColumnIterator(col, groupByColumn, Pagination{})
if err != nil {
return errors.Wrapf(err, "unable to create aggregate calculator")
return fmt.Errorf("unable to create aggregate calculator: %w", err)
}
columnIter = append(columnIter, it)
}
Expand Down
23 changes: 5 additions & 18 deletions pkg/inspect/col_stats.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package inspect

import (
"errors"
"fmt"
"io"

"github.com/parquet-go/parquet-go"
"github.com/pkg/errors"
"github.com/stoewer/parquet-cli/pkg/output"
)

Expand Down Expand Up @@ -88,7 +89,7 @@ func NewColStatCalculator(file *parquet.File, selectedCols []int) (*ColStatCalcu
columns = make([]*parquet.Column, 0, len(selectedCols))
for _, idx := range selectedCols {
if idx >= len(all) {
return nil, errors.Errorf("column index expectd be below %d but was %d", idx, len(all))
return nil, fmt.Errorf("column index expectd be below %d but was %d", idx, len(all))
}
columns = append(columns, all[idx])
}
Expand All @@ -109,7 +110,7 @@ func (cc *ColStatCalculator) Header() []any {

func (cc *ColStatCalculator) NextRow() (output.TableRow, error) {
if cc.current >= len(cc.columns) {
return nil, errors.Wrapf(io.EOF, "stop iteration: no more culumns")
return nil, fmt.Errorf("stop iteration: no more culumns: %w", io.EOF)
}

col := cc.columns[cc.current]
Expand Down Expand Up @@ -148,23 +149,9 @@ func (cc *ColStatCalculator) NextRow() (output.TableRow, error) {
page, err = pages.ReadPage()
}
if !errors.Is(err, io.EOF) {
return nil, errors.Wrapf(err, "unable to read page rom column '%s", col.Name())
return nil, fmt.Errorf("unable to read page rom column '%s': %w", col.Name(), err)
}
}

return &stats, nil
}

func min(a, b int64) int64 {
if a < b {
return a
}
return b
}

func max(a, b int64) int64 {
if a > b {
return a
}
return b
}
15 changes: 8 additions & 7 deletions pkg/inspect/grouping_column_iter.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package inspect

import (
"errors"
"fmt"
"io"

"github.com/parquet-go/parquet-go"
"github.com/pkg/errors"
)

// newGroupingColumnIterator creates a new groupingColumnIterator.
Expand All @@ -24,7 +25,7 @@ func newGroupingColumnIterator(column, groupByColumn *parquet.Column, pagination
}
err := it.forwardToOffset()
if err != nil {
return nil, errors.Wrap(err, "unable to create grouping column iterator")
return nil, fmt.Errorf("unable to create grouping column iterator: %w", err)
}

return &it, err
Expand Down Expand Up @@ -54,7 +55,7 @@ func (r *groupingColumnIterator) NextGroup() ([]parquet.Value, error) {
for {
for i, v := range r.unread {
if r.groupLimit != nil && r.currentGroup >= *r.groupLimit+r.groupOffset {
return nil, errors.Wrapf(io.EOF, "stop iteration: group limit reached")
return nil, fmt.Errorf("stop iteration: group limit reached: %w", io.EOF)
}
if r.isNewGroup(&v) && len(result) > 0 {
r.unread = r.unread[i:]
Expand All @@ -66,7 +67,7 @@ func (r *groupingColumnIterator) NextGroup() ([]parquet.Value, error) {

count, err := r.values.ReadValues(r.readBuffer)
if err != nil && !errors.Is(err, io.EOF) {
return nil, errors.Wrapf(err, "unable to read values from column '%s'", r.column.Name())
return nil, fmt.Errorf("unable to read values from column '%s': %w", r.column.Name(), err)
}

r.unread = r.readBuffer[:count]
Expand All @@ -78,7 +79,7 @@ func (r *groupingColumnIterator) NextGroup() ([]parquet.Value, error) {
p, err := r.pages.ReadPage()
if err != nil {
if !errors.Is(err, io.EOF) {
return nil, errors.Wrapf(err, "unable to read new page from column '%s'", r.column.Name())
return nil, fmt.Errorf("unable to read new page from column '%s': %w", r.column.Name(), err)
}
if len(result) > 0 {
return result, nil
Expand All @@ -95,7 +96,7 @@ func (r *groupingColumnIterator) forwardToOffset() error {
page, err := r.pages.ReadPage()
if err != nil {
if !errors.Is(err, io.EOF) {
return errors.Wrapf(err, "unable to read new page from column '%s'", r.column.Name())
return fmt.Errorf("unable to read new page from column '%s': %w", r.column.Name(), err)
}
return err
}
Expand All @@ -121,7 +122,7 @@ func (r *groupingColumnIterator) forwardToOffset() error {

count, err := r.values.ReadValues(r.readBuffer)
if err != nil && !errors.Is(err, io.EOF) {
return errors.Wrapf(err, "unable to read values from column '%s'", r.column.Name())
return fmt.Errorf("unable to read values from column '%s': %w", r.column.Name(), err)
}

r.unread = r.readBuffer[:count]
Expand Down
5 changes: 2 additions & 3 deletions pkg/inspect/row_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"

"github.com/parquet-go/parquet-go"
"github.com/pkg/errors"
"github.com/stoewer/parquet-cli/pkg/output"
)

Expand Down Expand Up @@ -54,7 +53,7 @@ func NewRowDump(file *parquet.File, options RowDumpOptions) (*RowDump, error) {
columns = make([]*parquet.Column, 0, len(options.Columns))
for _, idx := range options.Columns {
if idx >= len(all) {
return nil, errors.Errorf("column index expectd be below %d but was %d", idx, len(all))
return nil, fmt.Errorf("column index expectd be below %d but was %d", idx, len(all))
}
columns = append(columns, all[idx])
}
Expand All @@ -72,7 +71,7 @@ func NewRowDump(file *parquet.File, options RowDumpOptions) (*RowDump, error) {
for _, col := range columns {
it, err := newGroupingColumnIterator(col, nil, options.Pagination)
if err != nil {
return nil, errors.Wrapf(err, "unable to create row stats calculator")
return nil, fmt.Errorf("unable to create row stats calculator: %w", err)
}
c.columnIter = append(c.columnIter, it)
c.header = append(c.header, col.Name()+" d:r")
Expand Down
5 changes: 2 additions & 3 deletions pkg/inspect/row_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"

"github.com/parquet-go/parquet-go"
"github.com/pkg/errors"
"github.com/stoewer/parquet-cli/pkg/output"
)

Expand Down Expand Up @@ -52,7 +51,7 @@ func NewRowStatCalculator(file *parquet.File, options RowStatOptions) (*RowStatC
columns = make([]*parquet.Column, 0, len(options.Columns))
for _, idx := range options.Columns {
if idx >= len(all) {
return nil, errors.Errorf("column index expectd be below %d but was %d", idx, len(all))
return nil, fmt.Errorf("column index expectd be below %d but was %d", idx, len(all))
}
columns = append(columns, all[idx])
}
Expand All @@ -67,7 +66,7 @@ func NewRowStatCalculator(file *parquet.File, options RowStatOptions) (*RowStatC
for _, col := range columns {
it, err := newGroupingColumnIterator(col, nil, options.Pagination)
if err != nil {
return nil, errors.Wrapf(err, "unable to create row stats calculator")
return nil, fmt.Errorf("unable to create row stats calculator: %w", err)
}
c.columnIter = append(c.columnIter, it)
c.header = append(c.header, fmt.Sprintf("%d/%s: %s", col.Index(), col.Name(), rowCellFields[0]), rowCellFields[1], rowCellFields[2])
Expand Down
5 changes: 2 additions & 3 deletions pkg/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import (
"bytes"
"encoding/csv"
"encoding/json"
"errors"
"fmt"
"io"
"strings"
"text/tabwriter"

"github.com/pkg/errors"
)

// Format describes a printable data representation.
Expand Down Expand Up @@ -59,7 +58,7 @@ func PrintTable(w io.Writer, f Format, data Table) error {
case FormatCSV:
return printCSV(w, data)
default:
return errors.Errorf("format not supported yet '%s'", f)
return fmt.Errorf("format not supported yet '%s'", f)
}
}

Expand Down

0 comments on commit dfe8aa8

Please sign in to comment.