Skip to content

Commit

Permalink
Merge pull request #13 from jum/nil_fix_assert_or_log
Browse files Browse the repository at this point in the history
Need to check the value for nil here.
  • Loading branch information
nanoandrew4 authored Jul 4, 2024
2 parents 3ae225d + 3a33f6a commit 778a7d1
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions ngcplogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,14 +367,22 @@ func (l *nGCPLogger) extractMsgFromPayload(m map[string]any) {

func assertOrLog[T any](val any) T {
var v T
var ok bool
v, ok = val.(T)
if !ok {
if val == nil {
_, file, line, ok := runtime.Caller(1)
if !ok {
file = "unknown"
}
slog.Error("unexpected type", "want", reflect.TypeOf(v).String(), "got", reflect.TypeOf(val).String(), "file", file, "line", line)
slog.Error("unexpected nil value", "file", file, "line", line)
} else {
var ok bool
v, ok = val.(T)
if !ok {
_, file, line, ok := runtime.Caller(1)
if !ok {
file = "unknown"
}
slog.Error("unexpected type", "want", reflect.TypeOf(v).String(), "got", reflect.TypeOf(val).String(), "file", file, "line", line)
}
}
return v
}
Expand Down

0 comments on commit 778a7d1

Please sign in to comment.