forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New object: ErrChan for concurrent err handling
- Loading branch information
Showing
6 changed files
with
75 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package errchan | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type ErrChan struct { | ||
C chan error | ||
} | ||
|
||
// New returns an error channel of max length 'n' | ||
// errors can be sent to the ErrChan.C channel, and will be returned when | ||
// ErrChan.Error() is called. | ||
func New(n int) *ErrChan { | ||
return &ErrChan{ | ||
C: make(chan error, n), | ||
} | ||
} | ||
|
||
// Error closes the ErrChan.C channel and returns an error if there are any | ||
// non-nil errors, otherwise returns nil. | ||
func (e *ErrChan) Error() error { | ||
close(e.C) | ||
|
||
var out string | ||
for err := range e.C { | ||
if err != nil { | ||
out += "[" + err.Error() + "], " | ||
} | ||
} | ||
|
||
if out != "" { | ||
return fmt.Errorf("Errors encountered: " + strings.TrimRight(out, ", ")) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters