handle timeout errors for pet data, swfs, pet images

This commit is contained in:
Emi Matchu 2011-02-19 22:49:13 -05:00
parent ea4ec0f99d
commit 2a7fea11e6
5 changed files with 41 additions and 9 deletions

View file

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

View file

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

View file

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

View file

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

View file

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