You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
916 B
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package rpcserver
import (
"context"
"github.com/pkg/errors"
"github.com/zeromicro/go-zero/core/logx"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"live-service/common/nerr"
)
// LoggerInterceptor grpc logger拦截器
// see github.com\zeromicro\go-zero\zrpc\server.go#AddUnaryInterceptors
// see google.golang.org\grpc\interceptor.go
func LoggerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
resp, err = handler(ctx, req)
if err != nil {
// 解析自定义err类型给予友好提示
cause := errors.Cause(err)
if e, ok := cause.(*nerr.Error); ok {
logx.WithContext(ctx).Errorf("[RPC-SRV-ERROR] %+v", err)
// 转换err
err = status.Error(codes.Code(e.Code), e.Msg)
} else {
logx.WithContext(ctx).Errorf("[RPC-SRV-ERROR] %+v", err)
}
}
return
}