mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-26 11:36:44 +08:00
special error
This commit is contained in:
parent
0536a5fee2
commit
ce72365842
@ -20,6 +20,13 @@ import (
|
|||||||
const OperationID = "operationID"
|
const OperationID = "operationID"
|
||||||
const OpUserID = "opUserID"
|
const OpUserID = "opUserID"
|
||||||
|
|
||||||
|
func rpcString(v interface{}) string {
|
||||||
|
if s, ok := v.(interface{ String() string }); ok {
|
||||||
|
return s.String()
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%+v", v)
|
||||||
|
}
|
||||||
|
|
||||||
func rpcServerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
|
func rpcServerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
|
||||||
var operationID string
|
var operationID string
|
||||||
defer func() {
|
defer func() {
|
||||||
@ -42,39 +49,39 @@ func rpcServerInterceptor(ctx context.Context, req interface{}, info *grpc.Unary
|
|||||||
if opts := md.Get(OpUserID); len(opts) == 1 {
|
if opts := md.Get(OpUserID); len(opts) == 1 {
|
||||||
opUserID = opts[0]
|
opUserID = opts[0]
|
||||||
}
|
}
|
||||||
log.Info(OperationID, "opUserID", opUserID, "RPC", funcName, "Req", rpcString(req))
|
log.Info(operationID, "opUserID", opUserID, "RPC", funcName, "Req", rpcString(req))
|
||||||
ctx = tracelog.SetFuncInfos(ctx, funcName, operationID)
|
ctx = tracelog.SetFuncInfos(ctx, funcName, operationID)
|
||||||
ctx = context.WithValue(ctx, OperationID, operationID)
|
ctx = context.WithValue(ctx, OperationID, operationID)
|
||||||
ctx = context.WithValue(ctx, OpUserID, opUserID)
|
ctx = context.WithValue(ctx, OpUserID, opUserID)
|
||||||
resp, err = handler(ctx, req)
|
resp, err = handler(ctx, req)
|
||||||
if err != nil {
|
if err == nil {
|
||||||
log.Info(operationID, "rpc error:", err.Error())
|
log.Info(operationID, "opUserID", opUserID, "RPC", funcName, "Resp", rpcString(resp))
|
||||||
unwrap := errs.Unwrap(err)
|
return resp, nil
|
||||||
codeErr := specialerror.ErrCode(unwrap)
|
|
||||||
if codeErr == nil {
|
|
||||||
log.Error(operationID, "rpc InternalServer:", err.Error())
|
|
||||||
codeErr = errs.ErrInternalServer
|
|
||||||
}
|
|
||||||
if unwrap != err {
|
|
||||||
log.Info(operationID, "rpc error stack:", fmt.Sprintf("%+v", err))
|
|
||||||
}
|
|
||||||
code := codeErr.Code()
|
|
||||||
if code <= 0 || code > math.MaxUint32 {
|
|
||||||
log.Error(operationID, "rpc UnknownCode:", code, "err:", err.Error())
|
|
||||||
code = errs.UnknownCode
|
|
||||||
}
|
|
||||||
grpcStatus := status.New(codes.Code(code), codeErr.Msg())
|
|
||||||
if errs.Unwrap(err) != err {
|
|
||||||
stack := fmt.Sprintf("%+v", err)
|
|
||||||
log.Info(operationID, "rpc stack:", stack)
|
|
||||||
if details, err := grpcStatus.WithDetails(wrapperspb.String(stack)); err == nil {
|
|
||||||
grpcStatus = details
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil, grpcStatus.Err()
|
|
||||||
}
|
}
|
||||||
log.Info(OperationID, "opUserID", opUserID, "RPC", funcName, "Resp", rpcString(resp))
|
log.Info(operationID, "rpc error:", err.Error())
|
||||||
return resp, nil
|
unwrap := errs.Unwrap(err)
|
||||||
|
codeErr := specialerror.ErrCode(unwrap)
|
||||||
|
if codeErr == nil {
|
||||||
|
log.Error(operationID, "rpc InternalServer:", err.Error())
|
||||||
|
codeErr = errs.ErrInternalServer
|
||||||
|
}
|
||||||
|
var stack string
|
||||||
|
if unwrap != err {
|
||||||
|
stack = fmt.Sprintf("%+v", err)
|
||||||
|
log.Info(operationID, "rpc error stack:", stack)
|
||||||
|
}
|
||||||
|
code := codeErr.Code()
|
||||||
|
if code <= 0 || code > math.MaxUint32 {
|
||||||
|
log.Error(operationID, "rpc UnknownCode:", code, "err:", err.Error())
|
||||||
|
code = errs.ServerInternalError
|
||||||
|
}
|
||||||
|
grpcStatus := status.New(codes.Code(code), codeErr.Msg())
|
||||||
|
if errs.Unwrap(err) != err {
|
||||||
|
if details, err := grpcStatus.WithDetails(wrapperspb.String(stack)); err == nil {
|
||||||
|
grpcStatus = details
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, grpcStatus.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
func rpcClientInterceptor(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) (err error) {
|
func rpcClientInterceptor(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) (err error) {
|
||||||
@ -83,7 +90,6 @@ func rpcClientInterceptor(ctx context.Context, method string, req, reply interfa
|
|||||||
}
|
}
|
||||||
operationID, ok := ctx.Value(constant.OperationID).(string)
|
operationID, ok := ctx.Value(constant.OperationID).(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Error("1111", "ctx missing operationID")
|
|
||||||
return errs.ErrArgs.Wrap("ctx missing operationID")
|
return errs.ErrArgs.Wrap("ctx missing operationID")
|
||||||
}
|
}
|
||||||
md := metadata.Pairs(constant.OperationID, operationID)
|
md := metadata.Pairs(constant.OperationID, operationID)
|
||||||
@ -91,27 +97,25 @@ func rpcClientInterceptor(ctx context.Context, method string, req, reply interfa
|
|||||||
if ok {
|
if ok {
|
||||||
md.Append(constant.OpUserID, opUserID)
|
md.Append(constant.OpUserID, opUserID)
|
||||||
}
|
}
|
||||||
log.Info("", "rpc come here before")
|
log.Info(operationID, "OpUserID", "RPC", method, "Req", rpcString(req))
|
||||||
err = invoker(metadata.NewOutgoingContext(ctx, md), method, req, reply, cc, opts...)
|
err = invoker(metadata.NewOutgoingContext(ctx, md), method, req, reply, cc, opts...)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
log.Info(operationID, "Resp", rpcString(reply))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
log.Info("", "rpc come here err", err.Error())
|
log.Info(operationID, "rpc error:", err.Error())
|
||||||
|
|
||||||
rpcErr, ok := err.(interface{ GRPCStatus() *status.Status })
|
rpcErr, ok := err.(interface{ GRPCStatus() *status.Status })
|
||||||
if !ok {
|
if !ok {
|
||||||
return errs.ErrInternalServer.Wrap(err.Error())
|
return errs.ErrInternalServer.Wrap(err.Error())
|
||||||
}
|
}
|
||||||
sta := rpcErr.GRPCStatus()
|
sta := rpcErr.GRPCStatus()
|
||||||
if sta.Code() == 0 {
|
if sta.Code() == 0 {
|
||||||
return errs.NewCodeError(errs.DefaultOtherError, err.Error()).Wrap()
|
return errs.NewCodeError(errs.ServerInternalError, err.Error()).Wrap()
|
||||||
}
|
}
|
||||||
details := sta.Details()
|
if details := sta.Details(); len(details) > 0 {
|
||||||
if len(details) == 0 {
|
if v, ok := details[0].(*wrapperspb.StringValue); ok {
|
||||||
return errs.NewCodeError(int(sta.Code()), sta.Message()).Wrap()
|
return errs.NewCodeError(int(sta.Code()), sta.Message()).Wrap(v.String())
|
||||||
}
|
}
|
||||||
if v, ok := details[0].(*wrapperspb.StringValue); ok {
|
|
||||||
return errs.NewCodeError(int(sta.Code()), sta.Message()).Wrap(v.String())
|
|
||||||
}
|
}
|
||||||
return errs.NewCodeError(int(sta.Code()), sta.Message()).Wrap()
|
return errs.NewCodeError(int(sta.Code()), sta.Message()).Wrap()
|
||||||
}
|
}
|
||||||
|
@ -1,46 +0,0 @@
|
|||||||
package mw
|
|
||||||
|
|
||||||
import (
|
|
||||||
"OpenIM/pkg/errs"
|
|
||||||
"fmt"
|
|
||||||
"google.golang.org/grpc/codes"
|
|
||||||
"google.golang.org/grpc/status"
|
|
||||||
"google.golang.org/protobuf/types/known/wrapperspb"
|
|
||||||
"math"
|
|
||||||
)
|
|
||||||
|
|
||||||
func rpcString(v interface{}) string {
|
|
||||||
if s, ok := v.(interface{ String() string }); ok {
|
|
||||||
return s.String()
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("%+v", v)
|
|
||||||
}
|
|
||||||
|
|
||||||
func rpcErrorToCode(err error) *status.Status {
|
|
||||||
unwrap := errs.Unwrap(err)
|
|
||||||
var (
|
|
||||||
code codes.Code
|
|
||||||
msg string
|
|
||||||
)
|
|
||||||
if unwrap.(errs.CodeError) != nil {
|
|
||||||
c := unwrap.(errs.CodeError).Code()
|
|
||||||
if c <= 0 || c > math.MaxUint32 {
|
|
||||||
code = codes.OutOfRange // 错误码超出范围
|
|
||||||
} else {
|
|
||||||
code = codes.Code(c)
|
|
||||||
}
|
|
||||||
msg = unwrap.(errs.CodeError).Msg()
|
|
||||||
} else {
|
|
||||||
code = codes.Unknown
|
|
||||||
msg = unwrap.Error()
|
|
||||||
}
|
|
||||||
sta := status.New(code, msg)
|
|
||||||
if unwrap == err {
|
|
||||||
return sta
|
|
||||||
}
|
|
||||||
details, err := sta.WithDetails(wrapperspb.String(fmt.Sprintf("%+v", err)))
|
|
||||||
if err != nil {
|
|
||||||
return sta
|
|
||||||
}
|
|
||||||
return details
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user