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
Say which way a provider failure was resolved
Damian Legawiecc1cd3ea5e9dc
diff --git a/spree/core/app/models/concerns/spree/provider_failure_policy.rb b/spree/core/app/models/concerns/spree/provider_failure_policy.rbindex f430ae940a..bee918a9cc 100644--- a/spree/core/app/models/concerns/spree/provider_failure_policy.rb+++ b/spree/core/app/models/concerns/spree/provider_failure_policy.rb@@ -15,23 +15,29 @@ module SpreeDEFAULT_PRICING_POLICY = 'strict'.freezeDEFAULT_INVENTORY_POLICY = 'fallback'.freeze- # Records that a provider could not answer and Spree used its own figure- # instead. Emitted from one place because the payload keys are public API+ # Records that a provider could not answer, and what Spree did about it.+ # Emitted from one place because the payload keys are public API# (docs/plans/6.0-opentelemetry.md) and must stay PII-safe — an error# message can carry order or customer detail, so only its class is sent.#+ # `policy` distinguishes the two outcomes: a store on `fallback` traded on+ # Spree's own figure, while a `strict` store refused. Alerting cannot treat+ # a silent substitution and a refused sale as the same event.+ ## @param kind [String] 'pricing' or 'inventory'# @param provider [String, nil] the provider's registry key# @param store [Spree::Store, nil]# @param error [StandardError]+ # @param policy [String] the policy that decided the outcome# @return [void]- def self.report_fallback(kind:, provider:, store:, error:)+ def self.report_fallback(kind:, provider:, store:, error:, policy:)ActiveSupport::Notifications.instrument('provider.fallback.spree',provider: provider,kind: kind,store_id: store&.id,- reason: error.class.name+ reason: error.class.name,+ policy: policy)endenddiff --git a/spree/core/app/services/spree/carts/check_availability.rb b/spree/core/app/services/spree/carts/check_availability.rbindex 861df8f865..3c1a0a69de 100644--- a/spree/core/app/services/spree/carts/check_availability.rb+++ b/spree/core/app/services/spree/carts/check_availability.rb@@ -54,15 +54,17 @@ module Spreeraise error if error.is_a?(Spree::ServiceModule::ResultError)store = cart&.store+ policy = store&.inventory_failure_policySpree::ProviderFailurePolicy.report_fallback(kind: 'inventory', provider: store&.inventory_provider_instance&.class&.key,- store: store, error: error+ store: store, error: error,+ policy: policy || Spree::ProviderFailurePolicy::DEFAULT_INVENTORY_POLICY)# Strict stores would rather refuse than sell on a figure they cannot# confirm; the default is to trust the local snapshot, since an# oversell is recoverable and a blocked checkout is not.- return failure(cart, error.message) if store&.inventory_failure_policy == 'strict'+ return failure(cart, error.message) if policy == 'strict'success([])enddiff --git a/spree/core/lib/spree/core/pricing/price_resolution.rb b/spree/core/lib/spree/core/pricing/price_resolution.rbindex f6723e9162..5c6887426c 100644--- a/spree/core/lib/spree/core/pricing/price_resolution.rb+++ b/spree/core/lib/spree/core/pricing/price_resolution.rb@@ -136,11 +136,13 @@ module Spree# exception, so the workflow can fail the step; a catalog read (no# order) gets nil, which the serializers already render as "no price".def handle_failure(error)+ policy = store&.pricing_failure_policySpree::ProviderFailurePolicy.report_fallback(- kind: 'pricing', provider: provider.class.key, store: store, error: error+ kind: 'pricing', provider: provider.class.key, store: store, error: error,+ policy: policy || Spree::ProviderFailurePolicy::DEFAULT_PRICING_POLICY)- return internal_price unless store&.pricing_failure_policy == 'strict'+ return internal_price unless policy == 'strict'return nil if context.order.blank?raise ProviderUnavailable.new(error.message, provider_key: provider.class.key)