Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Harden the EasyPost provider against API failures
Damian Legawiec79a2956dc269
diff --git a/docs/v6/developer/how-to/custom-delivery-rate-provider.mdx b/docs/v6/developer/how-to/custom-delivery-rate-provider.mdxindex f26c991d6c..31be6ed454 100644--- a/docs/v6/developer/how-to/custom-delivery-rate-provider.mdx+++ b/docs/v6/developer/how-to/custom-delivery-rate-provider.mdx@@ -92,6 +92,8 @@ module SpreeAcmeCarrierend```+**Never let `estimate` raise.** It runs inside checkout's rate refresh, so an exception breaks the whole delivery step for the customer. Rescue your carrier's errors, report them (`Rails.error.report`), and return `nil` — the method disappears from the options while everything else keeps working.+**Returning `nil` hides the delivery method** for that package. This is the same contract calculators follow, and it is how you express "this carrier does not serve this destination" — the method simply does not appear at checkout rather than appearing at a wrong price.`cost` is pre-tax and pre-VAT. Spree applies the gross-up and resolves the tax rate afterwards, exactly as it does for calculator output, so you never handle tax yourself.diff --git a/spree/easypost/README.md b/spree/easypost/README.mdindex 9ccc333daf..3a82748c81 100644--- a/spree/easypost/README.md+++ b/spree/easypost/README.md@@ -31,3 +31,9 @@ bundle exec rspec```Specs stub the EasyPost client — no network calls, no API key needed.++To verify the real API contract (the exact calls the provider makes), run the live smoke check with a test key:++```bash+EASYPOST_TEST_API_KEY=EZTK... bin/smoke+```diff --git a/spree/easypost/app/models/spree_easypost/delivery_rate_provider.rb b/spree/easypost/app/models/spree_easypost/delivery_rate_provider.rbindex d8e38ea44b..8018d3b6db 100644--- a/spree/easypost/app/models/spree_easypost/delivery_rate_provider.rb+++ b/spree/easypost/app/models/spree_easypost/delivery_rate_provider.rb@@ -8,13 +8,22 @@ module SpreeEasyPost'SpreeEasyPost::Integration'end+ # A rating failure must never break checkout: any API error suppresses+ # this method (the customer still sees calculator-priced options) and is+ # reported for observability rather than raised into the rate refresh.+ ## @param package [Spree::Stock::Package]# @return [Spree::DeliveryRateProvider::Estimate, nil]def estimate(package)return if integration.nil?return if package.order.ship_address.nil?- rate = matching_rate(package)+ rate = begin+ matching_rate(package)+ rescue StandardError => e+ Rails.error.report(e, context: { delivery_method_id: delivery_method.id }, source: 'spree_easypost.rating')+ nil+ endreturn if rate.nil?Spree::DeliveryRateProvider::Estimate.new(diff --git a/spree/easypost/app/models/spree_easypost/integration.rb b/spree/easypost/app/models/spree_easypost/integration.rbindex b93cdee972..adc6ebcfb6 100644--- a/spree/easypost/app/models/spree_easypost/integration.rb+++ b/spree/easypost/app/models/spree_easypost/integration.rb@@ -24,10 +24,12 @@ module SpreeEasyPost# A cheap authenticated read: fails with 401 on a bad key without# creating anything on the account.+ # Rescues broadly, not just SDK errors — a DNS failure or timeout must+ # surface as a clean activation error, never a 500.def can_connect?client.carrier_account.alltrue- rescue EasyPost::Errors::EasyPostError => e+ rescue StandardError => eself.connection_error_message = e.messagefalseenddiff --git a/spree/easypost/bin/smoke b/spree/easypost/bin/smokenew file mode 100755index 0000000000..c69809a352--- /dev/null+++ b/spree/easypost/bin/smoke@@ -0,0 +1,26 @@+#!/usr/bin/env ruby+# Live sanity check against the EasyPost test API — verifies the exact+# shipment.create call shape the DeliveryRateProvider uses, end to end.+#+# EASYPOST_TEST_API_KEY=EZTK... bin/smoke++require 'bundler/setup'+require 'easypost'++client = EasyPost::Client.new(api_key: ENV.fetch('EASYPOST_TEST_API_KEY'))++puts '-> carrier_account.all (the can_connect? check)'+client.carrier_account.all+puts ' ok'++puts '-> shipment.create (the estimate call)'+shipment = client.shipment.create(+ from_address: { street1: '417 Montgomery St', city: 'San Francisco', state: 'CA', zip: '94104', country: 'US' },+ to_address: { street1: '1 E 161 St', city: 'Bronx', state: 'NY', zip: '10451', country: 'US' },+ parcel: { weight: 16 }+)++shipment.rates.each do |rate|+ puts format(' %-8s %-20s %8s delivery: %s (%s)', rate.carrier, rate.service, rate.rate, rate.delivery_date || '—', rate.id)+end+puts shipment.rates.any? ? 'ok: rates returned with the fields the provider reads' : 'WARNING: no rates — check carrier accounts on this test key'diff --git a/spree/easypost/spec/models/spree_easypost/delivery_rate_provider_spec.rb b/spree/easypost/spec/models/spree_easypost/delivery_rate_provider_spec.rbindex 8150ff63eb..dcabc317b9 100644--- a/spree/easypost/spec/models/spree_easypost/delivery_rate_provider_spec.rb+++ b/spree/easypost/spec/models/spree_easypost/delivery_rate_provider_spec.rb@@ -60,6 +60,16 @@ RSpec.describe SpreeEasyPost::DeliveryRateProvider doexpect(provider.estimate(package)).to be_nilend+ # A carrier outage must degrade to "method not offered", never break+ # the rate refresh.+ it 'suppresses the method and reports when the API call fails' do+ allow(shipment_service).to receive(:create).and_raise(StandardError.new('timeout'))+ allow(Rails.error).to receive(:report)++ expect(provider.estimate(package)).to be_nil+ expect(Rails.error).to have_received(:report)+ end+it 'suppresses the method when the integration is not connected' doallow(provider).to receive(:integration).and_return(nil)