The personal finance app for everyone (by everyone)
Ruby
fix(enable-banking): tolerate any 422 on PDNG fetch (#1805) (#1889)
Rene Arredondo0e1b3f839611
diff --git a/app/models/enable_banking_item/importer.rb b/app/models/enable_banking_item/importer.rbindex d08f0c5f56..bf1bb48cd4 100644--- a/app/models/enable_banking_item/importer.rb+++ b/app/models/enable_banking_item/importer.rb@@ -252,7 +252,12 @@ class EnableBankingItem::Importerpending_transactions = []if include_pending- # Also fetch pending transactions (visible for 1-3 days before they become BOOK) if setting is enabled+ # Also fetch pending transactions (visible for 1-3 days before they become BOOK) if setting is enabled.+ # The BOOK fetch above used the same date_from/date_to and succeeded, so any 422 raised here is+ # necessarily about the transaction_status param. Different ASPSPs reject it with different bodies+ # (e.g. ImaginV2 returns WRONG_REQUEST_PARAMETERS; others mention "transactionStatus" verbatim),+ # so we treat every validation_error on PDNG as "ASPSP doesn't support pending" and continue with+ # the booked transactions only. (Issue #1805)beginpending_transactions = fetch_paginated_transactions(enable_banking_account,@@ -261,8 +266,9 @@ class EnableBankingItem::Importerpsu_headers: enable_banking_item.build_psu_headers)rescue Provider::EnableBanking::EnableBankingError => e- raise unless e.error_type == :validation_error && e.message.include?("transactionStatus")- Rails.logger.warn "EnableBankingItem::Importer - ASPSP does not support PDNG transaction status for account #{enable_banking_account.uid}, skipping pending transactions. API error: #{e.message}"+ raise unless e.error_type == :validation_error+ api_error = e.response_data.is_a?(Hash) ? (e.response_data[:error] || e.response_data["error"]) : nil+ Rails.logger.warn "EnableBankingItem::Importer - ASPSP does not support PDNG transaction status for account #{enable_banking_account.uid}, skipping pending transactions. API error: #{api_error || e.message}"endenddiff --git a/test/models/enable_banking_item/importer_error_handling_test.rb b/test/models/enable_banking_item/importer_error_handling_test.rbindex 006ce127a0..5f2e0b73e6 100644--- a/test/models/enable_banking_item/importer_error_handling_test.rb+++ b/test/models/enable_banking_item/importer_error_handling_test.rb@@ -112,18 +112,40 @@ class EnableBankingItem::ImporterErrorHandlingTest < ActiveSupport::TestCaseassert result[:success]end- test "fetch_and_store_transactions fails when validation error is unrelated to transactionStatus" do+ # Regression for #1805: ImaginV2 (and other Enable Banking connectors) reject PDNG with+ # a generic WRONG_REQUEST_PARAMETERS body whose message does not mention "transactionStatus".+ # The sync must still succeed and import booked transactions.+ test "fetch_and_store_transactions succeeds when ASPSP rejects PDNG with WRONG_REQUEST_PARAMETERS" doenable_banking_account = EnableBankingAccount.new(uid: "test_uid")@importer.stubs(:determine_sync_start_date).returns(Date.today)@importer.stubs(:include_pending?).returns(true)- date_error = Provider::EnableBanking::EnableBankingError.new(- "Validation error from Enable Banking API: {\"message\":\"Invalid date_from format\"}",- :validation_error+ imagin_error = Provider::EnableBanking::EnableBankingError.new(+ "Validation error from Enable Banking API: {\"error\":\"WRONG_REQUEST_PARAMETERS\"}",+ :validation_error,+ response_data: { error: "WRONG_REQUEST_PARAMETERS" }+ )++ @importer.stubs(:fetch_paginated_transactions).with(enable_banking_account, has_entries(transaction_status: "BOOK")).returns([])+ @importer.stubs(:fetch_paginated_transactions).with(enable_banking_account, has_entries(transaction_status: "PDNG")).raises(imagin_error)++ result = @importer.send(:fetch_and_store_transactions, enable_banking_account)++ assert result[:success]+ end++ test "fetch_and_store_transactions propagates non-validation EnableBankingError from PDNG fetch" do+ enable_banking_account = EnableBankingAccount.new(uid: "test_uid")+ @importer.stubs(:determine_sync_start_date).returns(Date.today)+ @importer.stubs(:include_pending?).returns(true)++ rate_limit_error = Provider::EnableBanking::EnableBankingError.new(+ "Rate limit exceeded. Please try again later.",+ :rate_limited)@importer.stubs(:fetch_paginated_transactions).with(enable_banking_account, has_entries(transaction_status: "BOOK")).returns([])- @importer.stubs(:fetch_paginated_transactions).with(enable_banking_account, has_entries(transaction_status: "PDNG")).raises(date_error)+ @importer.stubs(:fetch_paginated_transactions).with(enable_banking_account, has_entries(transaction_status: "PDNG")).raises(rate_limit_error)result = @importer.send(:fetch_and_store_transactions, enable_banking_account)