2023-02-27 18:21:41 +08:00
|
|
|
package apiresp
|
|
|
|
|
|
2023-03-14 16:13:00 +08:00
|
|
|
import (
|
2023-03-16 10:46:06 +08:00
|
|
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
|
2023-03-14 16:13:00 +08:00
|
|
|
)
|
|
|
|
|
|
2023-03-03 18:59:28 +08:00
|
|
|
type apiResponse struct {
|
2023-02-27 18:21:41 +08:00
|
|
|
ErrCode int `json:"errCode"`
|
|
|
|
|
ErrMsg string `json:"errMsg"`
|
|
|
|
|
ErrDlt string `json:"errDlt"`
|
2023-03-14 16:13:00 +08:00
|
|
|
Data any `json:"data,omitempty"`
|
2023-02-27 18:21:41 +08:00
|
|
|
}
|
|
|
|
|
|
2023-03-03 18:59:28 +08:00
|
|
|
func apiSuccess(data any) *apiResponse {
|
|
|
|
|
return &apiResponse{
|
2023-02-27 18:21:41 +08:00
|
|
|
Data: data,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-03 18:59:28 +08:00
|
|
|
func apiError(err error) *apiResponse {
|
2023-03-14 16:13:00 +08:00
|
|
|
unwrap := errs.Unwrap(err)
|
|
|
|
|
if codeErr, ok := unwrap.(errs.CodeError); ok {
|
2023-03-16 16:32:42 +08:00
|
|
|
return &apiResponse{ErrCode: codeErr.Code(), ErrMsg: codeErr.Msg(), ErrDlt: codeErr.Detail()}
|
2023-03-14 16:13:00 +08:00
|
|
|
}
|
2023-03-16 16:32:42 +08:00
|
|
|
return &apiResponse{ErrCode: errs.ServerInternalError, ErrMsg: err.Error()}
|
2023-02-27 18:21:41 +08:00
|
|
|
}
|