Skip to content

Commit

Permalink
Merge pull request #2185 from onflow/sainati/pretty-print-interpreter…
Browse files Browse the repository at this point in the history
…-errors

Pretty print interpreter errors
  • Loading branch information
dsainati1 committed Dec 9, 2022
2 parents 505e9c3 + 593edb5 commit 29fd837
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
38 changes: 38 additions & 0 deletions runtime/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,44 @@ func TestRuntimeError(t *testing.T) {
)
})

t.Run("execution error with position", func(t *testing.T) {

t.Parallel()

runtime := newTestInterpreterRuntime()

script := []byte(`
pub fun main() {
let x: AnyStruct? = nil
let y = x!
}
`)

runtimeInterface := &testRuntimeInterface{}

location := common.ScriptLocation{0x1}

_, err := runtime.ExecuteScript(
Script{
Source: script,
},
Context{
Interface: runtimeInterface,
Location: location,
},
)
require.EqualError(
t,
err,
"Execution failed:\n"+
"error: unexpectedly found nil while forcing an Optional value\n"+
" --> 0100000000000000000000000000000000000000000000000000000000000000:4:12\n"+
" |\n"+
"4 | let y = x!\n"+
" | ^^\n",
)
})

t.Run("parse error in import", func(t *testing.T) {

t.Parallel()
Expand Down
10 changes: 9 additions & 1 deletion runtime/interpreter/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/onflow/cadence/runtime/ast"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/cadence/runtime/errors"
"github.com/onflow/cadence/runtime/pretty"
"github.com/onflow/cadence/runtime/sema"
)

Expand Down Expand Up @@ -60,7 +61,14 @@ func (e Error) Unwrap() error {
}

func (e Error) Error() string {
return e.Err.Error()
var sb strings.Builder
sb.WriteString("Execution failed:\n")
printErr := pretty.NewErrorPrettyPrinter(&sb, false).
PrettyPrintError(e.Err, e.Location, map[common.Location][]byte{})
if printErr != nil {
panic(printErr)
}
return sb.String()
}

func (e Error) ChildErrors() []error {
Expand Down
21 changes: 21 additions & 0 deletions runtime/interpreter/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ import (

"github.com/stretchr/testify/require"

"github.com/onflow/cadence/runtime/ast"
"github.com/onflow/cadence/runtime/common"
. "github.com/onflow/cadence/runtime/interpreter"
"github.com/onflow/cadence/runtime/tests/utils"
)

func TestOverwriteError_Error(t *testing.T) {
Expand All @@ -40,3 +42,22 @@ func TestOverwriteError_Error(t *testing.T) {
"failed to save object: path /storage/test in account 0x0000000000000001 already stores an object",
)
}

func TestErrorOutputIncludesLocationRage(t *testing.T) {
t.Parallel()
require.Equal(t,
Error{
Location: utils.TestLocation,
Err: DereferenceError{
LocationRange: LocationRange{
Location: utils.TestLocation,
HasPosition: ast.Range{
StartPos: ast.Position{Offset: 0, Column: 0, Line: 0},
EndPos: ast.Position{Offset: 0, Column: 0, Line: 0},
},
},
},
}.Error(),
"Execution failed:\nerror: dereference failed\n --> test:0:0\n",
)
}

0 comments on commit 29fd837

Please sign in to comment.