diff --git a/pkg/apiresp/gin.go b/pkg/apiresp/gin.go index 26e7be317..e17f0144a 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, ParseError(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 adc4e6f61..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() } @@ -30,7 +33,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 +43,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()}