On Wed, 9 Dec 2020 at 01:55, James Cook <[email protected]> wrote:

> I've noticed that sometimes when plugin code triggers a runtime error, I
> don't get a stack trace. Here's a made-up minimal-ish example:
>
> from beancount.core import amount, data, number
> __plugins__ = ("foo",)
> def foo(entries, _options):
>     return [data.Transaction(
>         flag = "*",
>         payee = None,
>         narration = "",
>         tags = "",
>         links = set(),
>         postings = [data.Posting("Equity:Foo",
> amount.Amount(number.D("10"), "CAD") + "X", None, None, None, None)]
>     )], ()
>
> If I run bean-check on a file that imports that plugin, I just get this
> output:
>
> falsifian angel beancount $ PYTHONPATH=./plugins bean-check main.beancount
>
> <load>:0:       Error importing "foo": can only concatenate tuple (not
> "str") to tuple
>
> On the other hand, if I replace the body of foo() with 'raise
> BaseException("error")', I get a nice stack trace:
>
> falsifian angel beancount $ PYTHONPATH=./plugins bean-check main.beancount
>
> Traceback (most recent call last):
>   File "/home/falsifian/.local/bin/bean-check", line 11, in <module>
>     load_entry_point('beancount==2.3.3', 'console_scripts', 'bean-check')()
>   File
> "/home/falsifian/.local/lib/python3.8/site-packages/beancount/scripts/check.py",
> line 49, in main
>     entries, errors, _ = loader.load_file(
>   File
> "/home/falsifian/.local/lib/python3.8/site-packages/beancount/loader.py",
> line 89, in load_file
>     entries, errors, options_map = _load_file(
>   File
> "/home/falsifian/.local/lib/python3.8/site-packages/beancount/loader.py",
> line 214, in wrapped
>     result = function(toplevel_filename, *args, **kw)
>   File
> "/home/falsifian/.local/lib/python3.8/site-packages/beancount/loader.py",
> line 255, in _uncached_loa
> d_file
>     return _load([(filename, True)], *args, **kw)
>   File
> "/home/falsifian/.local/lib/python3.8/site-packages/beancount/loader.py",
> line 510, in _load
>     entries, errors = run_transformations(entries, parse_errors,
> options_map,
>   File
> "/home/falsifian/.local/lib/python3.8/site-packages/beancount/loader.py",
> line 588, in run_transform
> ations
>     entries, plugin_errors = callback(entries, options_map)
>   File
> "/home/falsifian/w/colin_and_james/accounting/beancount/plugins/foo.py",
> line 4, in foo
>     raise BaseException("error")
> BaseException: error
>
> Why does that happen? Is there a way I can get a stack trace more
> consistently? It would help me debug.
>
> James
>

I mostly answered my own question. If I really want a traceback, I can just
catch the exception before foo() returns. Hacky example that uses
traceback.format_exc() to show the stack trace:

from beancount.core import amount, data, number
import traceback
__plugins__ = ("foo",)
def foo(entries, _options):
    try:
        return [data.Transaction(
            flag = "*",
            payee = None,
            narration = "",
            tags = "",
            links = set(),
            postings = [data.Posting("Equity:Foo",
amount.Amount(number.D("10"), "CAD") + "X", None, None, None, None)]
        )], ()
    except BaseException:
        traceback.format_exc()
        raise BaseException("error")

James

-- 
You received this message because you are subscribed to the Google Groups 
"Beancount" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/beancount/CAHpmPOAy6N0-A4hEHhwQ9UL0D3P_rie2NfW%3Dh0ecshyiihFC9w%40mail.gmail.com.

Reply via email to