From 429d5da1d2cf917640ab2e41c7fa9db16d9beff0 Mon Sep 17 00:00:00 2001 From: withchao <993506633@qq.com> Date: Fri, 24 Mar 2023 17:34:00 +0800 Subject: [PATCH] ctx --- pkg/apiresp/gin.go | 8 ++------ pkg/apiresp/resp.go | 17 ++++++++++------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/pkg/apiresp/gin.go b/pkg/apiresp/gin.go index 772a15c52..17bcef6bc 100644 --- a/pkg/apiresp/gin.go +++ b/pkg/apiresp/gin.go @@ -6,13 +6,9 @@ import ( ) func GinError(c *gin.Context, err error) { - if err == nil { - GinSuccess(c, nil) - return - } - c.JSON(http.StatusOK, apiError(err)) + c.JSON(http.StatusOK, ApiError(err)) } func GinSuccess(c *gin.Context, data any) { - c.JSON(http.StatusOK, apiSuccess(data)) + c.JSON(http.StatusOK, ApiSuccess(data)) } diff --git a/pkg/apiresp/resp.go b/pkg/apiresp/resp.go index 06001a541..52e7d7e5b 100644 --- a/pkg/apiresp/resp.go +++ b/pkg/apiresp/resp.go @@ -5,7 +5,7 @@ import ( "reflect" ) -type apiResponse struct { +type ApiCodeResponse struct { ErrCode int `json:"errCode"` ErrMsg string `json:"errMsg"` ErrDlt string `json:"errDlt"` @@ -30,23 +30,26 @@ func isAllFieldsPrivate(v any) bool { return true } -func apiSuccess(data any) *apiResponse { +func ApiSuccess(data any) *ApiCodeResponse { if isAllFieldsPrivate(data) { - return &apiResponse{} + return &ApiCodeResponse{} } - return &apiResponse{ + return &ApiCodeResponse{ Data: data, } } -func apiError(err error) *apiResponse { +func ApiError(err error) *ApiCodeResponse { + if err == nil { + return ApiSuccess(nil) + } unwrap := errs.Unwrap(err) if codeErr, ok := unwrap.(errs.CodeError); ok { - resp := apiResponse{ErrCode: codeErr.Code(), ErrMsg: codeErr.Msg(), ErrDlt: codeErr.Detail()} + resp := ApiCodeResponse{ErrCode: codeErr.Code(), ErrMsg: codeErr.Msg(), ErrDlt: codeErr.Detail()} if resp.ErrDlt == "" { resp.ErrDlt = err.Error() } return &resp } - return &apiResponse{ErrCode: errs.ServerInternalError, ErrMsg: err.Error()} + return &ApiCodeResponse{ErrCode: errs.ServerInternalError, ErrMsg: err.Error()} }