1 parent
568e516b1a
commit
7ea7b153a2
10 files changed
+595
-46
No files matched your search
+9
-8
@@ -12,6 +12,7 @@ import (
|
||||
|
||||
"git.zhangdeman.cn/zhangdeman/ginframework/diagnostic"
|
||||
"git.zhangdeman.cn/zhangdeman/ginframework/fault"
|
||||
"git.zhangdeman.cn/zhangdeman/ginframework/http/recovery"
|
||||
failure "git.zhangdeman.cn/zhangdeman/ginframework/internal/failure"
|
||||
"git.zhangdeman.cn/zhangdeman/ginframework/logger"
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -193,13 +194,6 @@ func (s *Server) initialize(ctx *gin.Context) {
|
||||
ctx.Request = ctx.Request.WithContext(diagnostic.With(ctx.Request.Context(), diagnostic.Snapshot{RequestID: id, TraceID: id, URI: ctx.Request.URL.Path, Method: ctx.Request.Method, Service: s.config.Service}))
|
||||
ctx.Header("X-Request-ID", id)
|
||||
defer func() {
|
||||
if recovered := recover(); recovered != nil {
|
||||
// panic 值可能含凭据,不写入响应或日志。
|
||||
panicError := fault.New(codePanic, "recover HTTP handler panic", nil)
|
||||
logger.RecordErrorContext(ctx.Request.Context(), "recover HTTP handler panic", panicError)
|
||||
s.writeResult(ctx, nil, panicError, http.StatusInternalServerError)
|
||||
ctx.Abort()
|
||||
}
|
||||
if ctx.Request.MultipartForm != nil {
|
||||
if err := ctx.Request.MultipartForm.RemoveAll(); err != nil {
|
||||
logger.RecordErrorContext(ctx.Request.Context(), "remove multipart temporary files failed", fault.New(codeMultipartCleanup, "remove multipart temporary files failed", err))
|
||||
@@ -209,5 +203,12 @@ func (s *Server) initialize(ctx *gin.Context) {
|
||||
s.options.AccessLogger.Info("HTTP request completed", zap.String("trace_id", id), zap.String("uri", ctx.Request.URL.Path), zap.String("method", ctx.Request.Method), zap.Int("status", ctx.Writer.Status()), zap.Duration("duration", time.Since(start)))
|
||||
}
|
||||
}()
|
||||
ctx.Next()
|
||||
recovery.New(recovery.Options{
|
||||
Report: func(ctx *gin.Context, _ recovery.Incident) {
|
||||
logger.RecordErrorContext(ctx.Request.Context(), "recover HTTP handler panic", fault.New(codePanic, "recover HTTP handler panic", nil))
|
||||
},
|
||||
Respond: func(ctx *gin.Context) {
|
||||
s.writeResult(ctx, nil, fault.New(codePanic, "recover HTTP handler panic", nil), http.StatusInternalServerError)
|
||||
},
|
||||
})(ctx)
|
||||
}
|
||||