github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/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 "github.com/hxx258456/ccgo/go-grpc-middleware" 8 grpc_recovery "github.com/hxx258456/ccgo/go-grpc-middleware/recovery" 9 "github.com/hxx258456/ccgo/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 }