2023-02-27 18:21:41 +08:00
|
|
|
package apiresp
|
|
|
|
|
|
2023-03-14 16:13:00 +08:00
|
|
|
import (
|
|
|
|
|
"OpenIM/pkg/errs"
|
|
|
|
|
"fmt"
|
|
|
|
|
)
|
|
|
|
|
|
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)
|
|
|
|
|
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}
|
2023-02-27 18:21:41 +08:00
|
|
|
}
|