Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(770)

Side by Side Diff: grpc/grpcutil/paniccatcher.go

Issue 2945013002: grpc: Interceptor to catch panics and convert them to Internal errors. (Closed)
Patch Set: oops Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The LUCI Authors. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file.
4
5 package grpcutil
6
7 import (
8 "golang.org/x/net/context"
9 "google.golang.org/grpc"
10
11 "github.com/luci/luci-go/common/logging"
12 "github.com/luci/luci-go/common/runtime/paniccatcher"
13 )
14
15 // NewUnaryServerPanicCatcher returns a unary interceptor that catches panics
16 // in RPC handlers, recovers them and returns codes.Internal gRPC errors
17 // instead.
18 //
19 // It can be optionally chained with other interceptor.
20 func NewUnaryServerPanicCatcher(next grpc.UnaryServerInterceptor) grpc.UnaryServ erInterceptor {
21 return func(ctx context.Context, req interface{}, info *grpc.UnaryServer Info, handler grpc.UnaryHandler) (resp interface{}, err error) {
22 defer paniccatcher.Catch(func(p *paniccatcher.Panic) {
23 logging.Fields{
24 "panic.error": p.Reason,
25 }.Errorf(ctx, "Caught panic during handling of %q: %s\n% s", info.FullMethod, p.Reason, p.Stack)
26 err = Internal
27 })
28 if next != nil {
29 return next(ctx, req, info, handler)
30 }
31 return handler(ctx, req)
32 }
33 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698