HandleResponse Handle response body
(ctx *gin.Context, err error, data any)
| 32 | |
| 33 | // HandleResponse Handle response body |
| 34 | func 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 |
| 64 | func BindAndCheck(ctx *gin.Context, data any) bool { |
no test coverage detected