File tree Expand file tree Collapse file tree 2 files changed +63
-0
lines changed Expand file tree Collapse file tree 2 files changed +63
-0
lines changed Original file line number Diff line number Diff line change 1+ // Copyright The OpenTelemetry Authors
2+ // SPDX-License-Identifier: Apache-2.0
3+
4+ package consumererror // import "go.opentelemetry.io/collector/consumer/consumererror"
5+
6+ import "errors"
7+
8+ type partialError struct {
9+ inner error
10+ failed int
11+ }
12+
13+ var _ error = partialError {}
14+
15+ func (pe partialError ) Error () string {
16+ return pe .inner .Error ()
17+ }
18+
19+ func (pe partialError ) Unwrap () error {
20+ return pe .inner
21+ }
22+
23+ func (pe partialError ) Failed () int {
24+ return pe .failed
25+ }
26+
27+ // NewPartial creates a new partial error. This is used by consumers
28+ // to report errors where only a subset of the total items failed
29+ // to be written, but it is not possible to tell which particular items
30+ // failed.
31+ func NewPartial (err error , failed int ) error {
32+ return NewPermanent (partialError {
33+ inner : err ,
34+ failed : failed ,
35+ })
36+ }
37+
38+ // AsPartial checks if an error was wrapped with the NewPartial function,
39+ // or if it contains one such error in its Unwrap() tree.
40+ func AsPartial (err error ) (partialError , bool ) {
41+ var pe partialError
42+ ok := errors .As (err , & pe )
43+ return pe , ok
44+ }
Original file line number Diff line number Diff line change 1+ package consumererror
2+
3+ import (
4+ "errors"
5+ "testing"
6+
7+ "github.com/stretchr/testify/assert"
8+ )
9+
10+ func TestPartial (t * testing.T ) {
11+ internalErr := errors .New ("some points failed" )
12+ err := NewPartial (internalErr , 5 )
13+ assert .True (t , IsPermanent (err ))
14+ partialErr , ok := AsPartial (err )
15+ assert .True (t , ok )
16+ assert .Equal (t , 5 , partialErr .Failed ())
17+ assert .Equal (t , internalErr , partialErr .Unwrap ())
18+ assert .Equal (t , internalErr .Error (), partialErr .Error ())
19+ }
You can’t perform that action at this time.
0 commit comments