Permalink
Browse files

Improved error handling, b/c exception.response can be instance of Ne…

…t::HTTPServiceUnavailable also.

So, now we prevent errors like
NoMethodError: undefined method `params' for #<Net::HTTPServiceUnavailable:0xd5ba314>
  • Loading branch information...
1 parent 1e25e2b commit 2d17a381c92691ee529389917494e75e4f139e92 @romul romul committed Oct 16, 2012
Showing with 10 additions and 6 deletions.
  1. +10 −6 app/models/spree/calculator/active_shipping/base.rb
@@ -102,7 +102,7 @@ def retrieve_rates(origin, destination, packages)
return rate_hash
rescue ActiveMerchant::ActiveMerchantError => e
- if [ActiveMerchant::ResponseError, ActiveMerchant::Shipping::ResponseError].include? e.class
+ if [ActiveMerchant::ResponseError, ActiveMerchant::Shipping::ResponseError].include?(e.class) && e.response.is_a?(ActiveMerchant::Shipping::Response)
params = e.response.params
if params.has_key?("Response") && params["Response"].has_key?("Error") && params["Response"]["Error"].has_key?("ErrorDescription")
message = params["Response"]["Error"]["ErrorDescription"]
@@ -113,7 +113,7 @@ def retrieve_rates(origin, destination, packages)
message = e.message
end
else
- message = e.to_s
+ message = e.message
end
error = Spree::ShippingError.new("#{I18n.t(:shipping_error)}: #{message}")
@@ -131,13 +131,17 @@ def retrieve_timings(origin, destination, packages)
return response
end
rescue ActiveMerchant::Shipping::ResponseError => re
- params = re.response.params
- if params.has_key?("Response") && params["Response"].has_key?("Error") && params["Response"]["Error"].has_key?("ErrorDescription")
- message = params["Response"]["Error"]["ErrorDescription"]
+ if re.response.is_a?(ActiveMerchant::Shipping::Response)
+ params = re.response.params
+ if params.has_key?("Response") && params["Response"].has_key?("Error") && params["Response"]["Error"].has_key?("ErrorDescription")
+ message = params["Response"]["Error"]["ErrorDescription"]
+ else
+ message = re.message
+ end
else
message = re.message
end
-
+
error = Spree::ShippingError.new("#{I18n.t(:shipping_error)}: #{message}")
Rails.cache.write @cache_key+"-timings", error #write error to cache to prevent constant re-lookups
raise error

0 comments on commit 2d17a38

Please sign in to comment.