Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActiveUtils::ConnectionError #273

Open
jkelleyj opened this issue Aug 29, 2018 · 1 comment
Open

ActiveUtils::ConnectionError #273

jkelleyj opened this issue Aug 29, 2018 · 1 comment

Comments

@jkelleyj
Copy link

Whenever the USPS rate api goes down (which is a surprisingly regular occurrence https://www.shippingapimonitor.com/history.html?api=usps), ActiveUtils will raise an ActiveUtils::ConnectionError that we don't handle today. Most other Active Shipping response errors are handled and reraised, but this one bubble up and can prevent checkout.

I'm patching our installation with the following extension and wonder if you guys think it would be worth submitting a PR.

module Spree
  module Calculator::Shipping
    module ActiveShipping
      module BaseExtensions

        # Catch low lying ActiveUtils error from ActiveShipping - not handled by spree extension.
        # This happens when the USPS api goes down. Reraise as ShippingError for proper handling.
        def retrieve_rates(origin, destination, shipment_packages)
          begin
            super
          rescue ::ActiveUtils::ConnectionError => e
            error = Spree::ShippingError.new("#{I18n.t(:shipping_error)}: It looks like we are unable to lookup shipping rates for this service right now.")
            Rails.cache.write @cache_key, error, expires_in: 10.minutes
            raise error
          end
        end
      end
    end
  end
end

Since USPS Base has it's own retrieve rates method, we need to prepend on it as well as the overall ActiveShipping Base.

Spree::Calculator::Shipping::Usps::Base.class_eval do
  prepend Spree::Calculator::Shipping::ActiveShipping::BaseExtensions
end
Spree::Calculator::Shipping::ActiveShipping::Base.class_eval do
  prepend Spree::Calculator::Shipping::ActiveShipping::BaseExtensions
end

Anybody see any issues or better approaches to solve this?

@jkelleyj
Copy link
Author

I followed the pattern in the retrieve_rates method to set a cache value to prevent frequent retries on this order. I added a 10 minute expiration so the order could get rates in the future if they do not complete now, however we could easily discard this optimization as it breaks from the pattern in the base classes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant