Skip to content

Commit

Permalink
feat: print culprit row if exception raises
Browse files Browse the repository at this point in the history
  • Loading branch information
wzyboy authored and dnicolodi committed Jan 24, 2025
1 parent 72fb933 commit 41d13f2
Showing 1 changed file with 34 additions and 29 deletions.
63 changes: 34 additions & 29 deletions beangulp/importers/csvbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import decimal
from enum import Enum
import re
import sys

from collections import defaultdict
from itertools import islice
Expand Down Expand Up @@ -303,35 +304,39 @@ def extract(self, filepath, existing):
offset = int(self.skiplines) + bool(self.names) + 1

for lineno, row in enumerate(self.read(filepath), offset):
# Skip empty lines.
if not row:
continue

tag = getattr(row, 'tag', None)
tags = {tag} if tag else EMPTY

link = getattr(row, 'link', None)
links = {link} if link else EMPTY

# This looks like an exercise in defensive programming
# gone too far, but we do not want to depend on any field
# being defined other than the essential ones.
flag = getattr(row, 'flag', self.flag)
payee = getattr(row, 'payee', None)
account = getattr(row, 'account', default_account)
currency = getattr(row, 'currency', self.currency)
units = data.Amount(row.amount, currency)

# Create a transaction.
txn = data.Transaction(self.metadata(filepath, lineno, row),
row.date, flag, payee, row.narration, tags, links, [
data.Posting(account, units, None, None, None, None),
])

# Apply user processing to the transaction.
txn = self.finalize(txn, row)
if txn is None:
continue
try:
# Skip empty lines.
if not row:
continue

tag = getattr(row, 'tag', None)
tags = {tag} if tag else EMPTY

link = getattr(row, 'link', None)
links = {link} if link else EMPTY

# This looks like an exercise in defensive programming
# gone too far, but we do not want to depend on any field
# being defined other than the essential ones.
flag = getattr(row, 'flag', self.flag)
payee = getattr(row, 'payee', None)
account = getattr(row, 'account', default_account)
currency = getattr(row, 'currency', self.currency)
units = data.Amount(row.amount, currency)

# Create a transaction.
txn = data.Transaction(self.metadata(filepath, lineno, row),
row.date, flag, payee, row.narration, tags, links, [
data.Posting(account, units, None, None, None, None),
])

# Apply user processing to the transaction.
txn = self.finalize(txn, row)
if txn is None:
continue
except Exception:
print(f'Error processing file {filepath}, line {lineno}: {row!r}', file=sys.stderr)
raise

# Add the transaction to the output list.
entries.append(txn)
Expand Down

0 comments on commit 41d13f2

Please sign in to comment.