From a0cf25ccdebedf13f28659e1245c9e543a9937f8 Mon Sep 17 00:00:00 2001 From: Muhsin Keloth Date: Fri, 7 Mar 2025 05:27:16 +0530 Subject: [PATCH] chore: Rescue slack link unfurling errors. (#11033) Fixes https://linear.app/chatwoot/issue/CW-4122/slackwebapierrorsmissingscope-missing-scope This PR adds the ability to handle errors when scopes are missing during link unfurling. Since link unfurling is just a nice-to-have feature that doesn't affect core functionality, we will silently ignore these errors. --------- Co-authored-by: Sojan --- lib/integrations/slack/send_on_slack_service.rb | 4 ++++ .../slack/send_on_slack_service_spec.rb | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/lib/integrations/slack/send_on_slack_service.rb b/lib/integrations/slack/send_on_slack_service.rb index c1454b869..e68dd81af 100644 --- a/lib/integrations/slack/send_on_slack_service.rb +++ b/lib/integrations/slack/send_on_slack_service.rb @@ -18,6 +18,10 @@ class Integrations::Slack::SendOnSlackService < Base::SendOnChannelService slack_client.chat_unfurl( event ) + # You may wonder why we're not requesting reauthorization and disabling hooks when scope errors occur. + # Since link unfurling is just a nice-to-have feature that doesn't affect core functionality, we will silently ignore these errors. + rescue Slack::Web::Api::Errors::MissingScope => e + Rails.logger.warn "Slack: Missing scope error: #{e.message}" end private diff --git a/spec/lib/integrations/slack/send_on_slack_service_spec.rb b/spec/lib/integrations/slack/send_on_slack_service_spec.rb index 684f2bc54..848a683b4 100644 --- a/spec/lib/integrations/slack/send_on_slack_service_spec.rb +++ b/spec/lib/integrations/slack/send_on_slack_service_spec.rb @@ -269,6 +269,19 @@ describe Integrations::Slack::SendOnSlackService do expect(hook).to be_disabled expect(hook).to have_received(:prompt_reauthorization!) end + + it 'logs MissingScope error during link unfurl' do + unflur_payload = { channel: 'channel', ts: 'timestamp', unfurls: {} } + error = Slack::Web::Api::Errors::MissingScope.new('Missing required scope') + + expect(slack_client).to receive(:chat_unfurl) + .with(unflur_payload) + .and_raise(error) + + expect(Rails.logger).to receive(:warn).with('Slack: Missing scope error: Missing required scope') + + link_builder.link_unfurl(unflur_payload) + end end context 'when message contains mentions' do