MCPcopy Create free account
hub / github.com/53AI/53AIHub / wrapHandlerFunc

Function wrapHandlerFunc

api/controller/wps.go:34–97  ·  view source on GitHub ↗

wrapHandlerFunc 封装返回处理函数

(f func(*gin.Context) (any, error))

Source from the content-addressed store, hash-verified

32
33// wrapHandlerFunc 封装返回处理函数
34func wrapHandlerFunc(f func(*gin.Context) (any, error)) gin.HandlerFunc {
35 return func(c *gin.Context) {
36 begin := time.Now()
37 data, err := f(c)
38 cost := time.Since(begin)
39
40 if err != nil {
41 var respErr *weboffice.Error
42 if e, ok := err.(*weboffice.Error); ok {
43 respErr = e
44 } else {
45 respErr = weboffice.ErrInternalError.WithMessage(err.Error())
46 }
47
48 // 记录详细的错误信息
49 logger.SysErrorf("WPS接口调用失败: %s %s 错误码=%d 错误消息=%s 耗时=%s",
50 c.Request.Method, c.Request.RequestURI, respErr.Code(), respErr.Message(), cost.String())
51
52 // 记录请求头信息(排除敏感信息)
53 headers := make(map[string]string)
54 for key, values := range c.Request.Header {
55 if key != "Authorization" && key != "X-Upload-Token" {
56 headers[key] = strings.Join(values, ",")
57 }
58 }
59 logger.SysErrorf("请求头信息: %+v", headers)
60
61 // 记录请求参数(如果是POST/PUT请求,记录请求体的一部分)
62 if c.Request.Method == "POST" || c.Request.Method == "PUT" {
63 body, _ := io.ReadAll(c.Request.Body)
64 if len(body) > 0 {
65 // 限制日志大小,避免记录过大的请求体
66 bodyStr := string(body)
67 if len(bodyStr) > 500 {
68 bodyStr = bodyStr[:500] + "..."
69 }
70 logger.SysErrorf("请求体内容: %s", bodyStr)
71 // 恢复请求体,以便后续处理
72 c.Request.Body = io.NopCloser(strings.NewReader(bodyStr))
73 }
74 }
75
76 c.JSON(respErr.StatusCode(), &weboffice.Reply{Code: respErr.Code(), Message: respErr.Message()})
77 } else {
78 // 记录成功的调用信息
79 logger.SysLogf("WPS接口调用成功: %s %s 耗时=%s",
80 c.Request.Method, c.Request.RequestURI, cost.String())
81
82 // 记录响应数据的摘要信息
83 if data != nil {
84 // 将响应数据转换为JSON字符串,限制长度
85 if jsonStr, err := json.Marshal(data); err == nil {
86 jsonStrStr := string(jsonStr)
87 if len(jsonStrStr) > 500 {
88 jsonStrStr = jsonStrStr[:500] + "..."
89 }
90 logger.SysLogf("响应数据摘要: %s", jsonStrStr)
91 }

Callers 9

RenameWPSFileFunction · 0.85
PrepareWPSFileUploadFunction · 0.85
GetWPSFileUploadAddressFunction · 0.85
ExecuteWPSFileUploadFunction · 0.85
CompleteWPSFileUploadFunction · 0.85
GetWPSFileFunction · 0.85
GetWPSFileDownloadFunction · 0.85
GetWPSFilePermissionFunction · 0.85
GetWPSUsersFunction · 0.85

Calls 7

CodeMethod · 0.95
MessageMethod · 0.95
StatusCodeMethod · 0.95
WithMessageMethod · 0.80
fFunction · 0.50
ErrorMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected