test-errcode
withchao 2 years ago
parent 05ae254d3d
commit eba6993726

@ -6,7 +6,7 @@ import (
) )
func GinError(c *gin.Context, err error) { 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) { func GinSuccess(c *gin.Context, data any) {

@ -30,7 +30,7 @@ func isAllFieldsPrivate(v any) bool {
return true return true
} }
func apiSuccess(data any) *ApiResponse { func ApiSuccess(data any) *ApiResponse {
if isAllFieldsPrivate(data) { if isAllFieldsPrivate(data) {
return &ApiResponse{} return &ApiResponse{}
} }
@ -40,6 +40,9 @@ func apiSuccess(data any) *ApiResponse {
} }
func ParseError(err error) *ApiResponse { func ParseError(err error) *ApiResponse {
if err == nil {
return ApiSuccess(nil)
}
unwrap := errs.Unwrap(err) unwrap := errs.Unwrap(err)
if codeErr, ok := unwrap.(errs.CodeError); ok { if codeErr, ok := unwrap.(errs.CodeError); ok {
resp := ApiResponse{ErrCode: codeErr.Code(), ErrMsg: codeErr.Msg(), ErrDlt: codeErr.Detail()} resp := ApiResponse{ErrCode: codeErr.Code(), ErrMsg: codeErr.Msg(), ErrDlt: codeErr.Detail()}

Loading…
Cancel
Save