|
1 | 1 | // Copyright The OpenTelemetry Authors |
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | 3 |
|
4 | | -package xconsumererror |
| 4 | +package xconsumererror_test |
5 | 5 |
|
6 | 6 | import ( |
7 | 7 | "errors" |
| 8 | + "fmt" |
8 | 9 | "testing" |
9 | 10 |
|
10 | 11 | "github.com/stretchr/testify/assert" |
11 | 12 | "go.opentelemetry.io/collector/consumer/consumererror" |
| 13 | + "go.opentelemetry.io/collector/consumer/consumererror/xconsumererror" |
12 | 14 | ) |
13 | 15 |
|
14 | 16 | func TestPartial(t *testing.T) { |
15 | 17 | internalErr := errors.New("some points failed") |
16 | | - err := NewPartial(internalErr, 5) |
| 18 | + err := xconsumererror.NewPartial(internalErr, 5) |
17 | 19 | assert.True(t, consumererror.IsPermanent(err)) |
18 | | - partialErr, ok := AsPartial(err) |
| 20 | + partialErr, ok := xconsumererror.AsPartial(err) |
19 | 21 | assert.True(t, ok) |
20 | 22 | assert.Equal(t, 5, partialErr.Failed()) |
21 | 23 | assert.Equal(t, internalErr, partialErr.Unwrap()) |
22 | 24 | assert.Equal(t, internalErr.Error(), partialErr.Error()) |
23 | 25 | } |
| 26 | + |
| 27 | +func ExampleNewPartial() { |
| 28 | + // Produce a partial error. |
| 29 | + failedItems := 5 |
| 30 | + consumptionErr := errors.New("some points failed to be written") |
| 31 | + err := xconsumererror.NewPartial(consumptionErr, failedItems) |
| 32 | + fmt.Println(err) |
| 33 | + |
| 34 | + // Output: "Permanent error: some points failed to be written" |
| 35 | +} |
| 36 | + |
| 37 | +func ExampleAsPartial() { |
| 38 | + // Produce a partial error. |
| 39 | + partialErr := xconsumererror.NewPartial(errors.New("some points failed to be written"), 10) |
| 40 | + |
| 41 | + // The result of `xconsumererror.AsPartial` has a method `Failed` |
| 42 | + // which can be used to retrieve the failed item count. |
| 43 | + if err, ok := xconsumererror.AsPartial(partialErr); ok { |
| 44 | + fmt.Println(err.Failed()) |
| 45 | + } |
| 46 | + |
| 47 | + // Output: 10 |
| 48 | +} |
| 49 | + |
| 50 | +func ExampleAsPartial_second() { |
| 51 | + // Produce a partial error. |
| 52 | + partialErr := xconsumererror.NewPartial(errors.New("some points failed to be written"), 10) |
| 53 | + |
| 54 | + // Partial errors wrap a Permanent error. |
| 55 | + fmt.Println(consumererror.IsPermanent(partialErr)) |
| 56 | + |
| 57 | + // Output: true |
| 58 | +} |
0 commit comments