-
Notifications
You must be signed in to change notification settings - Fork 703
/
Copy pathexamples_test.go
40 lines (35 loc) · 1.21 KB
/
examples_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Copyright (c) The go-grpc-middleware Authors.
// Licensed under the Apache License 2.0.
// Copyright 2017 David Ackroyd. All Rights Reserved.
// See LICENSE for licensing terms.
package recovery_test
import (
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/recovery"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
var (
customFunc recovery.RecoveryHandlerFunc
)
// Initialization shows an initialization sequence with a custom recovery handler func.
func Example_initialization() {
// Define customfunc to handle panic
customFunc = func(p any) (err error) {
return status.Errorf(codes.Unknown, "panic triggered: %v", p)
}
// Shared options for the logger, with a custom gRPC code to log level function.
opts := []recovery.Option{
recovery.WithRecoveryHandler(customFunc),
}
// Create a server. Recovery handlers should typically be last in the chain so that other middleware
// (e.g. logging) can operate on the recovered state instead of being directly affected by any panic
_ = grpc.NewServer(
grpc.UnaryInterceptor(
recovery.UnaryServerInterceptor(opts...),
),
grpc.StreamInterceptor(
recovery.StreamServerInterceptor(opts...),
),
)
}