Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the default error message one line #241

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

// MultiError is a list of errors occured on parsing.
//
// Note that `ParseXXX` methods returns this wrapped error even if the error is just one.
// Note that Parse* methods returns this wrapped error even if the error is just one.
type MultiError []*Error

func (list MultiError) String() string {
Expand All @@ -27,21 +27,22 @@ func (list MultiError) Error() string {
case 1:
return list[0].Error()
case 2:
return list[0].Error() + "\n(and 1 other error)"
return list[0].Error() + " (and 1 other error)"
default:
return fmt.Sprintf("%s\n(and %d other errors)", list[0].Error(), len(list))
return fmt.Sprintf("%s (and %d other errors)", list[0].Error(), len(list)-1)
}
}

// FullError returns a full error message.
func (list MultiError) FullError() string {
var message bytes.Buffer
for _, err := range list {
fmt.Fprintln(&message, err.Error())
fmt.Fprintln(&message, err.FullError())
}
return message.String()
}

// Error is an error occured on parsing.
type Error struct {
Message string
Position *token.Position
Expand All @@ -51,9 +52,14 @@ func (e *Error) String() string {
return e.Error()
}

// Error returns an error message.
func (e *Error) Error() string {
return fmt.Sprintf("syntax error: %s: %s", e.Position, e.Message)
}

func (e *Error) FullError() string {
var message bytes.Buffer
fmt.Fprintf(&message, "syntax error: %s: %s", e.Position, e.Message)
fmt.Fprint(&message, e.Error())
if e.Position.Source != "" {
fmt.Fprintf(&message, "\n%s", e.Position.Source)
}
Expand Down
18 changes: 12 additions & 6 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,7 @@ func TestMultiError(t *testing.T) {
},
{
MultiError{err1},
heredoc.Doc(`
syntax error: foo:1:1: error 1
1| a b
| ^
`),
"syntax error: foo:1:1: error 1",
heredoc.Doc(`
syntax error: foo:1:1: error 1
1| a b
Expand All @@ -61,19 +57,29 @@ func TestMultiError(t *testing.T) {
},
{
MultiError{err1, err2},
"syntax error: foo:1:1: error 1 (and 1 other error)",
heredoc.Doc(`
syntax error: foo:1:1: error 1
1| a b
| ^
(and 1 other error)
syntax error: foo:1:3: error 2
1| a b
| ^
`),
},
{
MultiError{err1, err2, err2},
"syntax error: foo:1:1: error 1 (and 2 other errors)",
heredoc.Doc(`
syntax error: foo:1:1: error 1
1| a b
| ^
syntax error: foo:1:3: error 2
1| a b
| ^
syntax error: foo:1:3: error 2
1| a b
| ^
`),
},
} {
Expand Down
2 changes: 1 addition & 1 deletion tools/util/poslang/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type Expr interface {
Unparse() string
}

// Typed expression types has a method EvalXXX.
// Typed expression types has a method Eval*.
// This method evaluates the expression on the given any value.

// PosExpr is a POS expression typed with token.Pos.
Expand Down
Loading