fix(dnscheck): log "ok" rather than "<nil>" on success (#695)

See https://github.com/ooni/probe/issues/2020
This commit is contained in:
Yeganathan S 2022-02-16 19:47:44 +00:00 committed by GitHub
commit 6a63f1b044
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 33 additions and 2 deletions

View file

@ -61,3 +61,11 @@ func (logDiscarder) Warn(msg string) {}
// Warnf implements Logger.Warnf
func (logDiscarder) Warnf(format string, v ...interface{}) {}
// ErrorToStringOrOK emits "ok" on "<nil>"" values for success.
func ErrorToStringOrOK(err error) string {
if err != nil {
return err.Error()
}
return "ok"
}

View file

@ -1,6 +1,9 @@
package model
import "testing"
import (
"io"
"testing"
)
func TestDiscardLoggerWorksAsIntended(t *testing.T) {
logger := DiscardLogger
@ -11,3 +14,20 @@ func TestDiscardLoggerWorksAsIntended(t *testing.T) {
logger.Warn("foo")
logger.Warnf("%s", "foo")
}
func TestErrorToStringOrOK(t *testing.T) {
t.Run("on success", func(t *testing.T) {
expectedResult := ErrorToStringOrOK(nil)
if expectedResult != "ok" {
t.Fatal("expected ok")
}
})
t.Run("on failure", func(t *testing.T) {
err := io.EOF
expectedResult := ErrorToStringOrOK(err)
if expectedResult != err.Error() {
t.Fatal("not the result we expected", expectedResult)
}
})
}