diff --git a/studio/router.go b/studio/router.go index b54b700e..ad76966e 100644 --- a/studio/router.go +++ b/studio/router.go @@ -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) { @@ -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) diff --git a/studio/studio_test.go b/studio/studio_test.go index 794e9d40..4d792935 100644 --- a/studio/studio_test.go +++ b/studio/studio_test.go @@ -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)