feat: handle Channel errors (#11015)
This PR adds missing error handlers for the following channels and cases 1. WhatsApp - Generic Handlers for both Cloud and 360Dialog (Deprecated) 2. Instagram - Handler for a case where there is an HTTP error instead of an `:error` in the 200 response 3. Facebook - Errors from the two sentry issues ([Net::OpenTimeout](https://chatwoot-p3.sentry.io/issues/6164805227) & [JSON::ParserError](https://chatwoot-p3.sentry.io/issues/5903200786)) 4. SMS: Generic handlers for Bandwidth SMS #### Checklist - [x] Bandwidth SMS - [x] Whatsapp Cloud + 360 Dialog - [x] Twilio SMS - [x] Line - [x] Telegram - [x] Instagram - [x] Facebook - [x] GMail - [x] 365 Mail - [x] SMTP Mail --------- Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
This commit is contained in:
@@ -4,7 +4,7 @@ describe Instagram::SendOnInstagramService do
|
||||
subject(:send_reply_service) { described_class.new(message: message) }
|
||||
|
||||
before do
|
||||
stub_request(:post, /graph.facebook.com/)
|
||||
stub_request(:post, /graph\.facebook\.com/)
|
||||
create(:message, message_type: :incoming, inbox: instagram_inbox, account: account, conversation: conversation)
|
||||
end
|
||||
|
||||
@@ -15,16 +15,49 @@ describe Instagram::SendOnInstagramService do
|
||||
let(:contact_inbox) { create(:contact_inbox, contact: contact, inbox: instagram_inbox) }
|
||||
let(:conversation) { create(:conversation, contact: contact, inbox: instagram_inbox, contact_inbox: contact_inbox) }
|
||||
let(:response) { double }
|
||||
let(:mock_response) do
|
||||
instance_double(
|
||||
HTTParty::Response,
|
||||
:success? => true,
|
||||
:body => { message_id: 'anyrandommessageid1234567890' }.to_json,
|
||||
:parsed_response => { 'message_id' => 'anyrandommessageid1234567890' }
|
||||
)
|
||||
end
|
||||
|
||||
let(:error_body) do
|
||||
{
|
||||
'error' => {
|
||||
'message' => 'The Instagram account is restricted.',
|
||||
'type' => 'OAuthException',
|
||||
'code' => 400,
|
||||
'fbtrace_id' => 'anyrandomfbtraceid1234567890'
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
let(:error_response) do
|
||||
instance_double(
|
||||
HTTParty::Response,
|
||||
:success? => false,
|
||||
:body => error_body.to_json,
|
||||
:parsed_response => error_body
|
||||
)
|
||||
end
|
||||
|
||||
let(:response_with_error) do
|
||||
instance_double(
|
||||
HTTParty::Response,
|
||||
:success? => true,
|
||||
:body => error_body.to_json,
|
||||
:parsed_response => error_body
|
||||
)
|
||||
end
|
||||
|
||||
describe '#perform' do
|
||||
context 'with reply' do
|
||||
before do
|
||||
allow(Facebook::Messenger::Configuration::AppSecretProofCalculator).to receive(:call).and_return('app_secret_key', 'access_token')
|
||||
allow(HTTParty).to receive(:post).and_return(
|
||||
{
|
||||
'message_id': 'anyrandommessageid1234567890'
|
||||
}
|
||||
)
|
||||
allow(HTTParty).to receive(:post).and_return(mock_response)
|
||||
end
|
||||
|
||||
context 'without message_tag HUMAN_AGENT' do
|
||||
@@ -35,22 +68,8 @@ describe Instagram::SendOnInstagramService do
|
||||
it 'if message is sent from chatwoot and is outgoing' do
|
||||
message = create(:message, message_type: 'outgoing', inbox: instagram_inbox, account: account, conversation: conversation)
|
||||
|
||||
allow(HTTParty).to receive(:post).with(
|
||||
{
|
||||
recipient: { id: contact.get_source_id(instagram_inbox.id) },
|
||||
message: {
|
||||
text: message.content
|
||||
}
|
||||
}
|
||||
).and_return(
|
||||
{
|
||||
'message_id': 'anyrandommessageid1234567890'
|
||||
}
|
||||
)
|
||||
|
||||
response = described_class.new(message: message).perform
|
||||
|
||||
expect(response).to eq({ message_id: 'anyrandommessageid1234567890' })
|
||||
expect(response['message_id']).to eq('anyrandommessageid1234567890')
|
||||
end
|
||||
|
||||
it 'if message is sent from chatwoot and is outgoing with multiple attachments' do
|
||||
@@ -78,22 +97,13 @@ describe Instagram::SendOnInstagramService do
|
||||
message.save!
|
||||
response = described_class.new(message: message).perform
|
||||
|
||||
expect(response).to eq({ message_id: 'anyrandommessageid1234567890' })
|
||||
expect(response['message_id']).to eq('anyrandommessageid1234567890')
|
||||
end
|
||||
|
||||
it 'if message sent from chatwoot is failed' do
|
||||
message = create(:message, message_type: 'outgoing', inbox: instagram_inbox, account: account, conversation: conversation)
|
||||
|
||||
allow(HTTParty).to receive(:post).and_return(
|
||||
{
|
||||
'error': {
|
||||
'message': 'The Instagram account is restricted.',
|
||||
'type': 'OAuthException',
|
||||
'code': 400,
|
||||
'fbtrace_id': 'anyrandomfbtraceid1234567890'
|
||||
}
|
||||
}
|
||||
)
|
||||
allow(HTTParty).to receive(:post).and_return(response_with_error)
|
||||
described_class.new(message: message).perform
|
||||
expect(HTTParty).to have_received(:post)
|
||||
expect(message.reload.status).to eq('failed')
|
||||
@@ -129,5 +139,39 @@ describe Instagram::SendOnInstagramService do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when handling errors' do
|
||||
before do
|
||||
allow(Facebook::Messenger::Configuration::AppSecretProofCalculator).to receive(:call).and_return('app_secret_key', 'access_token')
|
||||
end
|
||||
|
||||
it 'handles HTTP errors' do
|
||||
message = create(:message, message_type: 'outgoing', inbox: instagram_inbox, account: account, conversation: conversation)
|
||||
allow(HTTParty).to receive(:post).and_return(error_response)
|
||||
|
||||
described_class.new(message: message).perform
|
||||
|
||||
expect(message.reload.status).to eq('failed')
|
||||
expect(message.reload.external_error).to eq('400 - The Instagram account is restricted.')
|
||||
end
|
||||
|
||||
it 'handles response errors' do
|
||||
message = create(:message, message_type: 'outgoing', inbox: instagram_inbox, account: account, conversation: conversation)
|
||||
|
||||
error_response = instance_double(
|
||||
HTTParty::Response,
|
||||
success?: true,
|
||||
body: { 'error' => { 'message' => 'Invalid message format', 'code' => 100 } }.to_json,
|
||||
parsed_response: { 'error' => { 'message' => 'Invalid message format', 'code' => 100 } }
|
||||
)
|
||||
|
||||
allow(HTTParty).to receive(:post).and_return(error_response)
|
||||
|
||||
described_class.new(message: message).perform
|
||||
|
||||
expect(message.reload.status).to eq('failed')
|
||||
expect(message.reload.external_error).to eq('100 - Invalid message format')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user