Skip to content

Commit

Permalink
[devtools] [repl] Do not print character number of error
Browse files Browse the repository at this point in the history
The character number reported by Yaegi (where the error occurred) is always wrong. It will be better to simply hide this information from the user.
  • Loading branch information
elgopher committed Aug 14, 2023
1 parent b124302 commit 9462bf9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
27 changes: 25 additions & 2 deletions devtools/internal/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func (i Instance) runGoCode(source string) (r EvalResult, e error) {
err := recover()
if err != nil {
r = GoCodeExecuted
e = fmt.Errorf("panic when running Yaegi: %s", err)
e = fmt.Errorf("panic when running Yaegi Go interpreter: %s", err)
}
}()

Expand All @@ -244,14 +244,37 @@ func (i Instance) runGoCode(source string) (r EvalResult, e error) {
return Continued, nil
}

return GoCodeExecuted, err
return GoCodeExecuted, convertYaegiError(err)
}

printResult(res)

return GoCodeExecuted, nil
}

var (
yaegiCfgErrorPattern = regexp.MustCompile(`^(\d+:)(\d+:)(.*)`) // control flow graph generation error
yaegiScannerErrorPattern = regexp.MustCompile(`^_\.go:(\d+:)(\d+:)(.*)`)
)

// Yaegi reports wrong character number in error messages. It is better to remove this information completely.
func convertYaegiError(err error) error {
errStr := err.Error()

switch {
case yaegiCfgErrorPattern.MatchString(errStr):
return fmt.Errorf(
yaegiCfgErrorPattern.ReplaceAllString(errStr, "$1$3"),
)
case yaegiScannerErrorPattern.MatchString(errStr):
return fmt.Errorf(
yaegiScannerErrorPattern.ReplaceAllString(errStr, "$1$3"),
)
default:
return err
}
}

// shouldContinueOnError returns true if the error can be safely ignored
// to let the caller grab one more line before retrying to parse its input.
func shouldContinueOnError(err error, source string) bool {
Expand Down
31 changes: 31 additions & 0 deletions devtools/internal/interpreter/interpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,37 @@ func TestEval(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, "interpreter_test.anotherType: {}\n", swapper.ReadOutput(t))
})

t.Run("should convert error message to exclude invalid character number", func(t *testing.T) {
tests := map[string]struct {
source string
expectedErrMessage string
}{
"undefined": {
source: "undefinedVar",
expectedErrMessage: "1: undefined: undefinedVar",
},
"undefined in second line": {
source: "{ \n undefinedVar \n }",
expectedErrMessage: "2: undefined: undefinedVar",
},
"invalid number literal": {
source: "a := 123a123",
expectedErrMessage: "1: expected ';', found a123 (and 1 more errors)",
},
"invalid number literal in second line": {
source: "{ \n a := 123a123 \n }",
expectedErrMessage: "2: expected ';', found a123 (and 1 more errors)",
},
}
for name, testCase := range tests {
t.Run(name, func(t *testing.T) {
_, err := newInterpreterInstance(t).Eval(testCase.source)
require.Error(t, err)
assert.Equal(t, testCase.expectedErrMessage, err.Error())
})
}
})
}

func TestExport(t *testing.T) {
Expand Down

0 comments on commit 9462bf9

Please sign in to comment.