Skip to content

Commit

Permalink
Merge pull request #56 from leancloud/feat-print-stack-with-err
Browse files Browse the repository at this point in the history
feat: 输出带栈信息的错误
  • Loading branch information
antigloss authored Jul 1, 2024
2 parents 98e4481 + 6e31d53 commit c2055fe
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions leancloud/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ func (engine *engine) functionHandler(w http.ResponseWriter, r *http.Request, na
Code: 1,
Message: err.Error(),
StatusCode: http.StatusInternalServerError,
callStack: debug.Stack(),
})
return
}
Expand Down Expand Up @@ -219,7 +218,6 @@ func (engine *engine) classHookHandler(w http.ResponseWriter, r *http.Request, c
Code: 1,
Message: err.Error(),
StatusCode: http.StatusInternalServerError,
callStack: debug.Stack(),
})
return
}
Expand Down Expand Up @@ -290,9 +288,10 @@ func (engine *engine) executeTimeout(r *FunctionRequest, name string, timeout ti
}

func (engine *engine) unmarshalBody(r *http.Request) (interface{}, error) {
defer r.Body.Close()

body := make(map[string]interface{})
err := json.NewDecoder(r.Body).Decode(&body)

if err == io.EOF {
return nil, nil
}
Expand All @@ -301,11 +300,30 @@ func (engine *engine) unmarshalBody(r *http.Request) (interface{}, error) {
return nil, err
}

defer r.Body.Close()

return body, nil
}

func printErrWithStack(errMsg string) {
// 输出错误
builder := new(strings.Builder)
fmt.Fprintf(builder, "%s\n", errMsg)
// 输出调用栈信息
pc := make([]uintptr, 50) // 最多获取 50 层调用栈信息
n := runtime.Callers(1, pc)
frames := runtime.CallersFrames(pc[:n-1])
// 跳过当前层级的信息
frames.Next()
// 打印剩余的调用栈信息
for {
frame, more := frames.Next()
if !more {
break
}
fmt.Fprintf(builder, "%s()\n\t%s:%d\n", frame.Function, frame.File, frame.Line)
}
fmt.Fprintf(os.Stderr, builder.String())
}

func (engine *engine) constructRequest(r *http.Request, name string, rpc bool) (*FunctionRequest, error) {
request := new(FunctionRequest)
request.Meta = map[string]string{
Expand All @@ -323,6 +341,7 @@ func (engine *engine) constructRequest(r *http.Request, name string, rpc bool) (
if engine.functions[name].defineOption["fetchUser"] == true && sessionToken != "" {
user, err := Engine.client().Users.Become(sessionToken)
if err != nil {
printErrWithStack(fmt.Sprintf("Users.Become() failed. req=%s, err=%v", r.RequestURI, err))
return nil, err
}
request.CurrentUser = user
Expand All @@ -336,12 +355,14 @@ func (engine *engine) constructRequest(r *http.Request, name string, rpc bool) (

params, err := engine.unmarshalBody(r)
if err != nil {
printErrWithStack(fmt.Sprintf("engine.unmarshalBody() failed. req=%s err=%v", r.RequestURI, err))
return nil, err
}

if rpc {
decodedParams, err := decode(params)
if err != nil {
printErrWithStack(fmt.Sprintf("decode() failed. req=%s err=%v", r.RequestURI, err))
return nil, err
}

Expand Down

0 comments on commit c2055fe

Please sign in to comment.