Skip to content

Commit

Permalink
cue: remove most uses of xerrors
Browse files Browse the repository at this point in the history
Move them to the errors package to make it easier
to remove later.

Change-Id: Ib14b9daab7ee3cafe3d3cefec578ab5d351997e1
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7681
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Nov 18, 2020
1 parent 3585705 commit 03abe87
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 19 deletions.
4 changes: 2 additions & 2 deletions cue/ast/ident_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"golang.org/x/xerrors"

"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/format"
"cuelang.org/go/cue/token"
)
Expand Down Expand Up @@ -138,7 +138,7 @@ func TestLabelName(t *testing.T) {
assert.Equal(t, tc.out, str, "value")
assert.Equal(t, tc.isIdent, isIdent, "isIdent")
assert.Equal(t, tc.err, err != nil, "err")
assert.Equal(t, tc.expr, xerrors.Is(err, ast.ErrIsExpression), "expr")
assert.Equal(t, tc.expr, errors.Is(err, ast.ErrIsExpression), "expr")
})
}
}
27 changes: 27 additions & 0 deletions cue/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,33 @@ func New(msg string) error {
return errors.New(msg)
}

// Unwrap returns the result of calling the Unwrap method on err, if err
// implements Unwrap. Otherwise, Unwrap returns nil.
func Unwrap(err error) error {
return xerrors.Unwrap(err)
}

// Is reports whether any error in err's chain matches target.
//
// An error is considered to match a target if it is equal to that target or if
// it implements a method Is(error) bool such that Is(target) returns true.
func Is(err, target error) bool {
return xerrors.Is(err, target)
}

// As finds the first error in err's chain that matches the type to which target
// points, and if so, sets the target to its value and returns true. An error
// matches a type if it is assignable to the target type, or if it has a method
// As(interface{}) bool such that As(target) returns true. As will panic if
// target is not a non-nil pointer to a type which implements error or is of
// interface type.
//
// The As method should set the target to its value and return true if err
// matches the type to which target points.
func As(err error, target interface{}) bool {
return xerrors.As(err, target)
}

// A Message implements the error interface as well as Message to allow
// internationalized messages. A Message is typically used as an embedding
// in a CUE message.
Expand Down
5 changes: 2 additions & 3 deletions cue/load/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ import (
"reflect"
"testing"

"golang.org/x/xerrors"

"cuelang.org/go/cue/build"
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/token"
)

Expand Down Expand Up @@ -58,7 +57,7 @@ func TestEmptyFolderImport(t *testing.T) {
func TestIgnoredCUEFilesImport(t *testing.T) {
_, err := getInst(".", testdata+"ignored")
var e *NoFilesError
ok := xerrors.As(err, &e)
ok := errors.As(err, &e)
if !ok {
t.Fatal(`Import("testdata/ignored") did not return NoFilesError.`)
}
Expand Down
4 changes: 1 addition & 3 deletions cue/load/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ import (
"strings"
"unicode"

"golang.org/x/xerrors"

"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/build"
"cuelang.org/go/cue/errors"
Expand Down Expand Up @@ -193,7 +191,7 @@ func (l *loader) cueFilesPackage(files []*build.File) *build.Instance {
rewriteFiles(pkg, pkg.Dir, true)
for _, err := range errors.Errors(fp.finalize()) { // ImportDir(&ctxt, dir, 0)
var x *NoFilesError
if len(pkg.OrphanedFiles) == 0 || !xerrors.As(err, &x) {
if len(pkg.OrphanedFiles) == 0 || !errors.As(err, &x) {
pkg.ReportError(err)
}
}
Expand Down
3 changes: 1 addition & 2 deletions encoding/openapi/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/token"
"cuelang.org/go/internal"
"golang.org/x/xerrors"
)

type buildContext struct {
Expand Down Expand Up @@ -564,7 +563,7 @@ func (b *builder) disjunction(a []cue.Value, f typeFunc) {
continue
}
err := v.Subsume(w, cue.Schema())
if err == nil || xerrors.Is(err, internal.ErrInexact) {
if err == nil || errors.Is(err, internal.ErrInexact) {
subsumed = append(subsumed, schemas[j])
}
}
Expand Down
3 changes: 1 addition & 2 deletions internal/core/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"cuelang.org/go/cue/token"
"cuelang.org/go/internal"
"cuelang.org/go/internal/core/adt"
"golang.org/x/xerrors"
)

// Config configures a compilation.
Expand Down Expand Up @@ -445,7 +444,7 @@ func (c *compiler) resolve(n *ast.Ident) adt.Expr {
}
name, _, err := ast.LabelName(lab)
switch {
case xerrors.Is(err, ast.ErrIsExpression):
case errors.Is(err, ast.ErrIsExpression):
if aliasInfo.expr == nil {
panic("unreachable")
}
Expand Down
3 changes: 1 addition & 2 deletions internal/core/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"cuelang.org/go/cue/literal"
"cuelang.org/go/internal"
"cuelang.org/go/internal/core/adt"
"golang.org/x/xerrors"
)

const (
Expand Down Expand Up @@ -102,7 +101,7 @@ func (w *printer) shortError(errs errors.Error) {
msg, args := errs.Msg()
fmt.Fprintf(w, msg, args...)

err := xerrors.Unwrap(errs)
err := errors.Unwrap(errs)
if err == nil {
break
}
Expand Down
3 changes: 1 addition & 2 deletions internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"strings"

"github.com/cockroachdb/apd/v2"
"golang.org/x/xerrors"

"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/ast/astutil"
Expand Down Expand Up @@ -429,7 +428,7 @@ type decorated struct {
}

func (e *decorated) Is(err error) bool {
return xerrors.Is(e.info, err) || xerrors.Is(e.cueError, err)
return errors.Is(e.info, err) || errors.Is(e.cueError, err)
}

// MaxDepth indicates the maximum evaluation depth. This is there to break
Expand Down
4 changes: 1 addition & 3 deletions pkg/tool/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import (
"os/exec"
"strings"

"golang.org/x/xerrors"

"cuelang.org/go/cue"
"cuelang.org/go/cue/errors"
"cuelang.org/go/internal/task"
Expand Down Expand Up @@ -86,7 +84,7 @@ func (c *execCmd) Run(ctx *task.Context) (res interface{}, err error) {
}
update["success"] = err == nil
if err != nil {
if exit := (*exec.ExitError)(nil); xerrors.As(err, &exit) && captureErr {
if exit := (*exec.ExitError)(nil); errors.As(err, &exit) && captureErr {
update["stderr"] = string(exit.Stderr)
} else {
update = nil
Expand Down

0 comments on commit 03abe87

Please sign in to comment.