Skip to content

Commit

Permalink
Need to check the value for nil here.
Browse files Browse the repository at this point in the history
  • Loading branch information
jum committed Jun 25, 2024
1 parent d1dd97e commit b55253d
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 b55253d

Please sign in to comment.