Skip to content

Commit

Permalink
Merge pull request #132 from numary/fix/script-exec
Browse files Browse the repository at this point in the history
Interpret all error codes when executing script
  • Loading branch information
altitude authored Jan 20, 2022
2 parents 1555fad + b2efc2e commit 74e90d3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
13 changes: 7 additions & 6 deletions cmd/script_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"regexp"

"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"io/ioutil"
"net/http"
"regexp"
)

func NewScriptExec() *cobra.Command {
Expand Down Expand Up @@ -61,10 +62,10 @@ func NewScriptExec() *cobra.Command {
logrus.Fatal(err)
}

if result.Ok {
fmt.Println("Script ran successfully ✅")
} else {
if result.Err != "" {
logrus.Fatal(result.Err)
} else {
fmt.Println("Script ran successfully ✅")
}
},
}
Expand Down
15 changes: 13 additions & 2 deletions pkg/ledger/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,22 @@ func (l *Ledger) Execute(ctx context.Context, script core.Script) error {
}

c, err := m.Execute()

if err != nil {
return fmt.Errorf("script failed: %v", err)
}
if c == vm.EXIT_FAIL {
return errors.New("script exited with error code EXIT_FAIL")

if c != vm.EXIT_OK {
switch c {
case vm.EXIT_FAIL:
return errors.New("script exited with error code EXIT_FAIL")
case vm.EXIT_FAIL_INVALID:
return errors.New("internal error: compiled script was invalid")
case vm.EXIT_FAIL_INSUFFICIENT_FUNDS:
return errors.New("account had insufficient funds")
default:
return errors.New("script execution failed")
}
}

t := core.Transaction{
Expand Down

0 comments on commit 74e90d3

Please sign in to comment.