1
0
Fork 0
forked from OpenNeo/impress

gracefully handle connection errors with neopets servers

This commit is contained in:
Emi Matchu 2011-02-19 22:09:12 -05:00
parent fa14232473
commit ea4ec0f99d
3 changed files with 27 additions and 7 deletions

View file

@ -1,5 +1,6 @@
class PetsController < ApplicationController class PetsController < ApplicationController
rescue_from Pet::PetNotFound, :with => :pet_not_found rescue_from Pet::PetNotFound, :with => :pet_not_found
rescue_from PetType::DownloadError, SwfAsset::DownloadError, :with => :download_error
cache_sweeper :user_sweeper cache_sweeper :user_sweeper
@ -40,15 +41,30 @@ class PetsController < ApplicationController
protected protected
def pet_not_found def pet_not_found
pet_load_error :long_message => 'Could not find any pet by that name. Did you spell it correctly?',
:short_message => 'Pet not found',
:status => :not_found
end
def 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 " +
"down, or maybe it's just having trouble. 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| respond_to do |format|
format.html do format.html do
path = params[:origin] || root_path path = params[:origin] || root_path
path += "?name=#{params[:name]}" path += "?name=#{params[:name]}"
redirect_to path, :alert => 'Could not find any pet by that name. Did you spell it correctly?' redirect_to path, :alert => options[:long_message]
end end
format.json do format.json do
render :text => 'Pet not found', :status => :not_found render :text => options[:short_message], :status => options[:status]
end end
end end
end end

View file

@ -114,9 +114,9 @@ class PetType < ActiveRecord::Base
begin begin
res.error! res.error!
rescue Exception => e rescue Exception => e
raise "Error loading CPN image at #{cpn_uri}: #{e.message}" raise DownloadError, "Error loading CPN image at #{cpn_uri}: #{e.message}"
else else
raise "Error loading CPN image at #{cpn_uri}. Response: #{res.inspect}" raise DownloadError, "Error loading CPN image at #{cpn_uri}. Response: #{res.inspect}"
end end
end end
new_url = res['location'] new_url = res['location']
@ -124,7 +124,7 @@ class PetType < ActiveRecord::Base
if match if match
self.image_hash = match[1] self.image_hash = match[1]
else else
raise "CPN image pointed to #{new_url}, which does not match CP image format" raise DownloadError, "CPN image pointed to #{new_url}, which does not match CP image format"
end end
end end
end end
@ -148,4 +148,6 @@ class PetType < ActiveRecord::Base
end end
end end
end end
class DownloadError < Exception;end
end end

View file

@ -95,9 +95,9 @@ class SwfAsset < ActiveRecord::Base
begin begin
response.error! response.error!
rescue Exception => e rescue Exception => e
raise "Error loading SWF at #{url}: #{e.message}" raise DownloadError, "Error loading SWF at #{url}: #{e.message}"
else else
raise "Error loading SWF at #{url}. Response: #{response.inspect}" raise DownloadError, "Error loading SWF at #{url}. Response: #{response.inspect}"
end end
end end
end end
@ -108,6 +108,8 @@ class SwfAsset < ActiveRecord::Base
self.body_id = 0 if !self.body_specific? || (!self.new_record? && self.body_id_changed?) self.body_id = 0 if !self.body_specific? || (!self.new_record? && self.body_id_changed?)
end end
class DownloadError < Exception;end
private private
def local_path_within_outfit_swfs def local_path_within_outfit_swfs