gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/go-grpc-middleware/recovery/examples_test.go (about)

     1  // Copyright 2017 David Ackroyd. All Rights Reserved.
     2  // See LICENSE for licensing terms.
     3  
     4  package grpc_recovery_test
     5  
     6  import (
     7  	grpc_middleware "gitee.com/ks-custle/core-gm/go-grpc-middleware"
     8  	grpc_recovery "gitee.com/ks-custle/core-gm/go-grpc-middleware/recovery"
     9  	"gitee.com/ks-custle/core-gm/grpc"
    10  )
    11  
    12  var (
    13  	customFunc grpc_recovery.RecoveryHandlerFunc
    14  )
    15  
    16  // Initialization shows an initialization sequence with a custom recovery handler func.
    17  func Example_initialization() {
    18  	// Shared options for the logger, with a custom gRPC code to log level function.
    19  	opts := []grpc_recovery.Option{
    20  		grpc_recovery.WithRecoveryHandler(customFunc),
    21  	}
    22  	// Create a server. Recovery handlers should typically be last in the chain so that other middleware
    23  	// (e.g. logging) can operate on the recovered state instead of being directly affected by any panic
    24  	_ = grpc.NewServer(
    25  		grpc_middleware.WithUnaryServerChain(
    26  			grpc_recovery.UnaryServerInterceptor(opts...),
    27  		),
    28  		grpc_middleware.WithStreamServerChain(
    29  			grpc_recovery.StreamServerInterceptor(opts...),
    30  		),
    31  	)
    32  }