Skip to content

Commit

Permalink
Update ConfigErrors.Extend so that nil.Extend(nil) == nil. (istio#453)
Browse files Browse the repository at this point in the history
Previously such a call would return a zero-value ConfigErrors, breaking the standard `if ce := foo(); ce != nil { ... }` if ConfigErrors.Extend was used anywhere during the construction of the error in `foo` (or any of the methods it calls, transitively).

Former-commit-id: fe4de8be19dc07b25c32029b549617f6a4b5a509
  • Loading branch information
ZackButcher committed Mar 24, 2017
1 parent e557072 commit 4013ac3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
9 changes: 4 additions & 5 deletions mixer/pkg/adapter/configError.go
Expand Up @@ -87,12 +87,11 @@ func (e *ConfigErrors) Error() string {

// Extend joins 2 configErrors together.
func (e *ConfigErrors) Extend(ee *ConfigErrors) *ConfigErrors {
ce := e
if ce == nil {
ce = &ConfigErrors{}
if e == nil {
return ee
}
if ee != nil {
ce.Multi = me.Append(ce.Multi, ee.Multi.Errors...)
e.Multi = me.Append(e.Multi, ee.Multi.Errors...)
}
return ce
return e
}
10 changes: 10 additions & 0 deletions mixer/pkg/adapter/configError_test.go
Expand Up @@ -90,4 +90,14 @@ func TestExtend(t *testing.T) {
if len(c1.Multi.Errors) != len(c2.Multi.Errors) {
t.Error("Expected lengths to match")
}

var ce *ConfigErrors
if ce.Extend(nil) != nil {
t.Error("nil.Extend(nil) != nil")
}

ce = ce.Append("Foo", fmt.Errorf("format %d", 0))
if ce.Extend(nil) != ce {
t.Error("ce.Extend(nil) != ce")
}
}

0 comments on commit 4013ac3

Please sign in to comment.