Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(60)

Side by Side Diff: go/src/infra/libs/infra_util/errors.go

Issue 662113003: Drover's back, baby! (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git/+/master
Patch Set: more tests and refactors Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « go/src/infra/libs/infra_util/buf.go ('k') | go/src/infra/libs/infra_util/stringSet.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package infra_util
6
7 import (
8 "bytes"
9 "fmt"
10 "sync"
11 )
12
13 type MultiError []error
14
15 func FanOutIn(hint int, producer func(chan<- func() error)) error {
16 funcChan := make(chan func() error, hint)
17 errChan := make(chan error)
18 go func() {
19 go func() {
20 defer close(funcChan)
21 producer(funcChan)
22 }()
23 grp := sync.WaitGroup{}
24 for f := range funcChan {
25 grp.Add(1)
26 f := f
27 go func() { defer grp.Done(); errChan <- f() }()
28 }
29 go func() { defer close(errChan); grp.Wait() }()
30 }()
31 return MultiErrorFromChan(errChan)
32 }
33
34 func MultiErrorFromChan(in <-chan error) error {
35 ret := MultiError{}
36 for err := range in {
37 if err != nil {
38 ret = append(ret, err)
39 }
40 }
41 if len(ret) != 0 {
42 return &ret
43 }
44 return nil
45 }
46
47 func (m *MultiError) Error() string {
48 if m == nil {
49 return "ME[]"
50 }
51 buf := &bytes.Buffer{}
52 fmt.Fprintf(buf, "ME[")
53 first := true
54 for _, e := range *m {
55 if !first {
56 fmt.Fprint(buf, ",")
57 }
58 first = false
59 fmt.Fprintf(buf, "%#v", e.Error())
60 }
61 fmt.Fprintf(buf, "]")
62 return buf.String()
63 }
64
65 func (m *MultiError) ErrorStrings() StringSet {
66 ret := make(StringSet, len(*m))
67 for _, e := range *m {
68 ret.Add(e.Error())
69 }
70 return ret
71 }
OLDNEW
« no previous file with comments | « go/src/infra/libs/infra_util/buf.go ('k') | go/src/infra/libs/infra_util/stringSet.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698