Skip to content

Commit

Permalink
Check command and error details (#29)
Browse files Browse the repository at this point in the history
* add check command to verify validity of a script
* add details field to /script api responses in case of an error, with a url to numscript playground showing the formatted error
  • Loading branch information
Azorlogh authored Jul 26, 2021
1 parent 03f5f31 commit e8b8fb8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
17 changes: 16 additions & 1 deletion api/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package api
import (
"context"
_ "embed"
"encoding/base64"
"encoding/json"
"fmt"
"log"
"strings"

"github.com/gin-contrib/cors"
Expand Down Expand Up @@ -118,7 +122,18 @@ func NewHttpAPI(lc fx.Lifecycle, resolver *ledger.Resolver) *HttpAPI {
}

if err != nil {
res["err"] = err.Error()
err_str := err.Error()
err_str = strings.ReplaceAll(err_str, "\n", "\r\n")
payload, err := json.Marshal(gin.H{
"error": err_str,
})
if err != nil {
log.Fatal(err)
}
payload_b64 := base64.StdEncoding.EncodeToString([]byte(payload))
link := fmt.Sprintf("https://play.numscript.org/?payload=%v", payload_b64)
res["err"] = err_str
res["details"] = link
}

c.JSON(200, res)
Expand Down
29 changes: 26 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/numary/ledger/config"
"github.com/numary/ledger/ledger"
"github.com/numary/ledger/storage"
"github.com/numary/machine/script/compiler"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/fx"
Expand Down Expand Up @@ -91,7 +92,7 @@ func Execute() {
},
})

script := &cobra.Command{
script_exec := &cobra.Command{
Use: "exec [ledger] [script]",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -144,18 +145,40 @@ func Execute() {
log.Fatal(err)
}
if result.Ok {
fmt.Println("Script ran successfully 👍")
fmt.Println("Script ran successfully ")
} else {
log.Fatal(result.Err)
}
},
}

script_check := &cobra.Command{
Use: "check [script]",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
config.Init()

b, err := ioutil.ReadFile(args[0])

if err != nil {
log.Fatal(err)
}

_, err = compiler.Compile(string(b))
if err != nil {
log.Fatal(err)
} else {
fmt.Println("Script is correct ✅")
}
},
}

root.AddCommand(server)
root.AddCommand(conf)
root.AddCommand(UICmd)
root.AddCommand(store)
root.AddCommand(script)
root.AddCommand(script_exec)
root.AddCommand(script_check)

if err := root.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
Expand Down

0 comments on commit e8b8fb8

Please sign in to comment.