Merge pull request #416 from trheyi/main

Improve error messages
This commit is contained in:
Max 2023-05-15 18:48:13 +08:00 committed by GitHub
commit 63a2fcaab2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 6 deletions

View file

@ -14,7 +14,7 @@ import (
"github.com/yaoapp/yao/neo" "github.com/yaoapp/yao/neo"
) )
var regExcp = regexp.MustCompile("^Exception\\|([0-9]+):(.+)$") var regExcp = regexp.MustCompile(`Exception\|(\d+):(.*)`)
// Serve start the api server // Serve start the api server
func setRouter(router *gin.Engine) { func setRouter(router *gin.Engine) {
@ -212,15 +212,22 @@ func setRouter(router *gin.Engine) {
ctx, err := script.NewContext(fmt.Sprintf("%v", sid), nil) ctx, err := script.NewContext(fmt.Sprintf("%v", sid), nil)
if err != 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 return
} }
defer ctx.Close() defer ctx.Close()
res, err := ctx.Call(fun.Method, fun.Args...) res, err := ctx.Call(fun.Method, fun.Args...)
if err != nil { if err != nil {
// parse Exception
code := 500 code := 500
message := err.Error() message := err.Error()
match := regExcp.FindStringSubmatch(message) match := regExcp.FindStringSubmatch(message)

View file

@ -94,11 +94,11 @@ func TestAPI(t *testing.T) {
code, rows := httpGet[arr]("/dsl/ReadDir?name=/models", t) code, rows := httpGet[arr]("/dsl/ReadDir?name=/models", t)
assert.Equal(t, 200, code) 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) code, rows = httpGet[arr]("/dsl/ReadDir?name=/models&recursive=1", t)
assert.Equal(t, 200, code) 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) code, length := httpPost[int]("/dsl/WriteFile?name=/models/foo.mod.yao", []byte(`{"name":"foo"}`), t)
assert.Equal(t, 200, code) assert.Equal(t, 200, code)