This commit is contained in:
wangchuxiao
2022-02-12 17:13:31 +08:00
parent 6de61cb214
commit 52579c5fab
23 changed files with 618 additions and 111 deletions
+7 -24
View File
@@ -2,15 +2,14 @@ package http
import (
"Open_IM/pkg/common/constant"
"fmt"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
//"Open_IM/pkg/cms_api_struct"
"net/http"
"github.com/gin-gonic/gin"
)
type BaseResp struct {
@@ -20,32 +19,15 @@ type BaseResp struct {
}
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()
}
resp.Data=data
ctx.JSON(http.StatusOK, resp)
}
// 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
resp.ErrMsg = e.ErrMsg
default:
s, ok := status.FromError(err)
if !ok {
fmt.Println("need grpc format error")
return
}
resp.Code = int32(s.Code())
@@ -55,6 +37,7 @@ func RespHttp200S(ctx *gin.Context, err error, data interface{}) {
ctx.JSON(http.StatusOK, resp)
}
//func CheckErr(pb interface{}) constant.ErrInfo{
//
//}
// warp error
func WrapError(err constant.ErrInfo) error {
return status.Error(codes.Code(err.ErrCode), err.ErrMsg)
}