From 2a7fea11e61512c02f613ded3f14c60bb0fca8d5 Mon Sep 17 00:00:00 2001 From: Matchu Date: Sat, 19 Feb 2011 22:49:13 -0500 Subject: [PATCH] handle timeout errors for pet data, swfs, pet images --- app/controllers/pets_controller.rb | 13 +++++++++++-- app/models/pet.rb | 5 ++++- app/models/pet_type.rb | 6 +++++- app/models/swf_asset.rb | 6 +++++- lib/rocketamf/remote_gateway/request.rb | 20 ++++++++++++++++---- 5 files changed, 41 insertions(+), 9 deletions(-) diff --git a/app/controllers/pets_controller.rb b/app/controllers/pets_controller.rb index bf65dc62..cebe7c03 100644 --- a/app/controllers/pets_controller.rb +++ b/app/controllers/pets_controller.rb @@ -1,6 +1,7 @@ class PetsController < ApplicationController rescue_from Pet::PetNotFound, :with => :pet_not_found - rescue_from PetType::DownloadError, SwfAsset::DownloadError, :with => :download_error + rescue_from PetType::DownloadError, SwfAsset::DownloadError, :with => :asset_download_error + rescue_from Pet::DownloadError, :with => :pet_download_error cache_sweeper :user_sweeper @@ -46,7 +47,7 @@ class PetsController < ApplicationController :status => :not_found end - def download_error(e) + def asset_download_error(e) Rails.logger.warn e.message pet_load_error :long_message => "We found the pet all right, but the " + "Neopets image server didn't respond to our download request. Maybe it's " + @@ -55,6 +56,14 @@ class PetsController < ApplicationController :status => :gateway_timeout end + def pet_download_error(e) + Rails.logger.warn e.message + pet_load_error :long_message => "Could not connect to the Neopets server " + + "to look up the pet. Maybe they're down. Try again later, maybe. Sorry!", + :short_message => 'Neopets seems down. Try again?', + :status => :gateway_timeout + end + def pet_load_error(options) respond_to do |format| format.html do diff --git a/app/models/pet.rb b/app/models/pet.rb index 8115e911..27e6e655 100644 --- a/app/models/pet.rb +++ b/app/models/pet.rb @@ -22,7 +22,9 @@ class Pet < ActiveRecord::Base if e.message == PET_NOT_FOUND_REMOTE_ERROR raise PetNotFound, "Pet #{name.inspect} does not exist" end - raise + raise DownloadError, e.message + rescue RocketAMF::RemoteGateway::ConnectionError => e + raise DownloadError, e.message end contents = OpenStruct.new(envelope.messages[0].data.body) pet_data = OpenStruct.new(contents.custom_pet) @@ -88,4 +90,5 @@ class Pet < ActiveRecord::Base end class PetNotFound < Exception;end + class DownloadError < Exception;end end diff --git a/app/models/pet_type.rb b/app/models/pet_type.rb index c449c21e..f87f07bd 100644 --- a/app/models/pet_type.rb +++ b/app/models/pet_type.rb @@ -109,7 +109,11 @@ class PetType < ActiveRecord::Base before_save do if @origin_pet cpn_uri = URI.parse sprintf(IMAGE_CPN_FORMAT, @origin_pet.name); - res = Net::HTTP.get_response(cpn_uri) + begin + res = Net::HTTP.get_response(cpn_uri) + rescue Exception => e + raise DownloadError, e.message + end unless res.is_a? Net::HTTPFound begin res.error! diff --git a/app/models/swf_asset.rb b/app/models/swf_asset.rb index 5bbdef79..b9b68ab5 100644 --- a/app/models/swf_asset.rb +++ b/app/models/swf_asset.rb @@ -82,7 +82,11 @@ class SwfAsset < ActiveRecord::Base before_create do uri = URI.parse url - response = Net::HTTP.get_response(uri) + begin + response = Net::HTTP.get_response(uri) + rescue Exception => e + raise DownloadError, e.message + end if response.is_a? Net::HTTPSuccess new_local_path = File.join(LOCAL_ASSET_DIR, local_path_within_outfit_swfs) new_local_dir = File.dirname new_local_path diff --git a/lib/rocketamf/remote_gateway/request.rb b/lib/rocketamf/remote_gateway/request.rb index 3c442f07..8747fb23 100644 --- a/lib/rocketamf/remote_gateway/request.rb +++ b/lib/rocketamf/remote_gateway/request.rb @@ -19,7 +19,11 @@ module RocketAMF else req = Net::HTTP::Post.new(uri.path) req.body = data - res = Net::HTTP.new(uri.host, uri.port).start { |http| http.request(req) } + begin + res = Net::HTTP.new(uri.host, uri.port).start { |http| http.request(req) } + rescue Exception => e + raise ConnectionError, e.message + end case res when Net::HTTPSuccess response_body = res.body @@ -30,13 +34,13 @@ module RocketAMF rescue Exception => scoped_error error = scoped_error end - raise ConnectionError, "Error connecting to gateway: #{error}" + raise ConnectionError, error.message end end begin result = RocketAMF::Envelope.new.populate_from_stream(response_body) rescue Exception => e - raise ConnectionError, "Error parsing gateway response: #{e.message}" + raise ConnectionError, e.message end first_message_data = result.messages[0].data if first_message_data.respond_to?(:[]) && first_message_data[:code] == ERROR_CODE @@ -66,7 +70,15 @@ module RocketAMF end end - class ConnectionError < RuntimeError;end + class ConnectionError < RuntimeError + def initialize(message) + @message = message + end + + def message + "Error connecting to gateway: #{@message}" + end + end class AMFError < RuntimeError DATA_KEYS = [:details, :line, :code] attr_reader *DATA_KEYS