Skip to content

Commit

Permalink
Add context to plaid sync errors
Browse files Browse the repository at this point in the history
  • Loading branch information
zachgoll committed Dec 2, 2024
1 parent d73e7ea commit 9ec94cd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
6 changes: 5 additions & 1 deletion app/models/family.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ def sync_data(start_date: nil)
account.sync_data(start_date: start_date)
end

plaid_data = []

plaid_items.each do |plaid_item|
plaid_item.sync_data(start_date: start_date)
plaid_data << plaid_item.sync_data(start_date: start_date)
end

plaid_data
end

def post_sync
Expand Down
11 changes: 10 additions & 1 deletion app/models/plaid_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ def create_from_public_token(token, item_name:)
def sync_data(start_date: nil)
update!(last_synced_at: Time.current)

fetch_and_load_plaid_data
plaid_data = fetch_and_load_plaid_data

accounts.each do |account|
account.sync_data(start_date: start_date)
end

plaid_data
end

def post_sync
Expand All @@ -53,10 +55,12 @@ def destroy_later

private
def fetch_and_load_plaid_data
data = {}
item = plaid_provider.get_item(access_token).item
update!(available_products: item.available_products, billed_products: item.billed_products)

fetched_accounts = plaid_provider.get_item_accounts(self).accounts
data[:accounts] = fetched_accounts || []

internal_plaid_accounts = fetched_accounts.map do |account|
internal_plaid_account = plaid_accounts.find_or_create_from_plaid_data!(account, family)
Expand All @@ -65,6 +69,7 @@ def fetch_and_load_plaid_data
end

fetched_transactions = safe_fetch_plaid_data(:get_item_transactions)
data[:transactions] = fetched_transactions || []

if fetched_transactions
transaction do
Expand All @@ -81,6 +86,7 @@ def fetch_and_load_plaid_data
end

fetched_investments = safe_fetch_plaid_data(:get_item_investments)
data[:investments] = fetched_investments || []

if fetched_investments
transaction do
Expand All @@ -95,6 +101,7 @@ def fetch_and_load_plaid_data
end

fetched_liabilities = safe_fetch_plaid_data(:get_item_liabilities)
data[:liabilities] = fetched_liabilities || []

if fetched_liabilities
transaction do
Expand All @@ -109,6 +116,8 @@ def fetch_and_load_plaid_data
end
end
end

data
end

def safe_fetch_plaid_data(method)
Expand Down
8 changes: 6 additions & 2 deletions app/models/sync.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ def perform
start!

begin
syncable.sync_data(start_date: start_date)
data = syncable.sync_data(start_date: start_date)
update!(data: data) if data
complete!
rescue StandardError => error
fail! error
Expand All @@ -29,7 +30,10 @@ def complete!
end

def fail!(error)
Sentry.capture_exception(error)
Sentry.capture_exception(error) do |scope|
scope.set_context("sync", { id: id })
end

update! status: :failed, error: error.message, last_ran_at: Time.current
end
end

0 comments on commit 9ec94cd

Please sign in to comment.