From ae649a5b3fd5d891123324b61dd6d64180e73079 Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Thu, 14 Dec 2023 14:03:40 +0530 Subject: [PATCH 1/2] refactor: add better logs to Instagram::MessageText (#8538) --- app/services/instagram/message_text.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app/services/instagram/message_text.rb b/app/services/instagram/message_text.rb index 22114feef..54bff38fa 100644 --- a/app/services/instagram/message_text.rb +++ b/app/services/instagram/message_text.rb @@ -16,8 +16,12 @@ class Instagram::MessageText < Instagram::WebhooksBaseService inbox_channel(instagram_id) # person can connect the channel and then delete the inbox return if @inbox.blank? + # This channel might require reauthorization, may be owner might have changed the fb password - return if @inbox.channel.reauthorization_required? + if @inbox.channel.reauthorization_required? + Rails.logger.info("Skipping message processing as reauthorization is required for inbox #{inbox.id}") + return + end return unsend_message if message_is_deleted? @@ -36,20 +40,24 @@ class Instagram::MessageText < Instagram::WebhooksBaseService end end + # rubocop:disable Metrics/AbcSize def ensure_contact(ig_scope_id) begin k = Koala::Facebook::API.new(@inbox.channel.page_access_token) if @inbox.facebook? result = k.get_object(ig_scope_id) || {} rescue Koala::Facebook::AuthenticationError => e @inbox.channel.authorization_error! + Rails.logger.warn("Authorization error for account #{@inbox.account_id} for #{inbox @inbox.id}") ChatwootExceptionTracker.new(e, account: @inbox.account).capture_exception rescue StandardError, Koala::Facebook::ClientError => e + Rails.logger.warn("[FacebookUserFetchClientError]: account_id #{@inbox.account_id} inbox_id #{inbox @inbox.id}") Rails.logger.warn("[FacebookUserFetchClientError]: #{e.message}") ChatwootExceptionTracker.new(e, account: @inbox.account).capture_exception end find_or_create_contact(result) if defined?(result) && result.present? end + # rubocop:enable Metrics/AbcSize def agent_message_via_echo? @messaging[:message][:is_echo].present? From 6c480098f7f4ac0b42e01f691198f2bce2b4ce39 Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Thu, 14 Dec 2023 16:18:23 +0530 Subject: [PATCH 2/2] chore: log authentication error for FB and Instagram (#8551) --- app/builders/messages/facebook/message_builder.rb | 12 ++++++++++-- app/builders/messages/instagram/message_builder.rb | 4 +++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/app/builders/messages/facebook/message_builder.rb b/app/builders/messages/facebook/message_builder.rb index 3efb184b9..fec298bce 100644 --- a/app/builders/messages/facebook/message_builder.rb +++ b/app/builders/messages/facebook/message_builder.rb @@ -25,7 +25,9 @@ class Messages::Facebook::MessageBuilder < Messages::Messenger::MessageBuilder build_contact_inbox build_message end - rescue Koala::Facebook::AuthenticationError + rescue Koala::Facebook::AuthenticationError => e + Rails.logger.warn("Facebook authentication error for inbox: #{@inbox.id} with error: #{e.message}") + Rails.logger.error e @inbox.channel.authorization_error! rescue StandardError => e ChatwootExceptionTracker.new(e, account: @inbox.account).capture_exception @@ -108,11 +110,15 @@ class Messages::Facebook::MessageBuilder < Messages::Messenger::MessageBuilder } end + # rubocop:disable Metrics/AbcSize + # rubocop:disable Metrics/MethodLength def contact_params begin k = Koala::Facebook::API.new(@inbox.channel.page_access_token) if @inbox.facebook? result = k.get_object(@sender_id) || {} - rescue Koala::Facebook::AuthenticationError + rescue Koala::Facebook::AuthenticationError => e + Rails.logger.warn("Facebook authentication error for inbox: #{@inbox.id} with error: #{e.message}") + Rails.logger.error e @inbox.channel.authorization_error! raise rescue Koala::Facebook::ClientError => e @@ -130,4 +136,6 @@ class Messages::Facebook::MessageBuilder < Messages::Messenger::MessageBuilder end process_contact_params_result(result) end + # rubocop:enable Metrics/AbcSize + # rubocop:enable Metrics/MethodLength end diff --git a/app/builders/messages/instagram/message_builder.rb b/app/builders/messages/instagram/message_builder.rb index 5b2243906..5610e0671 100644 --- a/app/builders/messages/instagram/message_builder.rb +++ b/app/builders/messages/instagram/message_builder.rb @@ -20,7 +20,9 @@ class Messages::Instagram::MessageBuilder < Messages::Messenger::MessageBuilder ActiveRecord::Base.transaction do build_message end - rescue Koala::Facebook::AuthenticationError + rescue Koala::Facebook::AuthenticationError => e + Rails.logger.warn("Instagram authentication error for inbox: #{@inbox.id} with error: #{e.message}") + Rails.logger.error e @inbox.channel.authorization_error! raise rescue StandardError => e