Skip to content

Commit e8b8fb8

Browse files
authored
Check command and error details (#29)
* 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
1 parent 03f5f31 commit e8b8fb8

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

api/http.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ package api
33
import (
44
"context"
55
_ "embed"
6+
"encoding/base64"
7+
"encoding/json"
8+
"fmt"
9+
"log"
610
"strings"
711

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

120124
if err != nil {
121-
res["err"] = err.Error()
125+
err_str := err.Error()
126+
err_str = strings.ReplaceAll(err_str, "\n", "\r\n")
127+
payload, err := json.Marshal(gin.H{
128+
"error": err_str,
129+
})
130+
if err != nil {
131+
log.Fatal(err)
132+
}
133+
payload_b64 := base64.StdEncoding.EncodeToString([]byte(payload))
134+
link := fmt.Sprintf("https://play.numscript.org/?payload=%v", payload_b64)
135+
res["err"] = err_str
136+
res["details"] = link
122137
}
123138

124139
c.JSON(200, res)

cmd/root.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/numary/ledger/config"
1616
"github.com/numary/ledger/ledger"
1717
"github.com/numary/ledger/storage"
18+
"github.com/numary/machine/script/compiler"
1819
"github.com/spf13/cobra"
1920
"github.com/spf13/viper"
2021
"go.uber.org/fx"
@@ -91,7 +92,7 @@ func Execute() {
9192
},
9293
})
9394

94-
script := &cobra.Command{
95+
script_exec := &cobra.Command{
9596
Use: "exec [ledger] [script]",
9697
Args: cobra.ExactArgs(2),
9798
Run: func(cmd *cobra.Command, args []string) {
@@ -144,18 +145,40 @@ func Execute() {
144145
log.Fatal(err)
145146
}
146147
if result.Ok {
147-
fmt.Println("Script ran successfully 👍")
148+
fmt.Println("Script ran successfully ")
148149
} else {
149150
log.Fatal(result.Err)
150151
}
151152
},
152153
}
153154

155+
script_check := &cobra.Command{
156+
Use: "check [script]",
157+
Args: cobra.ExactArgs(1),
158+
Run: func(cmd *cobra.Command, args []string) {
159+
config.Init()
160+
161+
b, err := ioutil.ReadFile(args[0])
162+
163+
if err != nil {
164+
log.Fatal(err)
165+
}
166+
167+
_, err = compiler.Compile(string(b))
168+
if err != nil {
169+
log.Fatal(err)
170+
} else {
171+
fmt.Println("Script is correct ✅")
172+
}
173+
},
174+
}
175+
154176
root.AddCommand(server)
155177
root.AddCommand(conf)
156178
root.AddCommand(UICmd)
157179
root.AddCommand(store)
158-
root.AddCommand(script)
180+
root.AddCommand(script_exec)
181+
root.AddCommand(script_check)
159182

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

0 commit comments

Comments
 (0)