go.uber.org/yarpc@v1.72.1/transport/tchannel/error_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 tchannel
    22  
    23  import (
    24  	"context"
    25  	"errors"
    26  	"testing"
    27  
    28  	"github.com/stretchr/testify/assert"
    29  	"github.com/uber/tchannel-go"
    30  	"go.uber.org/yarpc/api/transport"
    31  	"go.uber.org/yarpc/yarpcerrors"
    32  )
    33  
    34  func TestToYARPCError(t *testing.T) {
    35  	tests := []struct {
    36  		name    string
    37  		giveErr error
    38  		giveReq *transport.Request
    39  		wantErr error
    40  	}{
    41  		{
    42  			name:    "nil",
    43  			giveErr: nil,
    44  			wantErr: nil,
    45  		},
    46  		{
    47  			name:    "yarpcerror",
    48  			giveErr: yarpcerrors.InvalidArgumentErrorf("test"),
    49  			wantErr: yarpcerrors.InvalidArgumentErrorf("test"),
    50  		},
    51  		{
    52  			name:    "tchannel error",
    53  			giveErr: tchannel.NewSystemError(tchannel.ErrCodeBadRequest, "test"),
    54  			wantErr: fromSystemError(tchannel.NewSystemError(tchannel.ErrCodeBadRequest, "test").(tchannel.SystemError)),
    55  		},
    56  		{
    57  			name:    "deadline exceeded",
    58  			giveErr: context.DeadlineExceeded,
    59  			giveReq: &transport.Request{Service: "serv", Procedure: "proc"},
    60  			wantErr: yarpcerrors.DeadlineExceededErrorf("deadline exceeded for service: %q, procedure: %q", "serv", "proc"),
    61  		},
    62  		{
    63  			name:    "unknown",
    64  			giveErr: errors.New("test"),
    65  			giveReq: &transport.Request{Service: "serv", Procedure: "proc"},
    66  			wantErr: yarpcerrors.UnknownErrorf("received unknown error calling service: %q, procedure: %q, err: %s", "serv", "proc", "test"),
    67  		},
    68  	}
    69  	for _, tt := range tests {
    70  		t.Run(tt.name, func(t *testing.T) {
    71  			gotErr := toYARPCError(tt.giveReq, tt.giveErr)
    72  			assert.Equal(t, tt.wantErr, gotErr)
    73  		})
    74  	}
    75  }
    76  
    77  func TestGetResponseErrorMeta(t *testing.T) {
    78  	tests := []struct {
    79  		name string
    80  		give error
    81  		want *ResponseErrorMeta
    82  	}{
    83  		{
    84  			name: "nil",
    85  		},
    86  		{
    87  			name: "wrong error",
    88  			give: errors.New("not a yarpc/tchannel error"),
    89  		},
    90  		{
    91  			name: "success",
    92  			give: fromSystemError(tchannel.NewSystemError(tchannel.ErrCodeProtocol, "foo bar").(tchannel.SystemError)),
    93  			want: &ResponseErrorMeta{
    94  				Code: tchannel.ErrCodeProtocol,
    95  			},
    96  		},
    97  	}
    98  
    99  	for _, tt := range tests {
   100  		t.Run(tt.name, func(t *testing.T) {
   101  			assert.Equal(t, tt.want, GetResponseErrorMeta(tt.give), "unexpected")
   102  		})
   103  	}
   104  }