Report a failed provider sync to Sentry once

JA
6d ago
+51-1

Summary

Provider kit Track B, K2: when a sync fails, the kit's syncer (Brex, Lunch Flow) and Mercury's re-raise their safe SafeSyncError with cause: nil, as Up, Akahu, Fio and Monobank already do. The syncer has already sent the raw error to Sentry, tagged with the connection id; until now Sync#report_error then sent the SafeSyncError with the raw error chained as its cause, so each failure reached Sentry twice, and the "safe" error still carried the raw message.

Part of jaysbeekay/sure#670 (Track B, K2, owner decision of 2026-11-16). Stacked on B7a (jaysbeekay/sure#731) in fork stack #687, so it changes Brex and Lunch Flow in one place (the kit syncer) and Mercury in its own syncer; Mercury's later move onto the kit stays a pure move.

Upstream draft PR: https://github.com/we-promise/sure/pull/5243 (same branch, same commit), in native stack we-promise/sure#5268 on refactor/provider-kit.

Behaviour delta (Brex, Lunch Flow, Mercury)

A sync that raisesBeforeAfter
Sentry events carrying the raw errortwo: the syncer's own (tagged <key>_item_id), and Sync#report_error's SafeSyncError, which chained the raw error as its causeone: the syncer's own
Sync#report_error's eventSafeSyncError with the raw error as its causeSafeSyncError with no cause
The sync's stored error and statsthe safe translated messageunchanged

What changed

  • ProviderItem::Syncer#perform_sync and MercuryItem::Syncer#perform_sync: raise SafeSyncError.new(safe_message), cause: nil.

Demo data

No demo data needed.

Migration notes

No migration.

Verification

  • Tests (fail-first): "a failed sync reaches Sentry once, without the safe error carrying it as its cause", in the kit's ProviderItemModelLifecycleTests (so it runs for Brex and Lunch Flow) and in MercuryItem::SyncerTest. It records every Sentry.capture_exception during Sync#perform (a Minitest stub, not a Mocha matcher), asserts exactly one SafeSyncError was reported and that it has no cause, and walks every reported exception's cause chain to assert the raw error appears exactly once. Before the change all three fail (Expected #<StandardError: raw provider failure> to be nil).
  • Mutations, each caught: the kit keeping the cause (Brex and Lunch Flow fail); the kit no longer reporting the raw error itself (count 0); the kit re-raising the raw error (no SafeSyncError reported); Mercury keeping the cause.
  • Brex, Lunch Flow and Mercury model tests and ProviderItemTest: 135 runs, 0 failures.
  • bin/rails test (full, at the K2 commit): 16,340 runs, 0 failures; the same 8 ReleaseNotesWorkflowTest errors (no node in the test image).
  • bin/rubocop on changed files: no offenses. bin/brakeman: no warnings.
  • Size: non-test +4 / -2.
  • Checked against upstream AGENTS.md (blob 1a5dbebd, 2026-11-17): no deviations.

Blast radius

  • Only Sync#perform rescues these errors (update(error: e.message), report_error(e)); neither reads cause.
  • Siblings (grep "raise.*SafeSyncError" app/models): Up, Akahu, Fio and Monobank already raise with cause: nil. Wise's syncer (wise_item/syncer.rb:73) has the same double report and is left for its own change: Wise is outside the kit (#670 lists it among the bespoke providers).

Review notes

A code review of the diff before commit raised nine points. Taken: the test now asserts a SafeSyncError was reported before checking its cause (it passed vacuously if none was), records calls with a Minitest stub instead of inside a Mocha with block, and walks the full cause chain in Mercury's test too. Noted, not changed:

  • A failure is still two Sentry events: the raw error (with backtrace, tagged <key>_item_id) from the syncer, and Sync#report_error's generic SafeSyncError (tagged sync_id). What changes is that the raw error is reported once rather than twice. This is the same shape as Up, Fio and Monobank. Dropping either event (Akahu reports only the safe one) would be a further change, and the two events are no longer linked through the cause chain; matching them needs the timestamp or the item id. Worth a follow-up if support wants one event per failure.
  • Fixing it once in Sync#report_error instead of per syncer: a broader change than the decision; the kit syncer is the one place for providers on the kit.
  • Wise's sibling: left for its own change (see Blast radius).