Skip to content

Commit

Permalink
Merge pull request #9 from m-mizutani/fix/nil-log-valuer
Browse files Browse the repository at this point in the history
fix(log): Fix nil pointer error in LogValue method
  • Loading branch information
m-mizutani authored Jul 7, 2024
2 parents ee7366c + 28c94fc commit cbba540
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ func (x *Error) Values() map[string]any {
}

func (x *Error) LogValue() slog.Value {
if x == nil {
return slog.AnyValue(nil)
}

attrs := []slog.Attr{
slog.String("message", x.msg),
}
Expand Down
10 changes: 10 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,13 @@ func TestLoggingNestedError(t *testing.T) {
t.Errorf("Expected log output to contain '\"color\":\"orange\"', got '%s'", out.String())
}
}

func TestLoggerWithNil(t *testing.T) {
out := &bytes.Buffer{}
var err *goerr.Error
logger := slog.New(slog.NewJSONHandler(out, nil))
logger.Error("fail", slog.Any("error", err))
if !strings.Contains(out.String(), `"error":null`) {
t.Errorf("Expected log output to contain '\"error\":null', got '%s'", out.String())
}
}

0 comments on commit cbba540

Please sign in to comment.