Improve error messages
This commit is contained in:
parent
e3a60a88c5
commit
a233fbcd67
2 changed files with 13 additions and 6 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue