diff --git a/errors.go b/errors.go index 0ded8f0..d7b4cbd 100644 --- a/errors.go +++ b/errors.go @@ -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), } diff --git a/errors_test.go b/errors_test.go index 3da7ee3..561c98b 100644 --- a/errors_test.go +++ b/errors_test.go @@ -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()) + } +}