Skip to content

Commit

Permalink
Shorten error message more
Browse files Browse the repository at this point in the history
  • Loading branch information
makenowjust committed Dec 21, 2024
1 parent b8e276c commit c162227
Show file tree
Hide file tree
Showing 24 changed files with 79 additions and 118 deletions.
12 changes: 4 additions & 8 deletions docs/content/error-recover/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@ Then, the following two errors are reported:

```sql
syntax error: :1:12: unexpected token: )

1: SELECT (1 +) + (* 2)
^


1| SELECT (1 +) + (* 2)
| ^
syntax error: :1:17: unexpected token: *

1: SELECT (1 +) + (* 2)
^
1| SELECT (1 +) + (* 2)
| ^
```

Hoever, the AST is also returned:
Expand Down
7 changes: 2 additions & 5 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ func (list MultiError) Error() string {
// FullError returns a full error message.
func (list MultiError) FullError() string {
var message bytes.Buffer
for i, err := range list {
if i > 0 {
fmt.Fprintln(&message)
}
for _, err := range list {
fmt.Fprintln(&message, err.Error())
}
return message.String()
Expand All @@ -58,7 +55,7 @@ func (e *Error) Error() string {
var message bytes.Buffer
fmt.Fprintf(&message, "syntax error: %s: %s", e.Position, e.Message)
if e.Position.Source != "" {
fmt.Fprintf(&message, "\n\n%s", e.Position.Source)
fmt.Fprintf(&message, "\n%s", e.Position.Source)
}
return message.String()
}
30 changes: 12 additions & 18 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestMultiError(t *testing.T) {
Column: 0,
EndLine: 0,
EndColumn: 1,
Source: " 1: a b\n ^",
Source: " 1| a b\n | ^",
},
}
err2 := &Error{
Expand All @@ -32,7 +32,7 @@ func TestMultiError(t *testing.T) {
Column: 2,
EndLine: 0,
EndColumn: 3,
Source: " 1: a b\n ^",
Source: " 1| a b\n | ^",
},
}

Expand All @@ -50,36 +50,30 @@ func TestMultiError(t *testing.T) {
MultiError{err1},
heredoc.Doc(`
syntax error: foo:1:1: error 1
1: a b
^
1| a b
| ^
`),
heredoc.Doc(`
syntax error: foo:1:1: error 1
1: a b
^
1| a b
| ^
`),
},
{
MultiError{err1, err2},
heredoc.Doc(`
syntax error: foo:1:1: error 1
1: a b
^
1| a b
| ^
(and 1 other error)
`),
heredoc.Doc(`
syntax error: foo:1:1: error 1
1: a b
^
1| a b
| ^
syntax error: foo:1:3: error 2
1: a b
^
1| a b
| ^
`),
},
} {
Expand Down
5 changes: 2 additions & 3 deletions testdata/result/ddl/!bad_alter_table_add_column.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
alter table foo add column baz string(max) null
--- Error
syntax error: testdata/input/ddl/!bad_alter_table_add_column.sql:1:44: expected token: <eof>, but: NULL

1: alter table foo add column baz string(max) null
^~~~
1| alter table foo add column baz string(max) null
| ^~~~


--- AST
Expand Down
5 changes: 2 additions & 3 deletions testdata/result/dml/!bad_insert.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ vales (1, 2, 3),
(4, 5, 6)
--- Error
syntax error: testdata/input/dml/!bad_insert.sql:2:1: expected beginning of simple query "(", SELECT, FROM, but: "vales"

2: vales (1, 2, 3),
^~~~~
2| vales (1, 2, 3),
| ^~~~~


--- AST
Expand Down
5 changes: 2 additions & 3 deletions testdata/result/expr/!bad_new_braced_constructor.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ NEW foo { bar: 1 + }

--- Error
syntax error: testdata/input/expr/!bad_new_braced_constructor.sql:1:20: unexpected token: }

1: NEW foo { bar: 1 + }
^
1| NEW foo { bar: 1 + }
| ^


--- AST
Expand Down
5 changes: 2 additions & 3 deletions testdata/result/expr/!bad_plus.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@

--- Error
syntax error: testdata/input/expr/!bad_plus.sql:2:1: unexpected token: <eof>

2:
^
2|
| ^


--- AST
Expand Down
11 changes: 4 additions & 7 deletions testdata/result/expr/!bad_plus2.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@

--- Error
syntax error: testdata/input/expr/!bad_plus2.sql:1:5: unexpected token: )

1: (1 +) + (2 +)
^

1| (1 +) + (2 +)
| ^
syntax error: testdata/input/expr/!bad_plus2.sql:1:13: unexpected token: )

1: (1 +) + (2 +)
^
1| (1 +) + (2 +)
| ^


--- AST
Expand Down
11 changes: 4 additions & 7 deletions testdata/result/expr/!bad_typed_struct.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ STRUCT<1>(2 +)

--- Error
syntax error: testdata/input/expr/!bad_typed_struct.sql:1:8: expected token: <ident>, ARRAY, STRUCT, but: <int>

1: STRUCT<1>(2 +)
^

1| STRUCT<1>(2 +)
| ^
syntax error: testdata/input/expr/!bad_typed_struct.sql:1:14: unexpected token: )

1: STRUCT<1>(2 +)
^
1| STRUCT<1>(2 +)
| ^


--- AST
Expand Down
5 changes: 2 additions & 3 deletions testdata/result/query/!bad_hint_select.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@

--- Error
syntax error: testdata/input/query/!bad_hint_select.sql:1:3: expected token: {, but: SELECT

1: @ select 1
^~~~~~
1| @ select 1
| ^~~~~~


--- AST
Expand Down
5 changes: 2 additions & 3 deletions testdata/result/query/!bad_hint_select_2.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
@{hint = 1} select
--- Error
syntax error: testdata/input/query/!bad_hint_select_2.sql:1:19: unexpected token: <eof>

1: @{hint = 1} select
^
1| @{hint = 1} select
| ^


--- AST
Expand Down
5 changes: 2 additions & 3 deletions testdata/result/query/!bad_select.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ select

--- Error
syntax error: testdata/input/query/!bad_select.sql:2:1: unexpected token: <eof>

2:
^
2|
| ^


--- AST
Expand Down
5 changes: 2 additions & 3 deletions testdata/result/query/!bad_select_order.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ select 1 order x asc

--- Error
syntax error: testdata/input/query/!bad_select_order.sql:1:16: expected token: BY, but: <ident>

1: select 1 order x asc
^
1| select 1 order x asc
| ^


--- AST
Expand Down
11 changes: 4 additions & 7 deletions testdata/result/query/!bad_select_union_select.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@
select union all select
--- Error
syntax error: testdata/input/query/!bad_select_union_select.sql:1:8: unexpected token: UNION

1: select union all select
^~~~~

1| select union all select
| ^~~~~
syntax error: testdata/input/query/!bad_select_union_select.sql:1:24: unexpected token: <eof>

1: select union all select
^
1| select union all select
| ^


--- AST
Expand Down
5 changes: 2 additions & 3 deletions testdata/result/statement/!bad_alter_table_add_column.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
alter table foo add column baz string(max) null
--- Error
syntax error: testdata/input/ddl/!bad_alter_table_add_column.sql:1:44: expected token: <eof>, but: NULL

1: alter table foo add column baz string(max) null
^~~~
1| alter table foo add column baz string(max) null
| ^~~~


--- AST
Expand Down
5 changes: 2 additions & 3 deletions testdata/result/statement/!bad_hint_select.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@

--- Error
syntax error: testdata/input/query/!bad_hint_select.sql:1:3: expected token: {, but: SELECT

1: @ select 1
^~~~~~
1| @ select 1
| ^~~~~~


--- AST
Expand Down
5 changes: 2 additions & 3 deletions testdata/result/statement/!bad_hint_select_2.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
@{hint = 1} select
--- Error
syntax error: testdata/input/query/!bad_hint_select_2.sql:1:19: unexpected token: <eof>

1: @{hint = 1} select
^
1| @{hint = 1} select
| ^


--- AST
Expand Down
5 changes: 2 additions & 3 deletions testdata/result/statement/!bad_insert.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ vales (1, 2, 3),
(4, 5, 6)
--- Error
syntax error: testdata/input/dml/!bad_insert.sql:2:1: expected beginning of simple query "(", SELECT, FROM, but: "vales"

2: vales (1, 2, 3),
^~~~~
2| vales (1, 2, 3),
| ^~~~~


--- AST
Expand Down
5 changes: 2 additions & 3 deletions testdata/result/statement/!bad_select.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ select

--- Error
syntax error: testdata/input/query/!bad_select.sql:2:1: unexpected token: <eof>

2:
^
2|
| ^


--- AST
Expand Down
5 changes: 2 additions & 3 deletions testdata/result/statement/!bad_select_order.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ select 1 order x asc

--- Error
syntax error: testdata/input/query/!bad_select_order.sql:1:16: expected token: BY, but: <ident>

1: select 1 order x asc
^
1| select 1 order x asc
| ^


--- AST
Expand Down
11 changes: 4 additions & 7 deletions testdata/result/statement/!bad_select_union_select.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@
select union all select
--- Error
syntax error: testdata/input/query/!bad_select_union_select.sql:1:8: unexpected token: UNION

1: select union all select
^~~~~

1| select union all select
| ^~~~~
syntax error: testdata/input/query/!bad_select_union_select.sql:1:24: unexpected token: <eof>

1: select union all select
^
1| select union all select
| ^


--- AST
Expand Down
6 changes: 3 additions & 3 deletions token/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ func (f *File) Position(pos, end Pos) *Position {
if count < 0 {
count = 0
}
fmt.Fprintf(&source, "%3d: %s\n", line+1, lineBuffer)
fmt.Fprintf(&source, " %s^%s", strings.Repeat(" ", column), strings.Repeat("~", count))
fmt.Fprintf(&source, "%3d| %s\n", line+1, lineBuffer)
fmt.Fprintf(&source, " | %s^%s", strings.Repeat(" ", column), strings.Repeat("~", count))
case line < endLine:
for l := line; l <= endLine; l++ {
if l > 0 {
fmt.Fprintln(&source)
}
lineBuffer := f.Buffer[f.lines[l] : f.lines[l+1]-1]
fmt.Fprintf(&source, "%3d: %s", l+1, lineBuffer)
fmt.Fprintf(&source, "%3d| %s", l+1, lineBuffer)
}
}

Expand Down
Loading

0 comments on commit c162227

Please sign in to comment.