Skip to content

Commit

Permalink
cue/errors: print positions explicitly
Browse files Browse the repository at this point in the history
This gives more control over printing and
prepares for error rework.

Issue cue-lang#52

Change-Id: I5629109298341cc700f860a6610f9362b7741a22
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2168
Reviewed-by: Marcel van Lohuizen <mpvl@google.com>
  • Loading branch information
mpvl committed Jun 7, 2019
1 parent aeb7dee commit bfa4c43
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
1 change: 0 additions & 1 deletion cmd/cue/cmd/testdata/tasks/cmd_baddisplay.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
unsupported op &(int, string):
$CWD/testdata/tasks/task_tool.cue:29:9
tool/cli:4:9

2 changes: 1 addition & 1 deletion cue/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ type bottom struct {

func (x *bottom) kind() kind { return bottomKind }

func (x *bottom) Position() []token.Pos {
func (x *bottom) Positions() []token.Pos {
if x.index != nil {
return appendPositions(nil, x.pos)
}
Expand Down
39 changes: 34 additions & 5 deletions cue/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"cuelang.org/go/cue/token"
"golang.org/x/exp/errors"
"golang.org/x/exp/errors/fmt"
"golang.org/x/xerrors"
)

// New is a convenience wrapper for errors.New in the core library.
Expand Down Expand Up @@ -115,14 +116,14 @@ func toErr(err error) Error {
return &posError{err: err}
}

func (e posError) Position() token.Position {
func (e *posError) Position() token.Position {
return e.pos
}

// Error implements the error interface.
func (e posError) Error() string { return fmt.Sprint(e) }
func (e *posError) Error() string { return fmt.Sprint(e) }

func (e posError) FormatError(p errors.Printer) error {
func (e *posError) FormatError(p errors.Printer) error {
next := e.err
if e.msg == "" {
next = errFormat(p, e.err)
Expand All @@ -136,6 +137,10 @@ func (e posError) FormatError(p errors.Printer) error {

}

func (e posError) Unwrap() error {
return e.err
}

func errFormat(p errors.Printer, err error) (next error) {
switch v := err.(type) {
case errors.Formatter:
Expand Down Expand Up @@ -250,6 +255,30 @@ func Print(w io.Writer, err error) {
}
}

func printError(w io.Writer, err Error) {
fmt.Fprintf(w, "%+v\n", err)
func printError(w io.Writer, err error) {
fmt.Fprintf(w, "%v", err)
printedColon := false
for ; err != nil; err = xerrors.Unwrap(err) {
switch x := err.(type) {
case interface{ Position() token.Position }:
if pos := x.Position().String(); pos != "-" {
if !printedColon {
fmt.Fprint(w, ":")
printedColon = true
}
fmt.Fprintf(w, "\n %v", pos)
}
case interface{ Positions() []token.Pos }:
for _, p := range x.Positions() {
if p.IsValid() {
if !printedColon {
fmt.Fprint(w, ":")
printedColon = true
}
fmt.Fprintf(w, "\n %v", p)
}
}
}
}
fmt.Fprintln(w)
}

0 comments on commit bfa4c43

Please sign in to comment.