mirror of
https://github.com/openshift/image-registry.git
synced 2026-02-05 09:45:55 +01:00
Assigning a typed nil to the error interface gives a non-nil value.
That breaks the common pattern `if nil != nil { ... }`.
To avoid this problem, NewError should return an interface.
28 lines
500 B
Go
28 lines
500 B
Go
package errors
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
func check(fail bool) Error {
|
|
if fail {
|
|
return NewError("Check", "fail is true", fmt.Errorf("underlying error"))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func TestNewError(t *testing.T) {
|
|
var err error
|
|
|
|
err = check(true)
|
|
if expected := "Check: fail is true: underlying error"; err == nil || err.Error() != expected {
|
|
t.Errorf("check(true): got %v, want %v", err, expected)
|
|
}
|
|
|
|
err = check(false)
|
|
if err != nil {
|
|
t.Errorf("check(false): got %v, want nil", err)
|
|
}
|
|
}
|