Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

snegov
Copy link

@snegov snegov commented Dec 22, 2024

This PR adds a safe_get_balance Jinja filter to handle exceptions raised by Account.get_balance(). When an exception occurs, the balance field now shows "Error" instead of causing an HTTP 500 error.

Copy link
Owner

@joshuabach joshuabach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your PR! I added to questions.

These are mainly aimed at finding out how these errors can happen, since this affects how to best handle them. Can this happenthrough normal user operation? Are they the result of a corrupted database or a bug?

Comment on lines +64 to +65
if amount is None:
return Markup('<span class="text-danger">Error</span>')
Copy link
Owner

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?

Copy link
Author

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, the safe_get_balance filter returns None as an amount.

Comment on lines +146 to +148
except GnucashException as e:
logging.warning(f"Failed to retrieve balance for account {account}: {e}")
return None
Copy link
Owner

Choose a reason for hiding this comment

The 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?

Copy link
Author

Choose a reason for hiding this comment

The 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants