• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

bavix / gripmock / 17066567963

19 Aug 2025 10:15AM UTC coverage: 52.632% (-0.9%) from 53.531%
17066567963

push

github

web-flow
Merge pull request #653 from bavix/652-field-outputheaders-not-being-sent-as-trailers-if-outputcode-0

Fix Trailers

78 of 190 new or added lines in 8 files covered. (41.05%)

7 existing lines in 1 file now uncovered.

1590 of 3021 relevant lines covered (52.63%)

30.19 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

71.19
/internal/infra/grpccontext/interceptor.go
1
package grpccontext
2

3
import (
4
        "context"
5

6
        "github.com/rs/zerolog"
7
        "google.golang.org/grpc"
8
        "google.golang.org/grpc/metadata"
9
)
10

11
// PanicRecoveryUnaryInterceptor recovers from panics in unary gRPC handlers.
12
func PanicRecoveryUnaryInterceptor(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
69✔
13
        defer func() {
138✔
14
                if r := recover(); r != nil {
69✔
NEW
15
                        zerolog.Ctx(ctx).
×
NEW
16
                                Error().
×
NEW
17
                                Interface("panic", r).
×
NEW
18
                                Str("grpc.service", info.FullMethod).
×
NEW
19
                                Str("grpc.method_type", "unary").
×
NEW
20
                                Msg("Panic recovered in unary gRPC handler")
×
NEW
21
                }
×
22
        }()
23

24
        return handler(ctx, req)
69✔
25
}
26

27
// PanicRecoveryStreamInterceptor recovers from panics in streaming gRPC handlers.
28
func PanicRecoveryStreamInterceptor(srv any, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
118✔
29
        defer func() {
236✔
30
                if r := recover(); r != nil {
118✔
NEW
31
                        zerolog.Ctx(stream.Context()).
×
NEW
32
                                Error().
×
NEW
33
                                Interface("panic", r).
×
NEW
34
                                Str("grpc.service", info.FullMethod).
×
NEW
35
                                Str("grpc.method_type", "stream").
×
NEW
36
                                Msg("Panic recovered in streaming gRPC handler")
×
NEW
37
                }
×
38
        }()
39

40
        return handler(srv, stream)
118✔
41
}
42

43
// UnaryInterceptor is a gRPC interceptor that adds a logger to the context.
44
// The logger can be used to log messages related to the gRPC request.
45
//
46
// It takes a logger as a parameter and returns a grpc.UnaryServerInterceptor.
47
// The returned interceptor is used to intercept the gRPC unary requests.
48
func UnaryInterceptor(logger *zerolog.Logger) grpc.UnaryServerInterceptor {
1✔
49
        // The interceptor function is called for each gRPC unary request.
1✔
50
        // It takes the inner context, the request, the server info, and the handler.
1✔
51
        // It returns the response and an error.
1✔
52
        return func(
1✔
53
                innerCtx context.Context, // The context of the gRPC request.
1✔
54
                req any, // The request object.
1✔
55
                _ *grpc.UnaryServerInfo, // The server info.
1✔
56
                handler grpc.UnaryHandler, // The handler function for the request.
1✔
57
        ) (any, error) {
70✔
58
                // Add the logger to the context and call the handler.
69✔
59
                // The logger can be accessed using grpc.GetLogger(ctx).
69✔
60
                return handler(logger.WithContext(innerCtx), req)
69✔
61
        }
69✔
62
}
63

64
type serverStreamWrapper struct {
65
        ss  grpc.ServerStream
66
        ctx context.Context //nolint:containedctx
67
}
68

69
func (w serverStreamWrapper) Context() context.Context        { return w.ctx }
384✔
70
func (w serverStreamWrapper) RecvMsg(msg any) error           { return w.ss.RecvMsg(msg) }   //nolint:wrapcheck
274✔
71
func (w serverStreamWrapper) SendMsg(msg any) error           { return w.ss.SendMsg(msg) }   //nolint:wrapcheck
141✔
72
func (w serverStreamWrapper) SendHeader(md metadata.MD) error { return w.ss.SendHeader(md) } //nolint:wrapcheck
×
73
func (w serverStreamWrapper) SetHeader(md metadata.MD) error  { return w.ss.SetHeader(md) }  //nolint:wrapcheck
×
74
func (w serverStreamWrapper) SetTrailer(md metadata.MD)       { w.ss.SetTrailer(md) }
×
75

76
// StreamInterceptor is a gRPC interceptor that adds a logger to the context.
77
// The logger can be used to log messages related to the gRPC stream.
78
//
79
// It takes a logger as a parameter and returns a grpc.StreamServerInterceptor.
80
// The returned interceptor is used to intercept the gRPC stream requests.
81
func StreamInterceptor(logger *zerolog.Logger) grpc.StreamServerInterceptor {
1✔
82
        // The interceptor function is called for each gRPC stream request.
1✔
83
        // It takes the server, the stream, the server info, and the handler.
1✔
84
        // It returns an error.
1✔
85
        return func(
1✔
86
                srv any, // The server object.
1✔
87
                ss grpc.ServerStream, // The stream object.
1✔
88
                _ *grpc.StreamServerInfo, // The server info.
1✔
89
                handler grpc.StreamHandler, // The handler function for the stream.
1✔
90
        ) error {
119✔
91
                // Create a serverStreamWrapper object with the stream and context.
118✔
92
                // The context is created with the logger.
118✔
93
                return handler(srv, serverStreamWrapper{
118✔
94
                        ss:  ss,
118✔
95
                        ctx: logger.WithContext(ss.Context()),
118✔
96
                })
118✔
97
        }
118✔
98
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc