The personal finance app for everyone (by everyone)
Ruby
Keep the raw Wise and Brex errors off the safe sync error (#5484)
jaysbeekaybf354d6af445
diff --git a/app/models/brex_item/syncer.rb b/app/models/brex_item/syncer.rbindex 3e5de1686e..0388813ed2 100644--- a/app/models/brex_item/syncer.rb+++ b/app/models/brex_item/syncer.rb@@ -79,7 +79,8 @@ class BrexItem::Syncerscope.set_tags(brex_item_id: brex_item.id)endcollect_health_stats(sync, errors: [ { message: safe_message, category: "sync_error" } ])- raise SafeSyncError, safe_message+ # cause: nil keeps the raw error, already reported above, off Sync's own report.+ raise SafeSyncError.new(safe_message), cause: nilenddef perform_post_syncdiff --git a/app/models/wise_item/syncer.rb b/app/models/wise_item/syncer.rbindex e890fb97c1..06075e4d01 100644--- a/app/models/wise_item/syncer.rb+++ b/app/models/wise_item/syncer.rb@@ -70,7 +70,8 @@ class WiseItem::SyncerRails.logger.error Array(e.backtrace).first(10).join("\n")Sentry.capture_exception(e) { |s| s.set_tags(wise_item_id: wise_item.id) }collect_health_stats(sync, errors: [ { message: safe_message, category: "sync_error" } ])- raise SafeSyncError, safe_message+ # cause: nil keeps the raw error, already reported above, off Sync's own report.+ raise SafeSyncError.new(safe_message), cause: nilenddef perform_post_syncdiff --git a/test/models/brex_item/syncer_test.rb b/test/models/brex_item/syncer_test.rbindex e5c5bbe555..5c02286023 100644--- a/test/models/brex_item/syncer_test.rb+++ b/test/models/brex_item/syncer_test.rb@@ -17,6 +17,23 @@ class BrexItem::SyncerTest < ActiveSupport::TestCase@syncer.perform_sync(sync)end+ test "a failed sync reaches Sentry once, without the safe error carrying it as its cause" do+ error = StandardError.new("raw provider failure")+ BrexItem.any_instance.stubs(:import_latest_brex_data).raises(error)+ sync = Sync.create!(syncable: @brex_item)+ captured = []++ Sentry.stub(:capture_exception, ->(exception, *) { captured << exception }) do+ sync.perform+ end++ safe_errors = captured.grep(BrexItem::Syncer::SafeSyncError)+ assert_equal 1, safe_errors.size+ assert_nil safe_errors.first.cause+ assert_equal 1, captured.flat_map { |exception| exception_chain(exception) }.count { |exception| exception.equal?(error) }+ assert_equal I18n.t("brex_items.syncer.failed"), sync.reload.error+ end+test "records localized setup status text and counts" dowindow_start_date = Date.new(2026, 2, 1)sync = recording_sync(window_start_date: window_start_date)@@ -129,6 +146,15 @@ class BrexItem::SyncerTest < ActiveSupport::TestCaseprivate+ def exception_chain(exception)+ chain = []+ while exception+ chain << exception+ exception = exception.cause+ end+ chain+ end+def mock_sync(window_start_date:)sync = mock("sync")sync.stubs(:respond_to?).with(:status_text).returns(true)diff --git a/test/models/wise_item/syncer_test.rb b/test/models/wise_item/syncer_test.rbindex 2e192fe490..1f9fbe1806 100644--- a/test/models/wise_item/syncer_test.rb+++ b/test/models/wise_item/syncer_test.rb@@ -16,6 +16,24 @@ class WiseItem::SyncerTest < ActiveSupport::TestCaseassert wise_item.reload.requires_update?end+ test "a failed sync reaches Sentry once, without the safe error carrying it as its cause" do+ wise_item = wise_items(:one)+ error = StandardError.new("raw provider failure")+ WiseItem.any_instance.stubs(:import_latest_wise_data).raises(error)+ sync = Sync.create!(syncable: wise_item)+ captured = []++ Sentry.stub(:capture_exception, ->(exception, *) { captured << exception }) do+ sync.perform+ end++ safe_errors = captured.grep(WiseItem::Syncer::SafeSyncError)+ assert_equal 1, safe_errors.size+ assert_nil safe_errors.first.cause+ assert_equal 1, captured.flat_map { |exception| exception_chain(exception) }.count { |exception| exception.equal?(error) }+ assert_equal I18n.t("wise_items.syncer.failed"), sync.reload.error+ end+# A failed fetch completed the sync with no error anywhere. It still# completes, but the error is in the sync stats the Wise card shows, and# processing is told the import was incomplete.@@ -71,6 +89,15 @@ class WiseItem::SyncerTest < ActiveSupport::TestCaseprivate+ def exception_chain(exception)+ chain = []+ while exception+ chain << exception+ exception = exception.cause+ end+ chain+ end+def linked_wise_itemwise_item = wise_items(:one)account = Account.create!(family: wise_item.family, name: "Wise EUR", balance: 0, currency: "EUR",