2021-07-05 10:58:58 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/getsentry/sentry-go"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/codes"
|
2021-07-13 15:42:41 +02:00
|
|
|
"google.golang.org/grpc/status"
|
2021-07-05 10:58:58 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func SentryHandler() grpc.UnaryServerInterceptor {
|
|
|
|
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
|
|
|
|
return sendErrToSentry(ctx, req, handler)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendErrToSentry(ctx context.Context, req interface{}, handler grpc.UnaryHandler) (interface{}, error) {
|
|
|
|
resp, err := handler(ctx, req)
|
2021-07-13 15:42:41 +02:00
|
|
|
code := status.Code(err)
|
|
|
|
switch code {
|
|
|
|
case codes.Canceled,
|
|
|
|
codes.Unknown,
|
|
|
|
codes.DeadlineExceeded,
|
|
|
|
codes.ResourceExhausted,
|
|
|
|
codes.Aborted,
|
|
|
|
codes.Unimplemented,
|
|
|
|
codes.Internal,
|
|
|
|
codes.Unavailable,
|
|
|
|
codes.DataLoss:
|
2021-07-05 10:58:58 +02:00
|
|
|
sentry.CaptureException(err)
|
|
|
|
}
|
|
|
|
return resp, err
|
|
|
|
}
|