Skip to content

Commit 0a819f0

Browse files
authored
Make the default error message one line (#241)
1 parent 6f345ac commit 0a819f0

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

error.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

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

1515
func (list MultiError) String() string {
@@ -27,21 +27,22 @@ func (list MultiError) Error() string {
2727
case 1:
2828
return list[0].Error()
2929
case 2:
30-
return list[0].Error() + "\n(and 1 other error)"
30+
return list[0].Error() + " (and 1 other error)"
3131
default:
32-
return fmt.Sprintf("%s\n(and %d other errors)", list[0].Error(), len(list))
32+
return fmt.Sprintf("%s (and %d other errors)", list[0].Error(), len(list)-1)
3333
}
3434
}
3535

3636
// FullError returns a full error message.
3737
func (list MultiError) FullError() string {
3838
var message bytes.Buffer
3939
for _, err := range list {
40-
fmt.Fprintln(&message, err.Error())
40+
fmt.Fprintln(&message, err.FullError())
4141
}
4242
return message.String()
4343
}
4444

45+
// Error is an error occured on parsing.
4546
type Error struct {
4647
Message string
4748
Position *token.Position
@@ -51,9 +52,14 @@ func (e *Error) String() string {
5152
return e.Error()
5253
}
5354

55+
// Error returns an error message.
5456
func (e *Error) Error() string {
57+
return fmt.Sprintf("syntax error: %s: %s", e.Position, e.Message)
58+
}
59+
60+
func (e *Error) FullError() string {
5561
var message bytes.Buffer
56-
fmt.Fprintf(&message, "syntax error: %s: %s", e.Position, e.Message)
62+
fmt.Fprint(&message, e.Error())
5763
if e.Position.Source != "" {
5864
fmt.Fprintf(&message, "\n%s", e.Position.Source)
5965
}

error_test.go

+12-6
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,7 @@ func TestMultiError(t *testing.T) {
4848
},
4949
{
5050
MultiError{err1},
51-
heredoc.Doc(`
52-
syntax error: foo:1:1: error 1
53-
1| a b
54-
| ^
55-
`),
51+
"syntax error: foo:1:1: error 1",
5652
heredoc.Doc(`
5753
syntax error: foo:1:1: error 1
5854
1| a b
@@ -61,19 +57,29 @@ func TestMultiError(t *testing.T) {
6157
},
6258
{
6359
MultiError{err1, err2},
60+
"syntax error: foo:1:1: error 1 (and 1 other error)",
6461
heredoc.Doc(`
6562
syntax error: foo:1:1: error 1
6663
1| a b
6764
| ^
68-
(and 1 other error)
65+
syntax error: foo:1:3: error 2
66+
1| a b
67+
| ^
6968
`),
69+
},
70+
{
71+
MultiError{err1, err2, err2},
72+
"syntax error: foo:1:1: error 1 (and 2 other errors)",
7073
heredoc.Doc(`
7174
syntax error: foo:1:1: error 1
7275
1| a b
7376
| ^
7477
syntax error: foo:1:3: error 2
7578
1| a b
7679
| ^
80+
syntax error: foo:1:3: error 2
81+
1| a b
82+
| ^
7783
`),
7884
},
7985
} {

tools/util/poslang/expr.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type Expr interface {
4343
Unparse() string
4444
}
4545

46-
// Typed expression types has a method EvalXXX.
46+
// Typed expression types has a method Eval*.
4747
// This method evaluates the expression on the given any value.
4848

4949
// PosExpr is a POS expression typed with token.Pos.

0 commit comments

Comments
 (0)