Skip to content

Commit

Permalink
Return error to caller
Browse files Browse the repository at this point in the history
Signed-off-by: Jonathan Marcantonio <[email protected]>
  • Loading branch information
lennysgarage committed Jul 18, 2024
1 parent 2964149 commit 86e99d1
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions internal/server/middleware/recovery.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
package middleware

import (
"context"
"fmt"
"runtime"
"time"

"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/middleware/recovery"

"google.golang.org/grpc"
)

func StreamRecoveryInterceptor(logger log.Logger) grpc.StreamServerInterceptor {
return func(srv any, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
wrapper := &requestInterceptingWrapper{ServerStream: ss}
type Option func(*options)

type options struct { // Duplicated from https://github.com/go-kratos/kratos/blob/main/middleware/recovery/recovery.go b/c no export
handler recovery.HandlerFunc
}

func StreamRecoveryInterceptor(logger log.Logger, opts ...Option) grpc.StreamServerInterceptor {
op := options{
handler: func(ctx context.Context, req, err interface{}) error {
return recovery.ErrUnknownRequest
},
}
for _, o := range opts {
o(&op)
}
return func(srv any, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) (err error) {
startTime := time.Now()
wrapper := &requestInterceptingWrapper{ServerStream: ss}
ctx := ss.Context()

defer func() {
Expand All @@ -27,7 +43,7 @@ func StreamRecoveryInterceptor(logger log.Logger) grpc.StreamServerInterceptor {
"latency", time.Since(startTime).Seconds(),
"reason", fmt.Sprintf("%v: %+v\n%s\n", rerr, wrapper.req, buf),
)
// fail silently?
err = op.handler(ctx, wrapper.req, rerr)
}
}()
return handler(srv, wrapper)
Expand Down

0 comments on commit 86e99d1

Please sign in to comment.