fix version 1
This commit is contained in:
@@ -5,11 +5,25 @@ import (
|
||||
pb "bsm/full/module/ec/address/pb"
|
||||
"context"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
gwRuntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/reflection"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
// recoverUnaryInterceptor 捕获处理过程中的 panic,转为 Internal 错误返回,避免进程崩溃。
|
||||
func recoverUnaryInterceptor(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
printer.Error("grpc panic recovered: %v", r)
|
||||
err = status.Error(codes.Internal, "internal server error")
|
||||
}
|
||||
}()
|
||||
return handler(ctx, req)
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
Grpc *grpc.Server
|
||||
Ctx context.Context
|
||||
@@ -20,7 +34,7 @@ type Server struct {
|
||||
func New(grpcServ *grpc.Server) *Server {
|
||||
standalone := grpcServ == nil
|
||||
if standalone {
|
||||
grpcServ = grpc.NewServer()
|
||||
grpcServ = grpc.NewServer(grpc.UnaryInterceptor(recoverUnaryInterceptor))
|
||||
}
|
||||
|
||||
srv := &Server{
|
||||
|
||||
Reference in New Issue
Block a user