Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/posthog/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,21 @@ def capture(attrs)

# Captures an exception as an event
#
# Supports both positional and keyword argument styles:
# capture_exception(exception, 'user-123', { '$exception_fingerprint' => 'CustomGroup' })
# capture_exception(exception, distinct_id: 'user-123', properties: { '$exception_fingerprint' => 'CustomGroup' })
#
# @param [Exception, String, Object] exception The exception to capture, a string message, or exception-like object
# @param [String] distinct_id The ID for the user (optional, defaults to a generated UUID)
# @param [Hash] additional_properties Additional properties to include with the exception event (optional)
def capture_exception(exception, distinct_id = nil, additional_properties = {})
# @param [Hash] kwargs Keyword form: :distinct_id, :properties
def capture_exception(exception, distinct_id = nil, additional_properties = nil, **kwargs)
unless kwargs.empty?
distinct_id = kwargs[:distinct_id] if distinct_id.nil?
additional_properties = kwargs[:properties] if additional_properties.nil?
end
additional_properties ||= {}

exception_info = ExceptionCapture.build_parsed_exception(exception)

return if exception_info.nil?
Expand Down
11 changes: 9 additions & 2 deletions posthog-rails/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,16 @@ PostHog.identify(
# Track an exception manually
PostHog.capture_exception(
exception,
current_user.id,
{ custom_property: 'value' }
distinct_id: current_user.id,
properties: {
custom_property: 'value',
# Override exception grouping by setting a fingerprint
'$exception_fingerprint' => 'CustomExceptionGroup'
}
)

# The positional form is still supported:
# PostHog.capture_exception(exception, current_user.id, { custom_property: 'value' })
```

### Background Jobs
Expand Down
25 changes: 25 additions & 0 deletions spec/posthog/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1347,6 +1347,31 @@ def run
expect(message[:distinct_id].length).to eq(36)
expect(message[:properties]['$process_person_profile']).to be false
end

it 'captures exception via kwargs with $exception_fingerprint' do
begin
raise StandardError, 'Test exception'
rescue StandardError => e
client.capture_exception(
e,
distinct_id: 'user-123',
properties: {
'$exception_fingerprint' => 'CustomExceptionGroup',
'user_agent' => 'Test Agent'
}
)
end

message = client.dequeue_last_message

expect(message[:event]).to eq('$exception')
expect(message[:distinct_id]).to eq('user-123')
expect(message[:properties]['$exception_list'].first['type']).to eq('StandardError')
expect(message[:properties]['$exception_list'].first['value']).to eq('Test exception')
expect(message[:properties]['$exception_fingerprint']).to eq('CustomExceptionGroup')
expect(message[:properties]['user_agent']).to eq('Test Agent')
expect(message[:properties]).not_to have_key('$process_person_profile')
end
end

context 'common' do
Expand Down
Loading