|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package consumererror |
| 5 | + |
| 6 | +import ( |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +func TestNewPartialError(t *testing.T) { |
| 16 | + origErr := errors.New("translation failed") |
| 17 | + |
| 18 | + t.Run("basic", func(t *testing.T) { |
| 19 | + err := NewPartialError(origErr, 5) |
| 20 | + assert.ErrorIs(t, err, origErr) |
| 21 | + assert.Equal(t, "translation failed", err.Error()) |
| 22 | + |
| 23 | + pe := GetPartialError(err) |
| 24 | + require.NotNil(t, pe) |
| 25 | + assert.Equal(t, 5, pe.FailedCount()) |
| 26 | + }) |
| 27 | + |
| 28 | + t.Run("zero_count", func(t *testing.T) { |
| 29 | + err := NewPartialError(origErr, 0) |
| 30 | + pe := GetPartialError(err) |
| 31 | + require.NotNil(t, pe) |
| 32 | + assert.Equal(t, 0, pe.FailedCount()) |
| 33 | + }) |
| 34 | + |
| 35 | + t.Run("negative_count_becomes_zero", func(t *testing.T) { |
| 36 | + err := NewPartialError(origErr, -1) |
| 37 | + pe := GetPartialError(err) |
| 38 | + require.NotNil(t, pe) |
| 39 | + assert.Equal(t, 0, pe.FailedCount()) |
| 40 | + }) |
| 41 | +} |
| 42 | + |
| 43 | +func TestGetPartialError(t *testing.T) { |
| 44 | + t.Run("nil_error", func(t *testing.T) { |
| 45 | + pe := GetPartialError(nil) |
| 46 | + assert.Nil(t, pe) |
| 47 | + }) |
| 48 | + |
| 49 | + t.Run("non_partial_error", func(t *testing.T) { |
| 50 | + err := errors.New("regular error") |
| 51 | + pe := GetPartialError(err) |
| 52 | + assert.Nil(t, pe) |
| 53 | + }) |
| 54 | + |
| 55 | + t.Run("wrapped_partial_error", func(t *testing.T) { |
| 56 | + origErr := errors.New("inner error") |
| 57 | + partialErr := NewPartialError(origErr, 10) |
| 58 | + wrappedErr := fmt.Errorf("outer: %w", partialErr) |
| 59 | + |
| 60 | + pe := GetPartialError(wrappedErr) |
| 61 | + require.NotNil(t, pe) |
| 62 | + assert.Equal(t, 10, pe.FailedCount()) |
| 63 | + }) |
| 64 | +} |
| 65 | + |
| 66 | +func TestPartialError_Unwrap(t *testing.T) { |
| 67 | + var innerErr error = testErrorType{"testError"} |
| 68 | + |
| 69 | + partialErr := NewPartialError(innerErr, 7) |
| 70 | + require.NotNil(t, GetPartialError(partialErr)) |
| 71 | + |
| 72 | + // Verify the inner error can be extracted with errors.As |
| 73 | + target := testErrorType{} |
| 74 | + require.NotEqual(t, innerErr, target) |
| 75 | + |
| 76 | + isWrapped := errors.As(partialErr, &target) |
| 77 | + require.True(t, isWrapped) |
| 78 | + require.Equal(t, innerErr, target) |
| 79 | +} |
| 80 | + |
| 81 | +func TestPartialError_CombinedWithDownstream(t *testing.T) { |
| 82 | + origErr := errors.New("queue full") |
| 83 | + partialErr := NewPartialError(origErr, 30) |
| 84 | + downstreamErr := NewDownstream(partialErr) |
| 85 | + |
| 86 | + // Should be recognized as downstream error |
| 87 | + assert.True(t, IsDownstream(downstreamErr)) |
| 88 | + |
| 89 | + // Should still be able to extract the partial error |
| 90 | + pe := GetPartialError(downstreamErr) |
| 91 | + require.NotNil(t, pe) |
| 92 | + assert.Equal(t, 30, pe.FailedCount()) |
| 93 | + |
| 94 | + // Original error should still be accessible |
| 95 | + assert.ErrorIs(t, downstreamErr, origErr) |
| 96 | +} |
| 97 | + |
| 98 | +func TestPartialError_CombinedWithPermanent(t *testing.T) { |
| 99 | + origErr := errors.New("invalid format") |
| 100 | + partialErr := NewPartialError(origErr, 15) |
| 101 | + permanentErr := NewPermanent(partialErr) |
| 102 | + |
| 103 | + // Should be recognized as permanent error |
| 104 | + assert.True(t, IsPermanent(permanentErr)) |
| 105 | + |
| 106 | + // Should still be able to extract the partial error |
| 107 | + pe := GetPartialError(permanentErr) |
| 108 | + require.NotNil(t, pe) |
| 109 | + assert.Equal(t, 15, pe.FailedCount()) |
| 110 | + |
| 111 | + // Original error should still be accessible |
| 112 | + assert.ErrorIs(t, permanentErr, origErr) |
| 113 | +} |
| 114 | + |
| 115 | +func TestPartialError_DownstreamWrappingPartial(t *testing.T) { |
| 116 | + // Test the recommended pattern: downstream wrapping partial |
| 117 | + origErr := errors.New("pipeline refused") |
| 118 | + partialErr := NewPartialError(origErr, 20) |
| 119 | + combinedErr := NewDownstream(partialErr) |
| 120 | + |
| 121 | + // All checks should work |
| 122 | + assert.True(t, IsDownstream(combinedErr)) |
| 123 | + pe := GetPartialError(combinedErr) |
| 124 | + require.NotNil(t, pe) |
| 125 | + assert.Equal(t, 20, pe.FailedCount()) |
| 126 | + assert.ErrorIs(t, combinedErr, origErr) |
| 127 | +} |
0 commit comments