github.com/lingyao2333/mo-zero@v1.4.1/rest/internal/errcode/grpc_test.go (about)

     1  package errcode
     2  
     3  import (
     4  	"errors"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"google.golang.org/grpc/codes"
    10  	"google.golang.org/grpc/status"
    11  )
    12  
    13  func TestCodeFromGrpcError(t *testing.T) {
    14  	tests := []struct {
    15  		name string
    16  		code codes.Code
    17  		want int
    18  	}{
    19  		{
    20  			name: "OK",
    21  			code: codes.OK,
    22  			want: http.StatusOK,
    23  		},
    24  		{
    25  			name: "Invalid argument",
    26  			code: codes.InvalidArgument,
    27  			want: http.StatusBadRequest,
    28  		},
    29  		{
    30  			name: "Failed precondition",
    31  			code: codes.FailedPrecondition,
    32  			want: http.StatusBadRequest,
    33  		},
    34  		{
    35  			name: "Out of range",
    36  			code: codes.OutOfRange,
    37  			want: http.StatusBadRequest,
    38  		},
    39  		{
    40  			name: "Unauthorized",
    41  			code: codes.Unauthenticated,
    42  			want: http.StatusUnauthorized,
    43  		},
    44  		{
    45  			name: "Permission denied",
    46  			code: codes.PermissionDenied,
    47  			want: http.StatusForbidden,
    48  		},
    49  		{
    50  			name: "Not found",
    51  			code: codes.NotFound,
    52  			want: http.StatusNotFound,
    53  		},
    54  		{
    55  			name: "Canceled",
    56  			code: codes.Canceled,
    57  			want: http.StatusRequestTimeout,
    58  		},
    59  		{
    60  			name: "Already exists",
    61  			code: codes.AlreadyExists,
    62  			want: http.StatusConflict,
    63  		},
    64  		{
    65  			name: "Aborted",
    66  			code: codes.Aborted,
    67  			want: http.StatusConflict,
    68  		},
    69  		{
    70  			name: "Resource exhausted",
    71  			code: codes.ResourceExhausted,
    72  			want: http.StatusTooManyRequests,
    73  		},
    74  		{
    75  			name: "Internal",
    76  			code: codes.Internal,
    77  			want: http.StatusInternalServerError,
    78  		},
    79  		{
    80  			name: "Data loss",
    81  			code: codes.DataLoss,
    82  			want: http.StatusInternalServerError,
    83  		},
    84  		{
    85  			name: "Unknown",
    86  			code: codes.Unknown,
    87  			want: http.StatusInternalServerError,
    88  		},
    89  		{
    90  			name: "Unimplemented",
    91  			code: codes.Unimplemented,
    92  			want: http.StatusNotImplemented,
    93  		},
    94  		{
    95  			name: "Unavailable",
    96  			code: codes.Unavailable,
    97  			want: http.StatusServiceUnavailable,
    98  		},
    99  		{
   100  			name: "Deadline exceeded",
   101  			code: codes.DeadlineExceeded,
   102  			want: http.StatusGatewayTimeout,
   103  		},
   104  		{
   105  			name: "Beyond defined error",
   106  			code: codes.Code(^uint32(0)),
   107  			want: http.StatusInternalServerError,
   108  		},
   109  	}
   110  
   111  	for _, test := range tests {
   112  		test := test
   113  		t.Run(test.name, func(t *testing.T) {
   114  			assert.Equal(t, test.want, CodeFromGrpcError(status.Error(test.code, "foo")))
   115  		})
   116  	}
   117  }
   118  
   119  func TestIsGrpcError(t *testing.T) {
   120  	assert.True(t, IsGrpcError(status.Error(codes.Unknown, "foo")))
   121  	assert.False(t, IsGrpcError(errors.New("foo")))
   122  	assert.False(t, IsGrpcError(nil))
   123  }