mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-06 04:15:46 +08:00
special error
This commit is contained in:
parent
8dd2d3054f
commit
c3b109ad4a
@ -3,6 +3,7 @@ package mw
|
||||
import (
|
||||
"OpenIM/pkg/common/constant"
|
||||
"OpenIM/pkg/common/log"
|
||||
"OpenIM/pkg/common/mw/specialerror"
|
||||
"OpenIM/pkg/common/tracelog"
|
||||
"OpenIM/pkg/errs"
|
||||
"context"
|
||||
@ -12,6 +13,7 @@ import (
|
||||
"google.golang.org/grpc/metadata"
|
||||
"google.golang.org/grpc/status"
|
||||
"google.golang.org/protobuf/types/known/wrapperspb"
|
||||
"math"
|
||||
"runtime/debug"
|
||||
)
|
||||
|
||||
@ -40,18 +42,39 @@ func rpcServerInterceptor(ctx context.Context, req interface{}, info *grpc.Unary
|
||||
if opts := md.Get(OpUserID); len(opts) == 1 {
|
||||
opUserID = opts[0]
|
||||
}
|
||||
log.Info(OperationID, "opUserID", opUserID, "RPC", funcName, "Req", rpcString(req))
|
||||
ctx = tracelog.SetFuncInfos(ctx, funcName, operationID)
|
||||
ctx = context.WithValue(ctx, OperationID, operationID)
|
||||
ctx = context.WithValue(ctx, OpUserID, opUserID)
|
||||
tracelog.SetCtxInfo(ctx, funcName, err, "opUserID", opUserID, "rpcReq", rpcString(req))
|
||||
resp, err = handler(ctx, req)
|
||||
if err != nil {
|
||||
tracelog.SetCtxInfo(ctx, funcName, err)
|
||||
log.Info("", "rpc come here,in rpc call,err:", err.Error())
|
||||
return nil, rpcErrorToCode(err).Err()
|
||||
log.Info(operationID, "rpc error:", err.Error())
|
||||
unwrap := errs.Unwrap(err)
|
||||
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()
|
||||
}
|
||||
tracelog.SetCtxInfo(ctx, funcName, nil, "rpcResp", rpcString(resp))
|
||||
return
|
||||
log.Info(OperationID, "opUserID", opUserID, "RPC", funcName, "Resp", rpcString(resp))
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func rpcClientInterceptor(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) (err error) {
|
||||
@ -77,7 +100,7 @@ func rpcClientInterceptor(ctx context.Context, method string, req, reply interfa
|
||||
|
||||
rpcErr, ok := err.(interface{ GRPCStatus() *status.Status })
|
||||
if !ok {
|
||||
return errs.NewCodeError(errs.DefaultOtherError, err.Error()).Wrap()
|
||||
return errs.ErrInternalServer.Wrap(err.Error())
|
||||
}
|
||||
sta := rpcErr.GRPCStatus()
|
||||
if sta.Code() == 0 {
|
||||
|
33
pkg/common/mw/specialerror/error.go
Normal file
33
pkg/common/mw/specialerror/error.go
Normal file
@ -0,0 +1,33 @@
|
||||
package specialerror
|
||||
|
||||
import "OpenIM/pkg/errs"
|
||||
|
||||
var handlers []func(err error) errs.CodeError
|
||||
|
||||
func AddErrHandler(h func(err error) errs.CodeError) {
|
||||
if h == nil {
|
||||
panic("nil handler")
|
||||
}
|
||||
handlers = append(handlers, h)
|
||||
}
|
||||
|
||||
func AddReplace(target error, codeErr errs.CodeError) {
|
||||
AddErrHandler(func(err error) errs.CodeError {
|
||||
if err == target {
|
||||
return codeErr
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func ErrCode(err error) errs.CodeError {
|
||||
if codeErr, ok := err.(errs.CodeError); ok {
|
||||
return codeErr
|
||||
}
|
||||
for i := 0; i < len(handlers); i++ {
|
||||
if codeErr := handlers[i](err); codeErr != nil {
|
||||
return codeErr
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user