refactor: send leadsquared activity errors to Sentry with context
Replace the manual log string in log_activity_error with structured context passed through ChatwootExceptionTracker.
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user