Skip to content

Commit

Permalink
Merge pull request YaoApp#416 from trheyi/main
Browse files Browse the repository at this point in the history
Improve error messages
  • Loading branch information
trheyi authored May 15, 2023
2 parents 1780678 + a233fbc commit 63a2fca
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
15 changes: 11 additions & 4 deletions studio/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/yaoapp/yao/neo"
)

var regExcp = regexp.MustCompile("^Exception\\|([0-9]+):(.+)$")
var regExcp = regexp.MustCompile(`Exception\|(\d+):(.*)`)

// Serve start the api server
func setRouter(router *gin.Engine) {
Expand Down Expand Up @@ -212,15 +212,22 @@ func setRouter(router *gin.Engine) {

ctx, err := script.NewContext(fmt.Sprintf("%v", sid), nil)
if err != nil {
throw(c, 500, err.Error())
code := 500
message := err.Error()
match := regExcp.FindStringSubmatch(message)
if len(match) > 0 {
code, err = strconv.Atoi(match[1])
if err == nil {
message = strings.TrimSpace(match[2])
}
}
throw(c, code, message)
return
}
defer ctx.Close()

res, err := ctx.Call(fun.Method, fun.Args...)

if err != nil {
// parse Exception
code := 500
message := err.Error()
match := regExcp.FindStringSubmatch(message)
Expand Down
4 changes: 2 additions & 2 deletions studio/studio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ func TestAPI(t *testing.T) {

code, rows := httpGet[arr]("/dsl/ReadDir?name=/models", t)
assert.Equal(t, 200, code)
assert.Equal(t, 8, len(rows))
assert.Equal(t, 10, len(rows))

code, rows = httpGet[arr]("/dsl/ReadDir?name=/models&recursive=1", t)
assert.Equal(t, 200, code)
assert.Equal(t, 13, len(rows))
assert.Equal(t, 15, len(rows))

code, length := httpPost[int]("/dsl/WriteFile?name=/models/foo.mod.yao", []byte(`{"name":"foo"}`), t)
assert.Equal(t, 200, code)
Expand Down

0 comments on commit 63a2fca

Please sign in to comment.