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 1/3] 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()} } From eba69937266bb85eb0ec5c5fd3a33605df184891 Mon Sep 17 00:00:00 2001 From: withchao <993506633@qq.com> Date: Fri, 24 Mar 2023 17:36:36 +0800 Subject: [PATCH 2/3] ctx --- pkg/apiresp/gin.go | 2 +- pkg/apiresp/resp.go | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/pkg/apiresp/gin.go b/pkg/apiresp/gin.go index 17bcef6bc..e17f0144a 100644 --- a/pkg/apiresp/gin.go +++ b/pkg/apiresp/gin.go @@ -6,7 +6,7 @@ import ( ) func GinError(c *gin.Context, err error) { - c.JSON(http.StatusOK, ApiError(err)) + c.JSON(http.StatusOK, ParseError(err)) } func GinSuccess(c *gin.Context, data any) { diff --git a/pkg/apiresp/resp.go b/pkg/apiresp/resp.go index adc4e6f61..b724e1703 100644 --- a/pkg/apiresp/resp.go +++ b/pkg/apiresp/resp.go @@ -30,7 +30,7 @@ func isAllFieldsPrivate(v any) bool { return true } -func apiSuccess(data any) *ApiResponse { +func ApiSuccess(data any) *ApiResponse { if isAllFieldsPrivate(data) { return &ApiResponse{} } @@ -40,6 +40,9 @@ func apiSuccess(data any) *ApiResponse { } func ParseError(err error) *ApiResponse { + 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()} From 9f19fd46e7828ce384cc496c0ac80c3e7fde4808 Mon Sep 17 00:00:00 2001 From: withchao <993506633@qq.com> Date: Fri, 24 Mar 2023 17:43:52 +0800 Subject: [PATCH 3/3] ctx --- pkg/apiresp/resp.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pkg/apiresp/resp.go b/pkg/apiresp/resp.go index b724e1703..cd215a984 100644 --- a/pkg/apiresp/resp.go +++ b/pkg/apiresp/resp.go @@ -14,6 +14,9 @@ type ApiResponse struct { func isAllFieldsPrivate(v any) bool { typeOf := reflect.TypeOf(v) + if typeOf == nil { + return false + } if typeOf.Kind() == reflect.Ptr { typeOf = typeOf.Elem() }