-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle exceptions when retrieving account balances #68
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
"""Utilities for templates.""" | ||
import logging | ||
import re | ||
from urllib.parse import quote_plus | ||
from itertools import islice, accumulate | ||
|
@@ -8,6 +9,7 @@ | |
from babel import numbers | ||
from markupsafe import Markup, escape | ||
from jinja2 import Environment, BaseLoader, pass_eval_context | ||
from piecash import GnucashException | ||
|
||
|
||
def safe_display_string(string): | ||
|
@@ -59,6 +61,9 @@ def money(eval_ctx, amount, commodity): | |
:returns: HTML snippet | ||
|
||
""" | ||
if amount is None: | ||
return Markup('<span class="text-danger">Error</span>') | ||
|
||
if numbers.get_currency_symbol(commodity.mnemonic) != commodity.mnemonic: | ||
value = numbers.format_currency(amount, commodity.mnemonic) | ||
else: | ||
|
@@ -132,3 +137,12 @@ def contra_splits(split): | |
def nth(iterable, n, default=None): | ||
"Returns the nth item or a default value" | ||
return next(islice(iterable, n, None), default) | ||
|
||
|
||
def safe_get_balance(account): | ||
"""Get balance of account, but catch piecash exceptions.""" | ||
try: | ||
return account.get_balance() | ||
except GnucashException as e: | ||
logging.warning(f"Failed to retrieve balance for account {account}: {e}") | ||
return None | ||
Comment on lines
+146
to
+148
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar question: What are the circumstances that lead to an exception in this case. Invalid database? Can this happen when using simply the GnuCash Desktop-App and gnucash_web? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Piecash raises this exception when there is no direct exchange rate between two currencies. This can occur in legitimate scenarios and is not necessarily due to an invalid database. In my case, I had a EUR account with a USD account as its parent. Neither of these currencies is the default currency in my GnuCash book; the default is CAD. As a result, I only had conversion rates for USD-CAD and EUR-CAD, but not for USD-EUR. When attempting to read this database in gnucash-web, the absence of a direct exchange rate caused an unhandled exception, resulting in an HTTP 500 error. This database was created entirely using the GnuCash desktop app and has not been modified by any other tool. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In which cases can the amount be None? Is it an invalid database then? Do you know how one arrives at such a state?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If a
GnucashException
was raised, thesafe_get_balance
filter returns None as an amount.