|
| 1 | +package errors |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | +) |
| 10 | + |
| 11 | +func TestRetryError(t *testing.T) { |
| 12 | + t.Run("NewRetryError with nil error", func(t *testing.T) { |
| 13 | + retryErr := NewRetryError(nil) |
| 14 | + assert.NotNil(t, retryErr) |
| 15 | + assert.Nil(t, retryErr.Err) |
| 16 | + assert.Equal(t, "retry error", retryErr.Error()) |
| 17 | + }) |
| 18 | + |
| 19 | + t.Run("NewRetryError with an underlying error", func(t *testing.T) { |
| 20 | + innerErr := errors.New("something went wrong") |
| 21 | + retryErr := NewRetryError(innerErr) |
| 22 | + assert.NotNil(t, retryErr) |
| 23 | + assert.Equal(t, innerErr, retryErr.Err) |
| 24 | + assert.Equal(t, "[retry]: something went wrong", retryErr.Error()) |
| 25 | + }) |
| 26 | + |
| 27 | + t.Run("IsRetryError checks", func(t *testing.T) { |
| 28 | + directRetryErr := NewRetryError(errors.New("direct")) |
| 29 | + wrappedRetryErr := fmt.Errorf("wrapped: %w", directRetryErr) |
| 30 | + otherErr := errors.New("not a retry error") |
| 31 | + |
| 32 | + assert.True(t, IsRetryError(directRetryErr)) |
| 33 | + assert.True(t, IsRetryError(wrappedRetryErr)) |
| 34 | + assert.False(t, IsRetryError(otherErr)) |
| 35 | + }) |
| 36 | + |
| 37 | + t.Run("AsRetryError checks", func(t *testing.T) { |
| 38 | + directRetryErr := NewRetryError(errors.New("direct")) |
| 39 | + wrappedRetryErr := fmt.Errorf("wrapped: %w", directRetryErr) |
| 40 | + |
| 41 | + var target *RetryError |
| 42 | + assert.True(t, AsRetryError(wrappedRetryErr, &target)) |
| 43 | + assert.Equal(t, directRetryErr, target) |
| 44 | + }) |
| 45 | +} |
| 46 | + |
| 47 | +func TestBadRequestError(t *testing.T) { |
| 48 | + t.Run("NewBadRequestError with nil error", func(t *testing.T) { |
| 49 | + badReqErr := NewBadRequestError(nil) |
| 50 | + assert.NotNil(t, badReqErr) |
| 51 | + assert.Nil(t, badReqErr.Err) |
| 52 | + assert.Equal(t, "fatal error", badReqErr.Error()) |
| 53 | + }) |
| 54 | + |
| 55 | + t.Run("NewBadRequestError with an underlying error", func(t *testing.T) { |
| 56 | + innerErr := errors.New("invalid input") |
| 57 | + badReqErr := NewBadRequestError(innerErr) |
| 58 | + assert.NotNil(t, badReqErr) |
| 59 | + assert.Equal(t, innerErr, badReqErr.Err) |
| 60 | + assert.Equal(t, "invalid input", badReqErr.Error()) |
| 61 | + }) |
| 62 | + |
| 63 | + t.Run("IsBadRequest checks", func(t *testing.T) { |
| 64 | + directBadReqErr := NewBadRequestError(errors.New("direct")) |
| 65 | + wrappedBadReqErr := fmt.Errorf("wrapped: %w", directBadReqErr) |
| 66 | + otherErr := errors.New("not a bad request error") |
| 67 | + |
| 68 | + assert.True(t, IsBadRequest(directBadReqErr)) |
| 69 | + assert.True(t, IsBadRequest(wrappedBadReqErr)) |
| 70 | + assert.False(t, IsBadRequest(otherErr)) |
| 71 | + }) |
| 72 | + |
| 73 | + t.Run("AsBadRequest checks", func(t *testing.T) { |
| 74 | + directBadReqErr := NewBadRequestError(errors.New("direct")) |
| 75 | + wrappedBadReqErr := fmt.Errorf("wrapped: %w", directBadReqErr) |
| 76 | + |
| 77 | + var target *BadRequestError |
| 78 | + assert.True(t, AsBadRequest(wrappedBadReqErr, &target)) |
| 79 | + assert.Equal(t, directBadReqErr, target) |
| 80 | + }) |
| 81 | +} |
0 commit comments