| OLD | NEW |
| (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 } |
| OLD | NEW |