Skip to content

Commit

Permalink
Merge pull request #3 from krakend/feature/parse-syntax-errors
Browse files Browse the repository at this point in the history
Properly parse syntax errors
  • Loading branch information
kpacha authored Nov 14, 2024
2 parents eeb2454 + d0ebcab commit a3efb11
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 9 deletions.
47 changes: 38 additions & 9 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,22 +177,26 @@ func newError(err error, h errSourceHandler) error {
switch err.(type) {
case *lua.ApiError:
e := err.(*lua.ApiError)
p := strings.Split(e.Object.String(), ":")
c := len(p)

var (
problem = -1
text = e.Error()
)

if c > 1 {
if v, err := strconv.Atoi(p[1]); err == nil {
problem = v
if e.Type == lua.ApiErrorSyntax {
text, problem = parseSyntaxError(e.Object.String())
} else {
p := strings.Split(e.Object.String(), ":")
c := len(p)

if c > 1 {
if v, err := strconv.Atoi(p[1]); err == nil {
problem = v
}
}
}

if c > 2 {
text = strings.Trim(strings.Join(p[2:], ":"), " ")
if c > 2 {
text = strings.Trim(strings.Join(p[2:], ":"), " ")
}
}

return &Error{
Expand All @@ -205,6 +209,31 @@ func newError(err error, h errSourceHandler) error {
return err
}

func parseSyntaxError(errorText string) (string, int) {
var (
problem = -1
text = errorText
)

if strings.Contains(errorText, " at EOF:") {
problem = 0
text = strings.TrimSpace(strings.Split(errorText, ":")[1]) + " at EOF"

return text, problem
}

parts := strings.Split(errorText, " near ")
if len(parts) == 2 {
text = strings.Join(strings.Fields(strings.TrimSuffix(parts[1], "\n")), " ")
where := strings.Split(strings.TrimSuffix(strings.ReplaceAll(parts[0], "<string> line:", ""), ")"), "(column:")
if v, err := strconv.Atoi(where[0]); err == nil {
problem = v
}

}
return text, problem
}

func highlight(s string) string {
var buf bytes.Buffer
if err := quick.Highlight(&buf, s, "lua", "terminal", "solarized-dark"); err != nil {
Expand Down
52 changes: 52 additions & 0 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,58 @@ import (
"testing"
)

func TestErrorSourceSyntaxError(t *testing.T) {
tests := []struct {
name string
src string
expected string
}{
{
name: "Inline",
src: "local p = person.new('Steeve');lokal t = 'fail'",
expected: "Line 1: 't': parse error",
},
{
name: "Parsing error",
src: "local p = person.new('Steeve')\nlokal t = 'fail'",
expected: "Line 2: 't': parse error",
},
{
name: "Bad token",
src: "local p = person.new('Steeve')\nlocal t & 'fail'",
expected: "Line 2: '&': Invalid token",
},
{
name: "Unterminated string",
src: "local p = person.new('Steeve)\nlocal t = 'okay'",
expected: "Line 2: 'Steeve)': unterminated string",
},
{
name: "End of file",
src: "local p = person.new('Steeve')\nprint(p:email()",
expected: "Line 0: syntax error at EOF",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
b := getBinder()

if err := b.DoString(test.src); err != nil {
switch err.(type) {
case *Error:
e := err.(*Error)
if e.Error() != test.expected {
t.Errorf("Error message does not match, expected=\"%s\" got=\"%s\"", test.expected, e.Error())
}
default:
t.Error("Must return error", err)
}
}
})
}
}

func TestErrorSourceSmall_Func(t *testing.T) {
b := getBinder()

Expand Down

0 comments on commit a3efb11

Please sign in to comment.