|
|
@@ -2,12 +2,13 @@ package myerrors
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
- "dashoo.cn/common_definition/comm_def"
|
|
|
+ "fmt"
|
|
|
"github.com/gogf/gf/errors/gcode"
|
|
|
"github.com/gogf/gf/errors/gerror"
|
|
|
"github.com/gogf/gf/frame/g"
|
|
|
"github.com/smallnest/rpcx/codec"
|
|
|
"github.com/smallnest/rpcx/protocol"
|
|
|
+ "github.com/smallnest/rpcx/share"
|
|
|
"strings"
|
|
|
)
|
|
|
|
|
|
@@ -83,37 +84,37 @@ func handleErrMsg(errCode gcode.Code, errMsg ...string) string {
|
|
|
|
|
|
type HandleErrorPlugin struct{}
|
|
|
|
|
|
-func (p HandleErrorPlugin) getSerializeType(mes *protocol.Message) codec.Codec {
|
|
|
- var serializer codec.Codec
|
|
|
+func (p HandleErrorPlugin) getSerializeType(mes *protocol.Message) (codec.Codec, g.Map) {
|
|
|
+ var err error
|
|
|
+ serializer := share.Codecs[mes.SerializeType()]
|
|
|
+ data := g.Map{}
|
|
|
switch mes.SerializeType() {
|
|
|
case protocol.SerializeNone:
|
|
|
- return nil
|
|
|
case protocol.JSON:
|
|
|
- serializer = codec.JSONCodec{}
|
|
|
+ err = serializer.Decode(mes.Payload, &data)
|
|
|
case protocol.ProtoBuffer:
|
|
|
- serializer = codec.PBCodec{}
|
|
|
case protocol.MsgPack:
|
|
|
- serializer = codec.MsgpackCodec{}
|
|
|
+ err = serializer.Decode(mes.Payload, &data)
|
|
|
case protocol.Thrift:
|
|
|
- serializer = codec.ThriftCodec{}
|
|
|
}
|
|
|
- return serializer
|
|
|
+ fmt.Println(err)
|
|
|
+ return serializer, data
|
|
|
}
|
|
|
|
|
|
func (p HandleErrorPlugin) PreWriteResponse(ctx context.Context, req *protocol.Message, res *protocol.Message, err error) error {
|
|
|
- resData := new(comm_def.CommonMsg)
|
|
|
- serializer := p.getSerializeType(res)
|
|
|
+ serializer := share.Codecs[res.SerializeType()]
|
|
|
+ var resData g.Map
|
|
|
+ if serializerErr := serializer.Decode(res.Payload, &resData); serializerErr != nil {
|
|
|
+ return serializerErr
|
|
|
+ }
|
|
|
+
|
|
|
if err == nil {
|
|
|
// todo 正常数据处理
|
|
|
- if serializerErr := serializer.Decode(res.Payload, resData); serializerErr != nil {
|
|
|
- return serializerErr
|
|
|
- }
|
|
|
-
|
|
|
- if resData.Code == 0 {
|
|
|
- resData.Code = 200
|
|
|
+ if resData["code"] == 0 {
|
|
|
+ resData["code"] = 200
|
|
|
}
|
|
|
- if resData.Msg == "" {
|
|
|
- resData.Msg = "操作成功"
|
|
|
+ if resData["msg"] == "" {
|
|
|
+ resData["msg"] = "操作成功"
|
|
|
}
|
|
|
res.Payload, _ = serializer.Encode(resData)
|
|
|
return nil
|
|
|
@@ -130,24 +131,24 @@ func (p HandleErrorPlugin) PreWriteResponse(ctx context.Context, req *protocol.M
|
|
|
switch true {
|
|
|
case nextErrCode > 1000:
|
|
|
// 业务级异常处理
|
|
|
- resData.Code = int32(topCode)
|
|
|
- resData.Msg = err.Error()
|
|
|
+ resData["code"] = int32(topCode)
|
|
|
+ resData["msg"] = err.Error()
|
|
|
|
|
|
case nextErrCode == gcode.CodeValidationFailed.Code():
|
|
|
// goframe 数据校验
|
|
|
// 检查是否存在业务信息,添加显示
|
|
|
- resData.Code = int32(codeValidErr.Code())
|
|
|
- resData.Msg = codeValidErr.Message() + ": " + err.Error()
|
|
|
+ resData["code"] = int32(codeValidErr.Code())
|
|
|
+ resData["msg"] = codeValidErr.Message() + ": " + err.Error()
|
|
|
|
|
|
case nextErrCode > 0 && topCode > 1000:
|
|
|
// 检查是否存在业务信息,添加显示
|
|
|
- resData.Code = int32(topCode)
|
|
|
- resData.Msg = splitErrorStr(err.Error())
|
|
|
+ resData["code"] = int32(topCode)
|
|
|
+ resData["msg"] = splitErrorStr(err.Error())
|
|
|
|
|
|
default:
|
|
|
// 默认返回系统级异常处理
|
|
|
- resData.Code = int32(codeSysErr.Code())
|
|
|
- resData.Msg = codeSysErr.Message()
|
|
|
+ resData["code"] = int32(codeSysErr.Code())
|
|
|
+ resData["msg"] = codeSysErr.Message()
|
|
|
}
|
|
|
res.Payload, _ = serializer.Encode(resData)
|
|
|
res.SetMessageStatusType(protocol.Normal)
|