From 59bee66e631d713673c2aca6a931be28ff749431 Mon Sep 17 00:00:00 2001 From: Sojan Jose Date: Sun, 11 Oct 2020 20:22:21 +0530 Subject: [PATCH] chore: Handle exceptions on external URL calls (#1334) --- app/builders/messages/facebook/message_builder.rb | 2 +- app/controllers/api/v1/accounts/callbacks_controller.rb | 2 +- app/jobs/contact_avatar_job.rb | 2 +- app/services/twilio/incoming_message_service.rb | 2 +- lib/exception_list.rb | 4 ++++ lib/webhooks/trigger.rb | 2 +- 6 files changed, 9 insertions(+), 5 deletions(-) create mode 100644 lib/exception_list.rb diff --git a/app/builders/messages/facebook/message_builder.rb b/app/builders/messages/facebook/message_builder.rb index 02f519fc4..447fe3cec 100644 --- a/app/builders/messages/facebook/message_builder.rb +++ b/app/builders/messages/facebook/message_builder.rb @@ -51,7 +51,7 @@ class Messages::Facebook::MessageBuilder def attach_file(attachment, file_url) file_resource = LocalResource.new(file_url) attachment.file.attach(io: file_resource.file, filename: file_resource.filename, content_type: file_resource.encoding) - rescue Errno::ETIMEDOUT, Errno::ECONNREFUSED, SocketError => e + rescue *ExceptionList::URI_EXCEPTIONS => e Rails.logger.info "invalid url #{file_url} : #{e.message}" end diff --git a/app/controllers/api/v1/accounts/callbacks_controller.rb b/app/controllers/api/v1/accounts/callbacks_controller.rb index ff2478447..9a4cd85e8 100644 --- a/app/controllers/api/v1/accounts/callbacks_controller.rb +++ b/app/controllers/api/v1/accounts/callbacks_controller.rb @@ -81,7 +81,7 @@ class Api::V1::Accounts::CallbacksController < Api::V1::Accounts::BaseController avatar_resource = LocalResource.new(uri) facebook_inbox.avatar.attach(io: avatar_resource.file, filename: avatar_resource.tmp_filename, content_type: avatar_resource.encoding) - rescue Errno::ETIMEDOUT, Errno::ECONNREFUSED, SocketError => e + rescue *ExceptionList::URI_EXCEPTIONS => e Rails.logger.info "invalid url #{file_url} : #{e.message}" end diff --git a/app/jobs/contact_avatar_job.rb b/app/jobs/contact_avatar_job.rb index f381b63f2..9e0c7b813 100644 --- a/app/jobs/contact_avatar_job.rb +++ b/app/jobs/contact_avatar_job.rb @@ -4,7 +4,7 @@ class ContactAvatarJob < ApplicationJob def perform(contact, avatar_url) avatar_resource = LocalResource.new(avatar_url) contact.avatar.attach(io: avatar_resource.file, filename: avatar_resource.tmp_filename, content_type: avatar_resource.encoding) - rescue Errno::ETIMEDOUT, Errno::ECONNREFUSED, SocketError, NoMethodError => e + rescue *ExceptionList::URI_EXCEPTIONS, NoMethodError => e Rails.logger.info "Exception: invalid avatar url #{avatar_url} : #{e.message}" end end diff --git a/app/services/twilio/incoming_message_service.rb b/app/services/twilio/incoming_message_service.rb index d9418f61e..a52a988ae 100644 --- a/app/services/twilio/incoming_message_service.rb +++ b/app/services/twilio/incoming_message_service.rb @@ -107,7 +107,7 @@ class Twilio::IncomingMessageService ) @message.save! - rescue Errno::ETIMEDOUT, Errno::ECONNREFUSED, SocketError => e + rescue *ExceptionList::URI_EXCEPTIONS => e Rails.logger.info "invalid url #{file_url} : #{e.message}" end end diff --git a/lib/exception_list.rb b/lib/exception_list.rb new file mode 100644 index 000000000..edb5899ab --- /dev/null +++ b/lib/exception_list.rb @@ -0,0 +1,4 @@ +module ExceptionList + URI_EXCEPTIONS = [Errno::ETIMEDOUT, Errno::ECONNREFUSED, URI::InvalidURIError, Net::OpenTimeout, SocketError].freeze + REST_CLIENT_EXCEPTIONS = [RestClient::NotFound, RestClient::GatewayTimeout, RestClient::BadRequest, RestClient::MethodNotAllowed].freeze +end diff --git a/lib/webhooks/trigger.rb b/lib/webhooks/trigger.rb index 5475b75f7..c3f0f95f2 100644 --- a/lib/webhooks/trigger.rb +++ b/lib/webhooks/trigger.rb @@ -1,7 +1,7 @@ class Webhooks::Trigger def self.execute(url, payload) RestClient.post(url, payload.to_json, { content_type: :json, accept: :json }) - rescue RestClient::NotFound, RestClient::GatewayTimeout, SocketError => e + rescue *ExceptionList::REST_CLIENT_EXCEPTIONS, *ExceptionList::URI_EXCEPTIONS => e Rails.logger.info "Exception: invalid webhook url #{url} : #{e.message}" rescue StandardError => e Raven.capture_exception(e)