-
Notifications
You must be signed in to change notification settings - Fork 507
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FAB-8261] Introduce Multi Errors type
This type is used to represent multiple errors, typically from multiple server targets invoked by the SDK in the same operation. Change-Id: I718478751c93e5b430b929dfd82f1d263bf864f7 Signed-off-by: Divyank Katira <Divyank.Katira@securekey.com>
- Loading branch information
Showing
29 changed files
with
374 additions
and
365 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package multi | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// Errors is used to represent multiple errors | ||
type Errors []error | ||
|
||
// New Errors object with the given errors. Only non-nil errors are added. | ||
func New(errs ...error) error { | ||
errors := Errors{} | ||
for _, err := range errs { | ||
if err != nil { | ||
errors = append(errors, err) | ||
} | ||
} | ||
|
||
if len(errors) == 0 { | ||
return nil | ||
} | ||
|
||
if len(errors) == 1 { | ||
return errors[0] | ||
} | ||
|
||
return errors | ||
} | ||
|
||
// Append error to Errors. If the first arg is not an Errors object, one will be created | ||
func Append(errs error, err error) error { | ||
m, ok := errs.(Errors) | ||
if !ok { | ||
return New(errs, err) | ||
} | ||
return append(m, err) | ||
} | ||
|
||
// ToError converts Errors to the error interface | ||
// returns nil if no errors are present, a single error object if only one is present | ||
func (errs Errors) ToError() error { | ||
if len(errs) == 0 { | ||
return nil | ||
} | ||
if len(errs) == 1 { | ||
return errs[0] | ||
} | ||
return errs | ||
} | ||
|
||
// Error implements the error interface to return a string representation of Errors | ||
func (errs Errors) Error() string { | ||
if len(errs) == 0 { | ||
return "" | ||
} | ||
if len(errs) == 1 { | ||
return errs[0].Error() | ||
} | ||
|
||
errors := []string{fmt.Sprint("Multiple errors occurred: ")} | ||
for _, err := range errs { | ||
errors = append(errors, err.Error()) | ||
} | ||
return strings.Join(errors, "\n") | ||
} |
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,63 @@ | ||
/* | ||
Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package multi | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestErrorString(t *testing.T) { | ||
testErr := fmt.Errorf("test") | ||
var errs Errors | ||
|
||
assert.Equal(t, "", errs.Error()) | ||
|
||
errs = append(errs, testErr) | ||
assert.Equal(t, testErr.Error(), errs.Error()) | ||
|
||
errs = append(errs, testErr) | ||
assert.Equal(t, "Multiple errors occurred: \ntest\ntest", errs.Error()) | ||
} | ||
|
||
func TestAppend(t *testing.T) { | ||
testErr := fmt.Errorf("test") | ||
testErr2 := fmt.Errorf("test2") | ||
|
||
m := Append(nil, nil) | ||
assert.Nil(t, m) | ||
|
||
m = Append(nil, testErr) | ||
assert.Equal(t, testErr, m) | ||
|
||
m = Append(testErr, testErr2) | ||
m1, ok := m.(Errors) | ||
assert.True(t, ok) | ||
assert.Equal(t, testErr, m1[0]) | ||
assert.Equal(t, testErr2, m1[1]) | ||
|
||
m = Append(Errors{testErr}, testErr2) | ||
m1, ok = m.(Errors) | ||
assert.True(t, ok) | ||
assert.Equal(t, testErr, m1[0]) | ||
assert.Equal(t, testErr2, m1[1]) | ||
} | ||
|
||
func TestToError(t *testing.T) { | ||
testErr := fmt.Errorf("test") | ||
var errs Errors | ||
|
||
assert.Equal(t, nil, errs.ToError()) | ||
|
||
errs = append(errs, testErr) | ||
assert.Equal(t, testErr, errs.ToError()) | ||
|
||
errs = append(errs, testErr) | ||
assert.Equal(t, errs, errs.ToError()) | ||
} |
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
Oops, something went wrong.