gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/go-grpc-middleware/recovery/options.go (about) 1 // Copyright 2017 David Ackroyd. All Rights Reserved. 2 // See LICENSE for licensing terms. 3 4 package grpc_recovery 5 6 import "gitee.com/ks-custle/core-gm/net/context" 7 8 var ( 9 defaultOptions = &options{ 10 recoveryHandlerFunc: nil, 11 } 12 ) 13 14 type options struct { 15 recoveryHandlerFunc RecoveryHandlerFuncContext 16 } 17 18 func evaluateOptions(opts []Option) *options { 19 optCopy := &options{} 20 *optCopy = *defaultOptions 21 for _, o := range opts { 22 o(optCopy) 23 } 24 return optCopy 25 } 26 27 type Option func(*options) 28 29 // WithRecoveryHandler customizes the function for recovering from a panic. 30 func WithRecoveryHandler(f RecoveryHandlerFunc) Option { 31 return func(o *options) { 32 o.recoveryHandlerFunc = RecoveryHandlerFuncContext(func(ctx context.Context, p interface{}) error { 33 return f(p) 34 }) 35 } 36 } 37 38 // WithRecoveryHandlerContext customizes the function for recovering from a panic. 39 func WithRecoveryHandlerContext(f RecoveryHandlerFuncContext) Option { 40 return func(o *options) { 41 o.recoveryHandlerFunc = f 42 } 43 }