crashinterceptor.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package serverinterceptors
  2. import (
  3. "context"
  4. "runtime/debug"
  5. "git.i2edu.net/i2/go-zero/core/logx"
  6. "google.golang.org/grpc"
  7. "google.golang.org/grpc/codes"
  8. "google.golang.org/grpc/status"
  9. )
  10. // StreamCrashInterceptor catches panics in processing stream requests and recovers.
  11. func StreamCrashInterceptor(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo,
  12. handler grpc.StreamHandler) (err error) {
  13. defer handleCrash(func(r interface{}) {
  14. err = toPanicError(r)
  15. })
  16. return handler(srv, stream)
  17. }
  18. // UnaryCrashInterceptor catches panics in processing unary requests and recovers.
  19. func UnaryCrashInterceptor() grpc.UnaryServerInterceptor {
  20. return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
  21. handler grpc.UnaryHandler) (resp interface{}, err error) {
  22. defer handleCrash(func(r interface{}) {
  23. err = toPanicError(r)
  24. })
  25. return handler(ctx, req)
  26. }
  27. }
  28. func handleCrash(handler func(interface{})) {
  29. if r := recover(); r != nil {
  30. handler(r)
  31. }
  32. }
  33. func toPanicError(r interface{}) error {
  34. logx.Errorf("%+v %s", r, debug.Stack())
  35. return status.Errorf(codes.Internal, "panic: %v", r)
  36. }