Skip to content

Commit

Permalink
[logging] Adds a method for using specific messaging for known errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dougbtv committed Jun 9, 2020
1 parent 809a16f commit 99b8ba8
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
45 changes: 45 additions & 0 deletions logging/known_errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package logging

import (
"fmt"
"strings"
)

// knownErrorPatterns Returns list of all known error patterns
func knownErrorPatterns() []string {
return []string{
"error dialing DHCP daemon",
}
}

// getKnownErrorMessage returns
func getKnownErrorMessage(patternkey string) (string, error) {
messages := map[string]string{
"error dialing DHCP daemon": "Please check that the DHCP CNI daemon is running and is properly configured.",
}

if val, ok := messages[patternkey]; ok {
return val, nil
}

return "", fmt.Errorf("Known error key '" + patternkey + "' does not have a message")

}

// detectKnownErrors detects the first known error given n number of stringers as passed to the logging methods
func addKnownErrorMessage(a ...interface{}) string {
var knownerrormessage string
knownerrors := knownErrorPatterns()

knownerrorloop:
for _, eachstringer := range a {
for _, eachknownerror := range knownerrors {
if strings.Contains(fmt.Sprintf("%s", eachstringer), eachknownerror) {
knownerrormessage, _ = getKnownErrorMessage(eachknownerror)
knownerrormessage = " (" + knownerrormessage + ") "
break knownerrorloop
}
}
}
return knownerrormessage
}
1 change: 1 addition & 0 deletions logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func Verbosef(format string, a ...interface{}) {

// Errorf prints logging if logging level >= error
func Errorf(format string, a ...interface{}) error {
format = addKnownErrorMessage(a) + format
printf(ErrorLevel, format, a...)
return fmt.Errorf(format, a...)
}
Expand Down
25 changes: 23 additions & 2 deletions logging/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
package logging

import (
"testing"

"fmt"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"testing"
)

func TestLogging(t *testing.T) {
Expand Down Expand Up @@ -79,4 +79,25 @@ var _ = Describe("logging operations", func() {
currentLevel := loggingLevel
Expect(currentLevel).To(Equal(GetLoggingLevel()))
})

It("Detects a known error", func() {
newerror := Errorf("Testing 123", fmt.Errorf("error dialing DHCP daemon: dial unix /run/cni/dhcp.sock: connect: no such file or directory"))
Expect(newerror.Error()).To(ContainSubstring("Please check that the DHCP CNI daemon is running"))
})

It("Properly errors when an error message is not set for a pattern", func() {
_, err := getKnownErrorMessage("intentionally unset error pattern")
Expect(err).To(HaveOccurred())
})

It("Has a message set for each error pattern that is set", func() {
// If this fails, it probably means you added the error pattern, but, not the error message.
errpats := knownErrorPatterns()
for _, errorpattern := range errpats {
message, err := getKnownErrorMessage(errorpattern)
Expect(message).NotTo(HaveLen(0))
Expect(err).NotTo(HaveOccurred())
}
})

})

0 comments on commit 99b8ba8

Please sign in to comment.