MCPcopy
hub / github.com/apache/answer / HandleResponse

Function HandleResponse

internal/base/handler/handler.go:34–61  ·  view source on GitHub ↗

HandleResponse Handle response body

(ctx *gin.Context, err error, data any)

Source from the content-addressed store, hash-verified

32
33// HandleResponse Handle response body
34func HandleResponse(ctx *gin.Context, err error, data any) {
35 lang := GetLangByCtx(ctx)
36 // no error
37 if err == nil {
38 ctx.JSON(http.StatusOK, NewRespBodyData(http.StatusOK, reason.Success, data).TrMsg(lang))
39 return
40 }
41
42 var myErr *myErrors.Error
43 // unknown error
44 if !errors.As(err, &myErr) {
45 log.Error(err, "\n", myErrors.LogStack(2, 5))
46 ctx.JSON(http.StatusInternalServerError, NewRespBody(
47 http.StatusInternalServerError, reason.UnknownError).TrMsg(lang))
48 return
49 }
50
51 // log internal server error
52 if myErrors.IsInternalServer(myErr) {
53 log.Error(myErr)
54 }
55
56 respBody := NewRespBodyFromError(myErr).TrMsg(lang)
57 if data != nil {
58 respBody.Data = data
59 }
60 ctx.JSON(myErr.Code, respBody)
61}
62
63// BindAndCheck bind request and check
64func BindAndCheck(ctx *gin.Context, data any) bool {

Callers 15

GetAllPluginStatusMethod · 0.92
GetPluginListMethod · 0.92
UpdatePluginStatusMethod · 0.92
GetPluginConfigMethod · 0.92
UpdatePluginConfigMethod · 0.92
GetAllAPIKeysMethod · 0.92
AddAPIKeyMethod · 0.92
UpdateAPIKeyMethod · 0.92
DeleteAPIKeyMethod · 0.92
UpdateUserStatusMethod · 0.92
UpdateUserRoleMethod · 0.92
AddUserMethod · 0.92

Calls 5

GetLangByCtxFunction · 0.85
NewRespBodyDataFunction · 0.85
NewRespBodyFunction · 0.85
NewRespBodyFromErrorFunction · 0.85
TrMsgMethod · 0.80

Tested by

no test coverage detected