github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/x/errors/errors_test.go (about)

     1  // Copyright (c) 2016 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 errors
    22  
    23  import (
    24  	"errors"
    25  	"fmt"
    26  	"testing"
    27  
    28  	"github.com/stretchr/testify/assert"
    29  	"github.com/stretchr/testify/require"
    30  )
    31  
    32  func TestWrap(t *testing.T) {
    33  	inner := errors.New("detailed error message")
    34  	err := NewInvalidParamsError(inner)
    35  	wrappedErr := Wrap(err, "context about params error")
    36  	assert.Error(t, wrappedErr)
    37  	assert.Equal(t, "context about params error: detailed error message", wrappedErr.Error())
    38  	assert.True(t, IsInvalidParams(wrappedErr))
    39  
    40  	err = NewResourceExhaustedError(inner)
    41  	wrappedErr = Wrap(err, "context about resource exhausted error")
    42  	assert.Error(t, wrappedErr)
    43  	assert.Equal(t, "context about resource exhausted error: detailed error message", wrappedErr.Error())
    44  	assert.True(t, IsResourceExhausted(wrappedErr))
    45  
    46  	err = NewRetryableError(inner)
    47  	wrappedErr = Wrap(err, "context about retryable error")
    48  	assert.Error(t, wrappedErr)
    49  	assert.Equal(t, "context about retryable error: detailed error message", wrappedErr.Error())
    50  	assert.True(t, IsRetryableError(wrappedErr))
    51  
    52  	err = NewNonRetryableError(inner)
    53  	wrappedErr = Wrap(err, "context about nonretryable error")
    54  	assert.Error(t, wrappedErr)
    55  	assert.Equal(t, "context about nonretryable error: detailed error message", wrappedErr.Error())
    56  	assert.True(t, IsNonRetryableError(wrappedErr))
    57  }
    58  
    59  func TestWrapf(t *testing.T) {
    60  	inner := errors.New("detailed error message")
    61  	err := NewInvalidParamsError(inner)
    62  	wrappedErr := Wrapf(err, "context about %s error", "params")
    63  	assert.Error(t, wrappedErr)
    64  	assert.Equal(t, "context about params error: detailed error message", wrappedErr.Error())
    65  	assert.True(t, IsInvalidParams(wrappedErr))
    66  
    67  	err = NewResourceExhaustedError(inner)
    68  	wrappedErr = Wrapf(err, "context about %s error", "resource exhausted")
    69  	assert.Error(t, wrappedErr)
    70  	assert.Equal(t, "context about resource exhausted error: detailed error message", wrappedErr.Error())
    71  	assert.True(t, IsResourceExhausted(wrappedErr))
    72  
    73  	err = NewRetryableError(inner)
    74  	wrappedErr = Wrapf(err, "context about %s error", "retryable")
    75  	assert.Error(t, wrappedErr)
    76  	assert.Equal(t, "context about retryable error: detailed error message", wrappedErr.Error())
    77  	assert.True(t, IsRetryableError(wrappedErr))
    78  
    79  	err = NewNonRetryableError(inner)
    80  	wrappedErr = Wrapf(err, "context about %s error", "nonretryable")
    81  	assert.Error(t, wrappedErr)
    82  	assert.Equal(t, "context about nonretryable error: detailed error message", wrappedErr.Error())
    83  	assert.True(t, IsNonRetryableError(wrappedErr))
    84  }
    85  
    86  func TestMultiErrorNoError(t *testing.T) {
    87  	err := NewMultiError()
    88  	require.Nil(t, err.FinalError())
    89  	require.Nil(t, err.LastError())
    90  	require.Equal(t, "", err.Error())
    91  	require.True(t, err.Empty())
    92  	require.Equal(t, 0, err.NumErrors())
    93  }
    94  
    95  func TestMultiErrorOneError(t *testing.T) {
    96  	err := NewMultiError()
    97  	err = err.Add(errors.New("foo"))
    98  	final := err.FinalError()
    99  	require.NotNil(t, final)
   100  	require.Equal(t, "foo", final.Error())
   101  	last := err.LastError()
   102  	require.NotNil(t, last)
   103  	require.Equal(t, "foo", last.Error())
   104  	require.False(t, err.Empty())
   105  	require.Equal(t, 1, err.NumErrors())
   106  }
   107  
   108  func TestMultiErrorMultipleErrors(t *testing.T) {
   109  	err := NewMultiError()
   110  	for _, errMsg := range []string{"foo", "bar", "baz"} {
   111  		err = err.Add(errors.New(errMsg))
   112  	}
   113  	err = err.Add(nil)
   114  	final := err.FinalError()
   115  	require.NotNil(t, final)
   116  	require.Equal(t, final.Error(), "foo\nbar\nbaz")
   117  	last := err.LastError()
   118  	require.NotNil(t, last)
   119  	require.Equal(t, last.Error(), "baz")
   120  	require.False(t, err.Empty())
   121  	require.Equal(t, 3, err.NumErrors())
   122  	require.Equal(t, 3, len(err.Errors()))
   123  }
   124  
   125  func TestErrorsIsAnErrorAndFormatsErrors(t *testing.T) {
   126  	errs := error(Errors{
   127  		fmt.Errorf("some error: foo=2, bar=baz"),
   128  		fmt.Errorf("some other error: foo=42, bar=qux"),
   129  	})
   130  	assert.Equal(t, "[<some error: foo=2, bar=baz>, "+
   131  		"<some other error: foo=42, bar=qux>]", errs.Error())
   132  }