Compare commits

...
3 changed files with 46 additions and 7 deletions
+7
View File
@@ -33,12 +33,19 @@ class NotificationListener < BaseListener
return if event.data[:notifiable_assignee_change].blank?
return if conversation.pending?
Rails.logger.info "[NotificationListener] Assignee changed for conversation: #{conversation.id} to #{assignee&.id} [#{assignee&.email}]"
NotificationBuilder.new(
notification_type: 'conversation_assignment',
user: assignee,
account: account,
primary_actor: conversation
).perform
rescue NoMethodError => e
Rails.logger.error "[NotificationListener] Error in assignee_changed: #{e.message}"
exception_tracker = ChatwootExceptionTracker.new(e, account: account,
additional_context: { 'conversation': conversation, 'assignee': assignee })
exception_tracker.capture_exception
end
def message_created(event)
+24 -7
View File
@@ -5,10 +5,11 @@
############
class ChatwootExceptionTracker
def initialize(exception, user: nil, account: nil)
def initialize(exception, user: nil, account: nil, additional_context: {})
@exception = exception
@user = user
@account = account
@additional_context = additional_context
end
def capture_exception
@@ -20,13 +21,29 @@ class ChatwootExceptionTracker
def capture_exception_with_sentry
Sentry.with_scope do |scope|
if @account.present?
scope.set_context('account', { id: @account.id, name: @account.name })
scope.set_tags(account_id: @account.id)
end
scope.set_user(id: @user.id, email: @user.email) if @user.is_a?(User)
append_account_context(scope)
append_additional_context(scope)
append_user_context(scope)
Sentry.capture_exception(@exception)
end
end
def append_account_context(scope)
return if @account.blank?
scope.set_context('account', { id: @account.id, name: @account.name })
scope.set_tags(account_id: @account.id)
end
def append_additional_context(scope)
@additional_context.each do |key, value|
scope.set_context(key.to_s, value)
end
end
def append_user_context(scope)
return unless @user.is_a?(User)
scope.set_user(id: @user.id, email: @user.email)
end
end
@@ -22,5 +22,20 @@ describe ChatwootExceptionTracker do
described_class.new('random').capture_exception
end
end
it 'sets additional context when provided' do
additional_context = { key1: 'value1', key2: 'value2' }
with_modified_env SENTRY_DSN: 'random dsn' do
scope = instance_double(Sentry::Scope)
allow(Sentry).to receive(:with_scope).and_yield(scope)
expect(scope).to receive(:set_context).with('key1', 'value1')
expect(scope).to receive(:set_context).with('key2', 'value2')
expect(Sentry).to receive(:capture_exception).with('random')
described_class.new('random', additional_context: additional_context).capture_exception
end
end
end
end