mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-02 16:15:59 +08:00
code error
This commit is contained in:
@@ -6,13 +6,16 @@ import (
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/mw/specialerror"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/wrapperspb"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/errinfo"
|
||||
"github.com/pkg/errors"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/metadata"
|
||||
"google.golang.org/grpc/status"
|
||||
"math"
|
||||
"runtime"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const OperationID = "operationID"
|
||||
@@ -31,7 +34,22 @@ func rpcServerInterceptor(ctx context.Context, req interface{}, info *grpc.Unary
|
||||
if r := recover(); r != nil {
|
||||
log.ZError(ctx, "rpc panic", nil, "FullMethod", info.FullMethod, "type:", fmt.Sprintf("%T", r), "panic:", r)
|
||||
fmt.Println("stack info:", string(debug.Stack()))
|
||||
err = errs.ErrInternalServer
|
||||
pc, file, line, ok := runtime.Caller(4)
|
||||
if ok {
|
||||
panic("get runtime.Caller failed")
|
||||
}
|
||||
errInfo := &errinfo.ErrorInfo{
|
||||
Path: file,
|
||||
Line: uint32(line),
|
||||
Name: runtime.FuncForPC(pc).Name(),
|
||||
Cause: fmt.Sprintf("%s", r),
|
||||
Warp: nil,
|
||||
}
|
||||
sta, err := status.New(codes.Code(errs.ErrInternalServer.Code()), errs.ErrInternalServer.Msg()).WithDetails(errInfo)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err = sta.Err()
|
||||
}
|
||||
}()
|
||||
funcName := info.FullMethod
|
||||
@@ -68,14 +86,37 @@ func rpcServerInterceptor(ctx context.Context, req interface{}, info *grpc.Unary
|
||||
code = errs.ServerInternalError
|
||||
}
|
||||
grpcStatus := status.New(codes.Code(code), codeErr.Msg())
|
||||
var errInfo *errinfo.ErrorInfo
|
||||
if unwrap != err {
|
||||
stack := fmt.Sprintf("%+v", err)
|
||||
if details, err := grpcStatus.WithDetails(wrapperspb.String(stack)); err == nil {
|
||||
grpcStatus = details
|
||||
sti, ok := err.(interface{ StackTrace() errors.StackTrace })
|
||||
if ok {
|
||||
log.ZWarn(ctx, "rpc server resp", err, "funcName", funcName, "unwrap", unwrap.Error(), "stack", fmt.Sprintf("%+v", err))
|
||||
if fs := sti.StackTrace(); len(fs) > 0 {
|
||||
pc := uintptr(fs[0])
|
||||
fn := runtime.FuncForPC(pc)
|
||||
file, line := fn.FileLine(pc)
|
||||
errInfo = &errinfo.ErrorInfo{
|
||||
Path: file,
|
||||
Line: uint32(line),
|
||||
Name: fn.Name(),
|
||||
Cause: unwrap.Error(),
|
||||
Warp: nil,
|
||||
}
|
||||
if arr := strings.Split(err.Error(), ": "); len(arr) > 1 {
|
||||
errInfo.Warp = arr[:len(arr)-1]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if errInfo == nil {
|
||||
errInfo = &errinfo.ErrorInfo{Cause: err.Error()}
|
||||
}
|
||||
details, err := grpcStatus.WithDetails(errInfo)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log.ZError(ctx, "rpc server resp", err, "funcName", funcName)
|
||||
return nil, grpcStatus.Err()
|
||||
return nil, details.Err()
|
||||
}
|
||||
|
||||
func GrpcServer() grpc.ServerOption {
|
||||
|
||||
Reference in New Issue
Block a user