diff --git a/internal/apiresp/resp.go b/internal/apiresp/resp.go index 4e9f72f26..d879a4683 100644 --- a/internal/apiresp/resp.go +++ b/internal/apiresp/resp.go @@ -1,10 +1,15 @@ package apiresp +import ( + "OpenIM/pkg/errs" + "fmt" +) + type apiResponse struct { ErrCode int `json:"errCode"` ErrMsg string `json:"errMsg"` ErrDlt string `json:"errDlt"` - Data any `json:"data"` + Data any `json:"data,omitempty"` } func apiSuccess(data any) *apiResponse { @@ -14,5 +19,13 @@ func apiSuccess(data any) *apiResponse { } func apiError(err error) *apiResponse { - return &apiResponse{ErrCode: 10000, ErrMsg: err.Error()} + unwrap := errs.Unwrap(err) + var dlt string + if unwrap != err { + dlt = fmt.Sprintf("%+v", dlt) + } + if codeErr, ok := unwrap.(errs.CodeError); ok { + return &apiResponse{ErrCode: codeErr.Code(), ErrMsg: codeErr.Msg(), ErrDlt: dlt} + } + return &apiResponse{ErrCode: errs.ServerInternalError, ErrMsg: err.Error(), ErrDlt: dlt} }