Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix safeError & safeString for json format #20

Merged
merged 2 commits into from Mar 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions json_logger.go
Expand Up @@ -68,7 +68,7 @@ func safeString(str fmt.Stringer) (s string) {
if v := reflect.ValueOf(str); v.Kind() == reflect.Ptr && v.IsNil() {
s = "NULL"
} else {
panic(panicVal)
s = fmt.Sprintf("panic stringer value: %v", panicVal)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should make it more obvious something went wrong with getting the string (or error). I think this message may not be prominent enough.

Let's change this to:

fmt.Sprintf("PANIC in String method: %v", panicVal)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure! updated.

}
}
}()
Expand All @@ -82,7 +82,7 @@ func safeError(err error) (s interface{}) {
if v := reflect.ValueOf(err); v.Kind() == reflect.Ptr && v.IsNil() {
s = nil
} else {
panic(panicVal)
s = fmt.Sprintf("panic error value: %v", panicVal)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to above let's make this:

fmt.Sprintf("PANIC in Error method: %v", panicVal)

}
}
}()
Expand Down
38 changes: 38 additions & 0 deletions json_logger_test.go
Expand Up @@ -60,6 +60,19 @@ func TestJSONLoggerNilStringerKey(t *testing.T) {
}
}

func TestJSONLoggerPanicStringerValue(t *testing.T) {
t.Parallel()

buf := &bytes.Buffer{}
logger := log.NewJSONLogger(buf)
if err := logger.Log("k", unsafeStringer{}); err != nil {
t.Fatal(err)
}
if want, have := `{"k":"panic stringer value: error"}`+"\n", buf.String(); want != have {
t.Errorf("\nwant %#v\nhave %#v", want, have)
}
}

func TestJSONLoggerNilErrorValue(t *testing.T) {
t.Parallel()

Expand All @@ -73,6 +86,19 @@ func TestJSONLoggerNilErrorValue(t *testing.T) {
}
}

func TestJSONLoggerPanicErrorValue(t *testing.T) {
t.Parallel()

buf := &bytes.Buffer{}
logger := log.NewJSONLogger(buf)
if err := logger.Log("err", unsafeError{}); err != nil {
t.Fatal(err)
}
if want, have := `{"err":"panic error value: error"}`+"\n", buf.String(); want != have {
t.Errorf("\nwant %#v\nhave %#v", want, have)
}
}

func TestJSONLoggerNoHTMLEscape(t *testing.T) {
t.Parallel()
buf := &bytes.Buffer{}
Expand Down Expand Up @@ -160,6 +186,18 @@ func (s stringError) Error() string {
return string(s)
}

type unsafeStringer struct{}

func (s unsafeStringer) String() string {
panic("error")
}

type unsafeError struct{}

func (s unsafeError) Error() string {
panic("error")
}

func BenchmarkJSONLoggerSimple(b *testing.B) {
benchmarkRunner(b, log.NewJSONLogger(ioutil.Discard), baseMessage)
}
Expand Down