chore: Handle exceptions on external URL calls (#1334)

This commit is contained in:
Sojan Jose 2020-10-11 20:22:21 +05:30 committed by GitHub
parent 7d409321e9
commit 59bee66e63
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 9 additions and 5 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

4
lib/exception_list.rb Normal file
View file

@ -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

View file

@ -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)