You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/pkg/common/http/http_resp.go

60 lines
1.2 KiB

3 years ago
package http
3 years ago
import (
"Open_IM/pkg/common/constant"
3 years ago
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
3 years ago
//"Open_IM/pkg/cms_api_struct"
3 years ago
"net/http"
"github.com/gin-gonic/gin"
3 years ago
3 years ago
)
type BaseResp struct {
Code int32 `json:"code"`
ErrMsg string `json:"err_msg"`
Data interface{} `json:"data"`
}
3 years ago
func RespHttp200(ctx *gin.Context, err error, data interface{}) {
var resp BaseResp
if v, ok := err.(constant.ErrInfo); ok {
resp.Code = v.Code()
resp.ErrMsg = v.Error()
} else {
resp.Code = constant.ErrServer.Code()
resp.ErrMsg = constant.ErrServer.Error()
3 years ago
}
3 years ago
resp.Data=data
3 years ago
ctx.JSON(http.StatusOK, resp)
}
3 years ago
3 years ago
// warp error
func WarpError(err constant.ErrInfo) error {
return status.Error(codes.Code(err.ErrCode), err.ErrMsg)
}
// parse error from server
func RespHttp200S(ctx *gin.Context, err error, data interface{}) {
var resp BaseResp
switch e := err.(type) {
case constant.ErrInfo:
resp.Code = e.ErrCode
default:
s, ok := status.FromError(err)
if !ok {
return
}
resp.Code = int32(s.Code())
resp.ErrMsg = s.Message()
}
resp.Data = data
ctx.JSON(http.StatusOK, resp)
}
3 years ago
//func CheckErr(pb interface{}) constant.ErrInfo{
//
//}