-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from donutloop/feat/multierror
Multierror: concatenate errors into one error
- Loading branch information
Showing
4 changed files
with
105 additions
and
0 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,21 @@ | ||
# Usage | ||
|
||
MultiError concatenate errors into one error. | ||
|
||
## Example | ||
```go | ||
package main | ||
|
||
import ( | ||
"github.com/donutloop/toolkit/multierror" | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
errs := []error{ | ||
errors.New("error connect to db failed"), | ||
errors.New("error marschaling json"), | ||
} | ||
fmt.Println(multierror.New(errs...)) | ||
} | ||
``` |
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,18 @@ | ||
package multierror_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
"errors" | ||
|
||
"github.com/donutloop/toolkit/multierror" | ||
) | ||
|
||
func ExampleMultiError() { | ||
errs := []error{ | ||
errors.New("error connect to db failed"), | ||
errors.New("error marschaling json"), | ||
} | ||
fmt.Println(multierror.New(errs...)) | ||
// Output: multiple errors: error connect to db failed; error marschaling json | ||
} |
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,44 @@ | ||
package multierror | ||
|
||
import "bytes" | ||
|
||
// New concatenate errors into one. | ||
// If all errors are nil then it will returns nil | ||
// otherwise the return value is a MultiError containing all the non-nil error. | ||
func New(errs ...error) error { | ||
if len(errs) == 0 { | ||
return nil | ||
} | ||
errBucket := make([]error, 0, len(errs)/2) | ||
for _, err := range errs { | ||
if err != nil { | ||
errBucket = append(errBucket, err) | ||
} | ||
} | ||
if len(errBucket) == 0 { | ||
return nil | ||
} | ||
return multiError{errBucket} | ||
} | ||
|
||
// MultiError concatenate errors into one error. | ||
type multiError struct { | ||
Errors []error | ||
} | ||
|
||
func (es multiError) Error() string { | ||
switch len(es.Errors) { | ||
case 1: | ||
return es.Errors[0].Error() | ||
default: | ||
var buf bytes.Buffer | ||
buf.WriteString("multiple errors: ") | ||
for i, e := range es.Errors { | ||
if i > 0 { | ||
buf.WriteString("; ") | ||
} | ||
buf.WriteString(e.Error()) | ||
} | ||
return buf.String() | ||
} | ||
} |
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,22 @@ | ||
package multierror_test | ||
|
||
import ( | ||
"errors" | ||
|
||
"testing" | ||
|
||
"github.com/donutloop/toolkit/multierror" | ||
) | ||
|
||
func TestMultiError_Error(t *testing.T) { | ||
errs := []error{ | ||
nil, | ||
errors.New("error connect to db failed"), | ||
errors.New("error marschaling json"), | ||
} | ||
expectedValue := "multiple errors: error connect to db failed; error marschaling json" | ||
err := multierror.New(errs...) | ||
if err.Error() != expectedValue { | ||
t.Errorf(`unexpected error message (actual:"%s", expected: "%s")`, err.Error(), expectedValue) | ||
} | ||
} |