Skip to content
This repository was archived by the owner on Nov 7, 2022. It is now read-only.

Commit f18a918

Browse files
author
Bogdan Drutu
authored
Remove duplicate code to combine errors. (#422)
1 parent a913fda commit f18a918

2 files changed

Lines changed: 12 additions & 26 deletions

File tree

internal/collector/processor/multi_processor.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
package processor
1616

1717
import (
18-
"fmt"
19-
"strings"
18+
"github.com/census-instrumentation/opencensus-service/internal"
2019

2120
"github.com/spf13/cast"
2221

@@ -124,18 +123,5 @@ func (msp *multiSpanProcessor) ProcessSpans(batch *agenttracepb.ExportTraceServi
124123
maxFailures = failures
125124
}
126125
}
127-
128-
var err error
129-
numErrors := len(errors)
130-
if numErrors == 1 {
131-
err = errors[0]
132-
} else if numErrors > 1 {
133-
errMsgs := make([]string, numErrors)
134-
for _, err := range errors {
135-
errMsgs = append(errMsgs, err.Error())
136-
}
137-
err = fmt.Errorf("[%s]", strings.Join(errMsgs, "; "))
138-
}
139-
140-
return maxFailures, err
126+
return maxFailures, internal.CombineErrors(errors)
141127
}

internal/internal.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package internal
1616

1717
import (
1818
"encoding/json"
19-
"errors"
2019
"fmt"
2120
"io/ioutil"
2221
"os"
@@ -102,14 +101,15 @@ func TimeToTimestamp(t time.Time) *timestamp.Timestamp {
102101

103102
// CombineErrors converts a list of errors into one error.
104103
func CombineErrors(errs []error) error {
105-
if len(errs) == 0 {
106-
return nil
107-
}
108-
109-
// Otherwise
110-
buf := new(strings.Builder)
111-
for _, err := range errs {
112-
fmt.Fprintf(buf, "%v\n", err)
104+
numErrors := len(errs)
105+
if numErrors == 1 {
106+
return errs[0]
107+
} else if numErrors > 1 {
108+
errMsgs := make([]string, numErrors)
109+
for _, err := range errs {
110+
errMsgs = append(errMsgs, err.Error())
111+
}
112+
return fmt.Errorf("[%s]", strings.Join(errMsgs, "; "))
113113
}
114-
return errors.New(buf.String())
114+
return nil
115115
}

0 commit comments

Comments
 (0)