...

Source file src/github.com/go-kivik/kivik/v4/errors_test.go

Documentation: github.com/go-kivik/kivik/v4

     1  // Licensed under the Apache License, Version 2.0 (the "License"); you may not
     2  // use this file except in compliance with the License. You may obtain a copy of
     3  // the License at
     4  //
     5  //  http://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     9  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    10  // License for the specific language governing permissions and limitations under
    11  // the License.
    12  
    13  package kivik
    14  
    15  import (
    16  	"errors"
    17  	"fmt"
    18  	"testing"
    19  
    20  	internal "github.com/go-kivik/kivik/v4/int/errors"
    21  )
    22  
    23  func TestHTTPStatus(t *testing.T) {
    24  	type scTest struct {
    25  		Name     string
    26  		Err      error
    27  		Expected int
    28  	}
    29  	tests := []scTest{
    30  		{
    31  			Name:     "nil",
    32  			Expected: 0,
    33  		},
    34  		{
    35  			Name:     "Standard error",
    36  			Err:      errors.New("foo"),
    37  			Expected: 500,
    38  		},
    39  		{
    40  			Name:     "HTTPStatus",
    41  			Err:      &internal.Error{Status: 400, Err: errors.New("bad request")},
    42  			Expected: 400,
    43  		},
    44  		{
    45  			Name:     "wrapped HTTPStatus",
    46  			Err:      fmt.Errorf("foo: %w", &internal.Error{Status: 400, Err: errors.New("bad request")}),
    47  			Expected: 400,
    48  		},
    49  		{
    50  			Name: "deeply buried",
    51  			Err: func() error {
    52  				err := error(&internal.Error{Status: 400, Err: errors.New("bad request")})
    53  				err = fmt.Errorf("foo:%w", err)
    54  				err = fmt.Errorf("bar: %w", err)
    55  				err = fmt.Errorf("foo:%w", err)
    56  				err = fmt.Errorf("bar: %w", err)
    57  				err = fmt.Errorf("foo:%w", err)
    58  				err = fmt.Errorf("bar: %w", err)
    59  				return err
    60  			}(),
    61  			Expected: 400,
    62  		},
    63  	}
    64  	for _, test := range tests {
    65  		func(test scTest) {
    66  			t.Run(test.Name, func(t *testing.T) {
    67  				result := HTTPStatus(test.Err)
    68  				if result != test.Expected {
    69  					t.Errorf("Unexpected result. Expected %d, got %d", test.Expected, result)
    70  				}
    71  			})
    72  		}(test)
    73  	}
    74  }
    75  

View as plain text