Skip to content

Conversation

@jonathanlab
Copy link

@jonathanlab jonathanlab commented Nov 17, 2025

Closes #132

Exceptions now support custom properties. Technically backwards compatible since NewDefaultException can be called w/o properties, but since we drop ExceptionInApiProperties it's a breaking change. Also added slog support.

client.Enqueue(posthog.NewDefaultException(
    time.Now(),
    "user-123",
    "Error title",
    "Description",
    posthog.NewProperties().
        Set("environment", "production").
        Set("retry_count", 3),
))

// Slog
logger := slog.New(posthog.NewSlogCaptureHandler(baseHandler, client,
    posthog.WithPropertiesFn(func(ctx context.Context, r slog.Record) posthog.Properties {
        props := posthog.NewProperties()
        r.Attrs(func(a slog.Attr) bool {
            props.Set(a.Key, a.Value.Any())
            return true
        })
        return props
    }),
))
logger.Error("Payment failed", "payment_id", "pay_123", "amount", 99.99)

Comment on lines +1 to +6
## 1.7.0

**Breaking Changes:**
* `ExceptionInApi.Properties` type changed from `ExceptionInApiProperties` (struct) to `Properties` (map[string]interface{})
* Removed `ExceptionInApiProperties` struct, replaced with `Properties` map for custom properties.
* Note: All existing system properties (`$lib`, `$lib_version`, `distinct_id`, `$exception_list`, etc.) are still attached and sent to the API, only the type representation has changed.
Copy link
Member

Choose a reason for hiding this comment

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

if we follow semver (Do we here?) this should be a major since it'll break people's apps compilation until they change the type

Copy link
Author

@jonathanlab jonathanlab Nov 17, 2025

Choose a reason for hiding this comment

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

Seems like in CHANGELOG.md we previously used a minor version for a breaking changes release, so not sure if we're strictly following semver. Happy to bump to 2.0.0 if we want to start being more strict about it.

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah, the versioning in Go got messed up at some point and people often release breaking changes as 1.X change.

Choose a reason for hiding this comment

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

imo, until the sdk reaches feature parity with the other more mature ones (e.g. js sdk), breaking changes are okay without major version bump. The APIs in go sdk still leaves a lot to be desired (compared to the other ones). I'd assume a few other breaking changes like this might be necessary going forward.

func NewDefaultException(
timestamp time.Time,
distinctID, title, description string,
properties ...Properties,
Copy link
Contributor

Choose a reason for hiding this comment

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

This is confusing and will lead to errors.
It's know sometimes people abuse variadic arg number in Go, but I think we should just proper properties Properties, that's all.

Copy link
Contributor

Choose a reason for hiding this comment

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

alternatively, add a method to Exception: WithProperties

Copy link
Author

Choose a reason for hiding this comment

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

Great idea, I'll do WithProperties instead.

@github-project-automation github-project-automation bot moved this from In Review to Approved in Feature Flags Nov 17, 2025
@posthog-project-board-bot posthog-project-board-bot bot moved this from Approved to In Review in Feature Flags Nov 17, 2025
func (msg Exception) APIfy() APIMessage {
libVersion := getVersion()

properties := Properties{}.

Choose a reason for hiding this comment

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

Just curious, wouldn't it be better to re-use msg.Properties instead of creating a new one here?

Copy link
Author

@jonathanlab jonathanlab Nov 17, 2025

Choose a reason for hiding this comment

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

We can't do that unfortunately, because that'd mutate the caller's msg.Properties (preferably no side-effects happen), so we create a new one instead

Fallback: "<no linked error>",
},
properties: func(ctx context.Context, r slog.Record) Properties {
return NewProperties()
Copy link
Contributor

@vdekrijger vdekrijger Nov 18, 2025

Choose a reason for hiding this comment

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

Minor, but is it worth considering having something like this (shamelessly taken from the unit test) as default behavior?

Suggested change
return NewProperties()
props := NewProperties()
r.Attrs(func(a slog.Attr) bool {
props.Set(a.Key, a.Value.Any())
return true
})
return props

I figure that most users will likely do something like this as their default behaviour, and if they don't they can override it through WithPropertiesFn.

@github-project-automation github-project-automation bot moved this from In Review to Approved in Feature Flags Nov 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Approved

Development

Successfully merging this pull request may close these issues.

Support custom Properties in Exception

6 participants