Merge branch 'errcode' of github.com:OpenIMSDK/Open-IM-Server into errcode

This commit is contained in:
wangchuxiao 2023-03-24 17:59:28 +08:00
commit 8399613018
2 changed files with 8 additions and 6 deletions

View File

@ -6,13 +6,9 @@ import (
) )
func GinError(c *gin.Context, err error) { func GinError(c *gin.Context, err error) {
if err == nil {
GinSuccess(c, nil)
return
}
c.JSON(http.StatusOK, ParseError(err)) c.JSON(http.StatusOK, ParseError(err))
} }
func GinSuccess(c *gin.Context, data any) { func GinSuccess(c *gin.Context, data any) {
c.JSON(http.StatusOK, apiSuccess(data)) c.JSON(http.StatusOK, ApiSuccess(data))
} }

View File

@ -14,6 +14,9 @@ type ApiResponse struct {
func isAllFieldsPrivate(v any) bool { func isAllFieldsPrivate(v any) bool {
typeOf := reflect.TypeOf(v) typeOf := reflect.TypeOf(v)
if typeOf == nil {
return false
}
if typeOf.Kind() == reflect.Ptr { if typeOf.Kind() == reflect.Ptr {
typeOf = typeOf.Elem() typeOf = typeOf.Elem()
} }
@ -30,7 +33,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 +43,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()}