github.com/cs3org/reva/v2@v2.27.7/internal/grpc/interceptors/recovery/recovery.go (about)

     1  // Copyright 2018-2021 CERN
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package recovery
    20  
    21  import (
    22  	"context"
    23  
    24  	"runtime/debug"
    25  
    26  	"github.com/cs3org/reva/v2/pkg/appctx"
    27  	grpc_recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
    28  	"google.golang.org/grpc"
    29  	"google.golang.org/grpc/codes"
    30  	"google.golang.org/grpc/status"
    31  )
    32  
    33  // NewUnary returns a server interceptor that adds telemetry to
    34  // grpc calls.
    35  func NewUnary() grpc.UnaryServerInterceptor {
    36  	interceptor := grpc_recovery.UnaryServerInterceptor(grpc_recovery.WithRecoveryHandlerContext(recoveryFunc))
    37  	return interceptor
    38  }
    39  
    40  // NewStream returns a streaming server interceptor that adds telemetry to
    41  // streaming grpc calls.
    42  func NewStream() grpc.StreamServerInterceptor {
    43  	interceptor := grpc_recovery.StreamServerInterceptor(grpc_recovery.WithRecoveryHandlerContext(recoveryFunc))
    44  	return interceptor
    45  }
    46  
    47  func recoveryFunc(ctx context.Context, p interface{}) (err error) {
    48  	debug.PrintStack()
    49  	log := appctx.GetLogger(ctx)
    50  	log.Error().Msgf("%+v; stack: %s", p, debug.Stack())
    51  	return status.Errorf(codes.Internal, "%s", p)
    52  }