github.com/blend/go-sdk@v1.20220411.3/grpcutil/server_recovery_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package grpcutil
     9  
    10  import (
    11  	"context"
    12  	"fmt"
    13  	"testing"
    14  
    15  	"google.golang.org/grpc"
    16  
    17  	"github.com/blend/go-sdk/assert"
    18  )
    19  
    20  func TestRecoverUnary(t *testing.T) {
    21  	assert := assert.New(t)
    22  
    23  	interceptor := RecoverServerUnary(WithServerRecoveryHandler(func(p interface{}) error {
    24  		return fmt.Errorf("panic: %v", p)
    25  	}))
    26  
    27  	_, err := interceptor(context.TODO(), nil, nil, func(ctx context.Context, req interface{}) (interface{}, error) {
    28  		panic("errored in handler")
    29  	})
    30  	assert.NotNil(err)
    31  	assert.Equal("panic: errored in handler", err.Error())
    32  }
    33  
    34  func TestRecoverStream(t *testing.T) {
    35  	assert := assert.New(t)
    36  
    37  	interceptor := RecoverServerStream(WithServerRecoveryHandler(func(p interface{}) error {
    38  		return fmt.Errorf("panic: %v", p)
    39  	}))
    40  
    41  	err := interceptor(nil, nil, nil, func(srv interface{}, stream grpc.ServerStream) error {
    42  		panic("errored in handler")
    43  	})
    44  	assert.NotNil(err)
    45  	assert.Equal("panic: errored in handler", err.Error())
    46  }