go.uber.org/yarpc@v1.72.1/yarpcerrors/yarpcerrorclassifier_test.go (about)

     1  // Copyright (c) 2022 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package yarpcerrors
    22  
    23  import (
    24  	"github.com/stretchr/testify/assert"
    25  	"testing"
    26  )
    27  
    28  func TestGetFaultTypeFromErrorForClientErrors(t *testing.T) {
    29  	tests := []struct {
    30  		name string
    31  		err  error
    32  	}{
    33  		{
    34  			name: "invalid argument",
    35  			err:  InvalidArgumentErrorf("test"),
    36  		},
    37  		{
    38  			name: "cancelled",
    39  			err:  CancelledErrorf("test"),
    40  		},
    41  		{
    42  			name: "not found",
    43  			err:  NotFoundErrorf("test"),
    44  		},
    45  		{
    46  			name: "already exists",
    47  			err:  AlreadyExistsErrorf("test"),
    48  		},
    49  		{
    50  			name: "permission denied",
    51  			err:  PermissionDeniedErrorf("test"),
    52  		},
    53  		{
    54  			name: "resource exhausted",
    55  			err:  ResourceExhaustedErrorf("test"),
    56  		},
    57  		{
    58  			name: "failed precondition",
    59  			err:  FailedPreconditionErrorf("test"),
    60  		},
    61  		{
    62  			name: "aborted",
    63  			err:  AbortedErrorf("test"),
    64  		},
    65  		{
    66  			name: "out of range",
    67  			err:  OutOfRangeErrorf("test"),
    68  		},
    69  		{
    70  			name: "unimplemented",
    71  			err:  UnimplementedErrorf("test"),
    72  		},
    73  		{
    74  			name: "unauthenticated",
    75  			err:  UnauthenticatedErrorf("test"),
    76  		},
    77  	}
    78  	for _, tt := range tests {
    79  		t.Run(tt.name, func(t *testing.T) {
    80  			assert.Equal(t, ClientFault, GetFaultTypeFromError(tt.err))
    81  		})
    82  	}
    83  }
    84  
    85  func TestGetFaultTypeFromErrorForServerErrors(t *testing.T) {
    86  	tests := []struct {
    87  		name string
    88  		err  error
    89  	}{
    90  		{
    91  			name: "unknown",
    92  			err:  UnknownErrorf("test"),
    93  		},
    94  		{
    95  			name: "deadline exceeded",
    96  			err:  DeadlineExceededErrorf("test"),
    97  		},
    98  		{
    99  			name: "internal",
   100  			err:  InternalErrorf("test"),
   101  		},
   102  		{
   103  			name: "unavailable",
   104  			err:  UnavailableErrorf("test"),
   105  		},
   106  		{
   107  			name: "data loss",
   108  			err:  DataLossErrorf("test"),
   109  		},
   110  	}
   111  	for _, tt := range tests {
   112  		t.Run(tt.name, func(t *testing.T) {
   113  			assert.Equal(t, ServerFault, GetFaultTypeFromError(tt.err))
   114  		})
   115  	}
   116  }