From 1df824454751f0a968f33dd06694bd1b31d7dd04 Mon Sep 17 00:00:00 2001 From: manu vasconcelos Date: Fri, 28 Jul 2023 14:28:28 -0300 Subject: [PATCH] start working on error handling --- app/models/twitter_client.rb | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/app/models/twitter_client.rb b/app/models/twitter_client.rb index 95d6c1f7..a59a9fa6 100644 --- a/app/models/twitter_client.rb +++ b/app/models/twitter_client.rb @@ -2,7 +2,6 @@ require 'net/http' class TwitterClient - # tweet_id = "1684310862842982400" BASE_URI = "https://api.twitter.com/2/" def self.tweet_lookup(tweet_id) @@ -38,19 +37,25 @@ def self.user_lookup_by_id(id) private def self.get(path, params) - uri = URI(URI.join(BASE_URI, path)) - uri.query = Rack::Utils.build_query(params) - - http = Net::HTTP.new(uri.host, uri.port) - http.use_ssl = true - - headers = { - "Authorization": "Bearer #{PenderConfig.get('twitter_bearer_token')}", - } - - request = Net::HTTP::Get.new(uri.request_uri, headers) - - response = http.request(request) - JSON.parse(response.body) + begin + uri = URI(URI.join(BASE_URI, path)) + uri.query = Rack::Utils.build_query(params) + + http = Net::HTTP.new(uri.host, uri.port) + http.use_ssl = true + + headers = { + "Authorization": "Bearer #{PenderConfig.get('twitter_bearer_token')}", + } + + request = Net::HTTP::Get.new(uri.request_uri, headers) + + response = http.request(request) + JSON.parse(response.body) + rescue Net::HTTPExceptions => e + raise Pender::Exception::RetryLater, "(#{response.code}) #{response.message}" + rescue JSON::ParserError => e + Rails.logger.warn level: 'WARN', message: '[Parser] Could not get `twitter` data', twitter_url: uri, error_class: error.class, response_code: response.code, response_body: response.body end + end end \ No newline at end of file