Compare commits

...
Author SHA1 Message Date
Shivam MishraandGitHub 45c65aa10c Merge branch 'develop' into feat/add-context-to-exception 2026-06-23 12:05:07 +05:30
Shivam Mishra 6812417043 refactor: send leadsquared activity errors to Sentry with context
Replace the manual log string in log_activity_error with structured
context passed through ChatwootExceptionTracker.
2026-06-23 12:02:08 +05:30
Shivam Mishra 15d47f94a1 feat: support structured additional context in exception tracker
Allow callers to pass an additional_context hash that is attached to the
Sentry event as named context blocks via scope.set_context.
2026-06-23 12:02:00 +05:30
3 changed files with 26 additions and 10 deletions
@@ -95,13 +95,20 @@ class Crm::Leadsquared::ProcessorService < Crm::BaseProcessorService
end
def log_activity_error(error, activity_type, conversation, payload: nil)
ChatwootExceptionTracker.new(error, account: @account).capture_exception
context = "account_id=#{conversation.account_id}, conversation_display_id=#{conversation.display_id}"
context = {
activity_type: activity_type,
account_id: conversation.account_id,
conversation_display_id: conversation.display_id
}
if payload
context += ", http_status=#{error.code}, prospect_id=#{payload[:lead_id]}, " \
"activity_event=#{payload[:activity_code]}, note_bytes=#{payload[:activity_note].to_s.bytesize}"
context.merge!(
http_status: error.code,
prospect_id: payload[:lead_id],
activity_event: payload[:activity_code],
note_bytes: payload[:activity_note].to_s.bytesize
)
end
Rails.logger.error("LeadSquared #{activity_type} activity failed: #{error.message} (#{context})")
ChatwootExceptionTracker.new(error, account: @account, additional_context: { leadsquared: context }).capture_exception
end
def get_activity_code(key)
+3 -1
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
@@ -25,6 +26,7 @@ class ChatwootExceptionTracker
scope.set_tags(account_id: @account.id)
end
@additional_context.each { |key, value| scope.set_context(key.to_s, value) }
scope.set_user(id: @user.id, email: @user.email) if @user.is_a?(User)
Sentry.capture_exception(@exception)
end
@@ -151,13 +151,20 @@ RSpec.describe Crm::Leadsquared::ProcessorService do
allow(activity_client).to receive(:post_activity)
.with('test_lead_id', 1001, activity_note)
.and_raise(StandardError.new('Activity error'))
allow(Rails.logger).to receive(:error)
end
it 'logs the error' do
it 'captures the exception with leadsquared context' do
tracker = instance_double(ChatwootExceptionTracker, capture_exception: nil)
allow(ChatwootExceptionTracker).to receive(:new).and_return(tracker)
service.handle_conversation_created(conversation)
expect(Rails.logger).to have_received(:error).with(/LeadSquared conversation activity failed/)
expect(ChatwootExceptionTracker).to have_received(:new).with(
instance_of(StandardError),
account: account,
additional_context: { leadsquared: hash_including(activity_type: 'conversation', conversation_display_id: conversation.display_id) }
)
expect(tracker).to have_received(:capture_exception)
end
end
end