Skip to content

Commit c2055fe

Browse files
authored
Merge pull request #56 from leancloud/feat-print-stack-with-err
feat: 输出带栈信息的错误
2 parents 98e4481 + 6e31d53 commit c2055fe

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

leancloud/server.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ func (engine *engine) functionHandler(w http.ResponseWriter, r *http.Request, na
170170
Code: 1,
171171
Message: err.Error(),
172172
StatusCode: http.StatusInternalServerError,
173-
callStack: debug.Stack(),
174173
})
175174
return
176175
}
@@ -219,7 +218,6 @@ func (engine *engine) classHookHandler(w http.ResponseWriter, r *http.Request, c
219218
Code: 1,
220219
Message: err.Error(),
221220
StatusCode: http.StatusInternalServerError,
222-
callStack: debug.Stack(),
223221
})
224222
return
225223
}
@@ -290,9 +288,10 @@ func (engine *engine) executeTimeout(r *FunctionRequest, name string, timeout ti
290288
}
291289

292290
func (engine *engine) unmarshalBody(r *http.Request) (interface{}, error) {
291+
defer r.Body.Close()
292+
293293
body := make(map[string]interface{})
294294
err := json.NewDecoder(r.Body).Decode(&body)
295-
296295
if err == io.EOF {
297296
return nil, nil
298297
}
@@ -301,11 +300,30 @@ func (engine *engine) unmarshalBody(r *http.Request) (interface{}, error) {
301300
return nil, err
302301
}
303302

304-
defer r.Body.Close()
305-
306303
return body, nil
307304
}
308305

306+
func printErrWithStack(errMsg string) {
307+
// 输出错误
308+
builder := new(strings.Builder)
309+
fmt.Fprintf(builder, "%s\n", errMsg)
310+
// 输出调用栈信息
311+
pc := make([]uintptr, 50) // 最多获取 50 层调用栈信息
312+
n := runtime.Callers(1, pc)
313+
frames := runtime.CallersFrames(pc[:n-1])
314+
// 跳过当前层级的信息
315+
frames.Next()
316+
// 打印剩余的调用栈信息
317+
for {
318+
frame, more := frames.Next()
319+
if !more {
320+
break
321+
}
322+
fmt.Fprintf(builder, "%s()\n\t%s:%d\n", frame.Function, frame.File, frame.Line)
323+
}
324+
fmt.Fprintf(os.Stderr, builder.String())
325+
}
326+
309327
func (engine *engine) constructRequest(r *http.Request, name string, rpc bool) (*FunctionRequest, error) {
310328
request := new(FunctionRequest)
311329
request.Meta = map[string]string{
@@ -323,6 +341,7 @@ func (engine *engine) constructRequest(r *http.Request, name string, rpc bool) (
323341
if engine.functions[name].defineOption["fetchUser"] == true && sessionToken != "" {
324342
user, err := Engine.client().Users.Become(sessionToken)
325343
if err != nil {
344+
printErrWithStack(fmt.Sprintf("Users.Become() failed. req=%s, err=%v", r.RequestURI, err))
326345
return nil, err
327346
}
328347
request.CurrentUser = user
@@ -336,12 +355,14 @@ func (engine *engine) constructRequest(r *http.Request, name string, rpc bool) (
336355

337356
params, err := engine.unmarshalBody(r)
338357
if err != nil {
358+
printErrWithStack(fmt.Sprintf("engine.unmarshalBody() failed. req=%s err=%v", r.RequestURI, err))
339359
return nil, err
340360
}
341361

342362
if rpc {
343363
decodedParams, err := decode(params)
344364
if err != nil {
365+
printErrWithStack(fmt.Sprintf("decode() failed. req=%s err=%v", r.RequestURI, err))
345366
return nil, err
346367
}
347368

0 commit comments

Comments
 (0)