Skip to content

Commit

Permalink
all: use x/xerrors
Browse files Browse the repository at this point in the history
Fixes go-hep#480.
  • Loading branch information
sbinet committed Nov 19, 2019
1 parent f9cbd76 commit 86f5de3
Show file tree
Hide file tree
Showing 206 changed files with 1,730 additions and 1,839 deletions.
4 changes: 2 additions & 2 deletions ci/run-tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"strings"
"time"

"github.com/pkg/errors"
"golang.org/x/xerrors"
)

func main() {
Expand Down Expand Up @@ -106,7 +106,7 @@ func pkgList() ([]string, error) {

err := cmd.Run()
if err != nil {
return nil, errors.Wrapf(err, "could not get package list")
return nil, xerrors.Errorf("could not get package list: %w", err)
}

var pkgs []string
Expand Down
8 changes: 4 additions & 4 deletions ci/update-godoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
"os/exec"
"strings"

"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
"golang.org/x/xerrors"
)

func main() {
Expand Down Expand Up @@ -55,10 +55,10 @@ func main() {
v.Add("path", pkg)
resp, err := http.PostForm("https://godoc.org/-/refresh", v)
if err != nil {
return errors.Wrapf(err, "could not post %q", pkg)
return xerrors.Errorf("could not post %q: %w", pkg, err)
}
if resp.StatusCode != http.StatusOK {
return errors.Errorf("invalid response status for %q: %v", pkg, resp.Status)
return xerrors.Errorf("invalid response status for %q: %v", pkg, resp.Status)
}
}
return nil
Expand All @@ -80,7 +80,7 @@ func pkgList() ([]string, error) {

err := cmd.Run()
if err != nil {
return nil, errors.Wrapf(err, "could not get package list")
return nil, xerrors.Errorf("could not get package list: %w", err)
}

var pkgs []string
Expand Down
18 changes: 9 additions & 9 deletions cmd/root2arrow/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ import (
"github.com/apache/arrow/go/arrow/array"
"github.com/apache/arrow/go/arrow/ipc"
"github.com/apache/arrow/go/arrow/memory"
"github.com/pkg/errors"
"go-hep.org/x/hep/groot"
"go-hep.org/x/hep/groot/rarrow"
_ "go-hep.org/x/hep/groot/riofs/plugin/http"
_ "go-hep.org/x/hep/groot/riofs/plugin/xrootd"
"go-hep.org/x/hep/groot/rtree"
"golang.org/x/xerrors"
)

func main() {
Expand Down Expand Up @@ -84,7 +84,7 @@ func process(oname, fname, tname string, stream bool) error {

tree, ok := obj.(rtree.Tree)
if !ok {
return errors.Errorf("object %q in file %q is not a rtree.Tree", tname, fname)
return xerrors.Errorf("object %q in file %q is not a rtree.Tree", tname, fname)
}

mem := memory.NewGoAllocator()
Expand Down Expand Up @@ -125,14 +125,14 @@ func processStream(o io.Writer, r array.RecordReader, mem memory.Allocator) erro
rec := r.Record()
err = w.Write(rec)
if err != nil {
return errors.Wrapf(err, "could not write record[%d]", i)
return xerrors.Errorf("could not write record[%d]: %w", i, err)
}
i++
}

err = w.Close()
if err != nil {
return errors.Wrap(err, "could not close Arrow stream writer")
return xerrors.Errorf("could not close Arrow stream writer: %w", err)
}

return nil
Expand All @@ -141,7 +141,7 @@ func processStream(o io.Writer, r array.RecordReader, mem memory.Allocator) erro
func processFile(o *os.File, r array.RecordReader, mem memory.Allocator) error {
w, err := ipc.NewFileWriter(o, ipc.WithSchema(r.Schema()), ipc.WithAllocator(mem))
if err != nil {
return errors.Wrap(err, "could not create Arrow file writer")
return xerrors.Errorf("could not create Arrow file writer: %w", err)
}
defer w.Close()

Expand All @@ -150,24 +150,24 @@ func processFile(o *os.File, r array.RecordReader, mem memory.Allocator) error {
rec := r.Record()
err = w.Write(rec)
if err != nil {
return errors.Wrapf(err, "could not write record[%d]", i)
return xerrors.Errorf("could not write record[%d]: %w", i, err)
}
i++
}

err = w.Close()
if err != nil {
return errors.Wrap(err, "could not close Arrow file writer")
return xerrors.Errorf("could not close Arrow file writer: %w", err)
}

err = o.Sync()
if err != nil {
return errors.Wrap(err, "could not sync data to disk")
return xerrors.Errorf("could not sync data to disk: %w", err)
}

err = o.Close()
if err != nil {
return errors.Wrap(err, "could not close output file")
return xerrors.Errorf("could not close output file: %w", err)
}

return nil
Expand Down
5 changes: 3 additions & 2 deletions cmd/root2arrow/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ package main // import "go-hep.org/x/hep/cmd/root2arrow"

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"testing"

"golang.org/x/xerrors"
)

func init() {
o, err := exec.Command("go", "get", "github.com/apache/arrow/go/arrow/ipc/cmd/arrow-cat").CombinedOutput()
if err != nil {
panic(fmt.Errorf("could not install arrow-cat command:\n%v\nerr=%v", string(o), err))
panic(xerrors.Errorf("could not install arrow-cat command:\n%v\nerr: %w", string(o), err))
}

}
Expand Down
20 changes: 10 additions & 10 deletions cmd/root2csv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ import (
"reflect"
"strings"

"github.com/pkg/errors"
"go-hep.org/x/hep/csvutil"
"go-hep.org/x/hep/groot"
_ "go-hep.org/x/hep/groot/riofs/plugin/http"
_ "go-hep.org/x/hep/groot/riofs/plugin/xrootd"
"go-hep.org/x/hep/groot/rtree"
"golang.org/x/xerrors"
)

func main() {
Expand Down Expand Up @@ -72,18 +72,18 @@ func process(oname, fname, tname string) error {

f, err := groot.Open(fname)
if err != nil {
return errors.Wrap(err, "could not open ROOT file")
return xerrors.Errorf("could not open ROOT file: %w", err)
}
defer f.Close()

obj, err := f.Get(tname)
if err != nil {
return errors.Wrap(err, "could not get ROOT object")
return xerrors.Errorf("could not get ROOT object: %w", err)
}

tree, ok := obj.(rtree.Tree)
if !ok {
return errors.Errorf("object %q in file %q is not a rtree.Tree", tname, fname)
return xerrors.Errorf("object %q in file %q is not a rtree.Tree", tname, fname)
}

var nt = ntuple{n: tree.Entries()}
Expand Down Expand Up @@ -119,23 +119,23 @@ func process(oname, fname, tname string) error {

sc, err := rtree.NewTreeScannerVars(tree, nt.args...)
if err != nil {
return errors.Wrap(err, "could not create tree scanner")
return xerrors.Errorf("could not create tree scanner: %w", err)
}
defer sc.Close()

nrows := 0
for sc.Next() {
err = sc.Scan(nt.vars...)
if err != nil {
return errors.Wrapf(err, "could not scan entry %d", nrows)
return xerrors.Errorf("could not scan entry %d: %w", nrows, err)
}
nt.fill()
nrows++
}

tbl, err := csvutil.Create(oname)
if err != nil {
return errors.Wrap(err, "could not create output CSV file")
return xerrors.Errorf("could not create output CSV file: %w", err)
}
defer tbl.Close()
tbl.Writer.Comma = ';'
Expand All @@ -150,7 +150,7 @@ func process(oname, fname, tname string) error {
strings.Join(names, string(tbl.Writer.Comma)),
))
if err != nil {
return errors.Wrap(err, "could not write CSV header")
return xerrors.Errorf("could not write CSV header: %w", err)
}

row := make([]interface{}, len(nt.cols))
Expand All @@ -160,13 +160,13 @@ func process(oname, fname, tname string) error {
}
err = tbl.WriteRow(row...)
if err != nil {
return errors.Wrapf(err, "could not write row %d to CSV file", irow)
return xerrors.Errorf("could not write row %d to CSV file: %w", irow, err)
}
}

err = tbl.Close()
if err != nil {
return errors.Wrap(err, "could not close CSV output file")
return xerrors.Errorf("could not close CSV output file: %w", err)
}

return nil
Expand Down
10 changes: 5 additions & 5 deletions cmd/yoda2root/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"os"
"path/filepath"

"github.com/pkg/errors"
"go-hep.org/x/hep/groot"
"go-hep.org/x/hep/groot/riofs"
_ "go-hep.org/x/hep/groot/riofs/plugin/http"
Expand All @@ -29,6 +28,7 @@ import (
"go-hep.org/x/hep/hbook"
"go-hep.org/x/hep/hbook/rootcnv"
"go-hep.org/x/hep/hbook/yodacnv"
"golang.org/x/xerrors"
)

func main() {
Expand Down Expand Up @@ -80,22 +80,22 @@ func convert(w *riofs.File, fname string) error {
var r io.ReadCloser
r, err := os.Open(fname)
if err != nil {
return errors.Errorf("error opening file [%s]: %v", fname, err)
return xerrors.Errorf("error opening file [%s]: %w", fname, err)
}
defer r.Close()

if filepath.Ext(fname) == ".gz" {
rz, err := gzip.NewReader(r)
if err != nil {
return errors.Errorf("could not open gzip file [%s]: %v", fname, err)
return xerrors.Errorf("could not open gzip file [%s]: %w", fname, err)
}
defer rz.Close()
r = rz
}

vs, err := yodacnv.Read(r)
if err != nil {
return errors.Errorf("error decoding YODA file [%s]: %v", fname, err)
return xerrors.Errorf("error decoding YODA file [%s]: %w", fname, err)
}

for i, v := range vs {
Expand Down Expand Up @@ -125,7 +125,7 @@ func convert(w *riofs.File, fname string) error {
}
err = w.Put(key, obj)
if err != nil {
return errors.Errorf("error writing %q from YODA file [%s]: %v\n", v.Name(), fname, err)
return xerrors.Errorf("error writing %q from YODA file [%s]: %w", v.Name(), fname, err)
}
}

Expand Down
13 changes: 7 additions & 6 deletions csvutil/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ package csvutil // import "go-hep.org/x/hep/csvutil"
import (
"bufio"
"encoding/csv"
"fmt"
"math"
"os"
"reflect"
"strconv"
"strings"

"golang.org/x/xerrors"
)

func min(a, b int) int {
Expand Down Expand Up @@ -143,12 +144,12 @@ func (tbl *Table) WriteHeader(hdr string) error {
func (tbl *Table) WriteRow(args ...interface{}) error {
var err error
if tbl.Writer == nil {
return fmt.Errorf("csvutil: Table is not in write mode")
return xerrors.Errorf("csvutil: Table is not in write mode")
}

switch len(args) {
case 0:
return fmt.Errorf("csvutil: Table.WriteRow needs at least one argument")
return xerrors.Errorf("csvutil: Table.WriteRow needs at least one argument")

case 1:
// maybe special case: struct?
Expand Down Expand Up @@ -186,7 +187,7 @@ func (tbl *Table) write(args ...interface{}) error {
case reflect.String:
rec[i] = rv.String()
default:
return fmt.Errorf("csvutil: invalid type (%[1]T) %[1]v (kind=%[2]v)", arg, rt.Kind())
return xerrors.Errorf("csvutil: invalid type (%[1]T) %[1]v (kind=%[2]v)", arg, rt.Kind())
}
}
return tbl.Writer.Write(rec)
Expand Down Expand Up @@ -258,7 +259,7 @@ func (rows *Rows) Scan(dest ...interface{}) error {

switch len(dest) {
case 0:
err = fmt.Errorf("csvutil: Rows.Scan needs at least one argument")
err = xerrors.Errorf("csvutil: Rows.Scan needs at least one argument")
return err

case 1:
Expand Down Expand Up @@ -316,7 +317,7 @@ func (rows *Rows) scan(args ...interface{}) error {
rv.SetString(rec)

default:
return fmt.Errorf("csvutil: invalid type (%T) %q (kind=%v)", rv.Interface(), rec, rt.Kind())
return xerrors.Errorf("csvutil: invalid type (%T) %q (kind=%v)", rv.Interface(), rec, rt.Kind())
}
}

Expand Down
Loading

0 comments on commit 86f5de3

Please sign in to comment.