...

Source file src/cloud.google.com/go/cloudsqlconn/errtype/errors_test.go

Documentation: cloud.google.com/go/cloudsqlconn/errtype

     1  // Copyright 2021 Google LLC
     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  //     https://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  package errtype_test
    16  
    17  import (
    18  	"errors"
    19  	"testing"
    20  
    21  	"cloud.google.com/go/cloudsqlconn/errtype"
    22  )
    23  
    24  func TestErrorFormatting(t *testing.T) {
    25  	tc := []struct {
    26  		desc string
    27  		err  error
    28  		want string
    29  	}{
    30  		{
    31  			desc: "config error message",
    32  			err:  errtype.NewConfigError("error message", "proj:reg:inst"),
    33  			want: "Config error: error message (connection name = \"proj:reg:inst\")",
    34  		},
    35  		{
    36  			desc: "refresh error message without internal error",
    37  			err:  errtype.NewRefreshError("error message", "proj:reg:inst", nil),
    38  			want: "Refresh error: error message (connection name = \"proj:reg:inst\")",
    39  		},
    40  		{
    41  			desc: "refresh error message with internal error",
    42  			err:  errtype.NewRefreshError("error message", "proj:reg:inst", errors.New("inner-error")),
    43  			want: "Refresh error: error message (connection name = \"proj:reg:inst\"): inner-error",
    44  		},
    45  		{
    46  			desc: "Dial error without inner error",
    47  			err: errtype.NewDialError(
    48  				"message",
    49  				"proj:reg:inst",
    50  				nil, // no error here
    51  			),
    52  			want: "Dial error: message (connection name = \"proj:reg:inst\")",
    53  		},
    54  		{
    55  			desc: "Dial error with inner error",
    56  			err: errtype.NewDialError(
    57  				"message",
    58  				"proj:reg:inst",
    59  				errors.New("inner-error"),
    60  			),
    61  			want: "Dial error: message (connection name = \"proj:reg:inst\"): inner-error",
    62  		},
    63  	}
    64  
    65  	for _, c := range tc {
    66  		if got := c.err.Error(); got != c.want {
    67  			t.Errorf("%v, got = %q, want = %q", c.desc, got, c.want)
    68  		}
    69  	}
    70  }
    71  

View as plain text