diff options
author | John Wiegley <johnw@newartisans.com> | 2018-07-19 22:01:31 -0700 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2018-07-19 22:01:31 -0700 |
commit | 5d02402291a923d30e66e4b0a08975ebf22361c3 (patch) | |
tree | 1802e52ef0bbbc4ab864564a242f032d22bb8f5f | |
parent | 8283abb594fa6729b0f32938377cbd6701441ebd (diff) | |
parent | 2b981925e5350022569203c045bd31b6d365d995 (diff) | |
download | fork-ledger-5d02402291a923d30e66e4b0a08975ebf22361c3.tar.gz fork-ledger-5d02402291a923d30e66e4b0a08975ebf22361c3.tar.bz2 fork-ledger-5d02402291a923d30e66e4b0a08975ebf22361c3.zip |
Merge remote-tracking branch 'origin/master' into next
-rw-r--r-- | src/context.h | 1 | ||||
-rw-r--r-- | src/error.h | 5 | ||||
-rw-r--r-- | src/global.h | 2 | ||||
-rw-r--r-- | src/textual.cc | 7 | ||||
-rw-r--r-- | test/python/JournalTest.py | 18 |
5 files changed, 29 insertions, 4 deletions
diff --git a/src/context.h b/src/context.h index 4a7a5441..0af59930 100644 --- a/src/context.h +++ b/src/context.h @@ -70,6 +70,7 @@ public: std::size_t errors; std::size_t count; std::size_t sequence; + std::string last; explicit parse_context_t(const path& cwd) : current_directory(cwd), master(NULL), scope(NULL), diff --git a/src/error.h b/src/error.h index 88e09329..ba278519 100644 --- a/src/error.h +++ b/src/error.h @@ -95,8 +95,9 @@ string source_context(const path& file, struct error_count { std::size_t count; - explicit error_count(std::size_t _count) : count(_count) {} - const char * what() const { return ""; } + std::string message; + explicit error_count(std::size_t _count, std::string _msg) : count(_count), message(_msg) {} + const char * what() const { return message.c_str(); } }; } // namespace ledger diff --git a/src/global.h b/src/global.h index 01ad60cc..8f8266ac 100644 --- a/src/global.h +++ b/src/global.h @@ -166,7 +166,7 @@ See LICENSE file included with the distribution for details and disclaimer."); OPTION_(global_scope_t, version, DO() { // -v parent->show_version_info(std::cout); - throw error_count(0); // exit immediately + throw error_count(0, ""); // exit immediately }); }; diff --git a/src/textual.cc b/src/textual.cc index 246db751..3416073b 100644 --- a/src/textual.cc +++ b/src/textual.cc @@ -282,6 +282,10 @@ void instance_t::parse() std::cerr << _("Error: ") << err.what() << std::endl; context.errors++; + if (! current_context.empty()) + context.last = current_context + "\n" + err.what(); + else + context.last = err.what(); } } @@ -2030,7 +2034,8 @@ std::size_t journal_t::read_textual(parse_context_stack_t& context_stack) TRACE_FINISH(parsing_total, 1); if (context_stack.get_current().errors > 0) - throw error_count(context_stack.get_current().errors); + throw error_count(context_stack.get_current().errors, + context_stack.get_current().last); return context_stack.get_current().count; } diff --git a/test/python/JournalTest.py b/test/python/JournalTest.py index e65c671d..2565ede8 100644 --- a/test/python/JournalTest.py +++ b/test/python/JournalTest.py @@ -22,6 +22,24 @@ class JournalTestCase(unittest.TestCase): for post in journal.query("food"): self.assertEqual(str(post.account), "Expenses:Food") self.assertEqual(post.amount, Amount("$21.34")) + + def testParseError(self): + # TODO: ledger spits out parse errors to standard out. + # This should not happen, especially when the error + # has already been captured by a Python exception. + def fun(): + read_journal_from_string(""" +2012-03-01 KFC + Expenses:Food rsnetnirsnti + Assets:Cash +""") + self.assertRaises(RuntimeError, fun) + try: + fun() + except RuntimeError as e: + self.assertEquals(str(e).splitlines()[-1], + "No quantity specified for amount") + def suite(): return unittest.TestLoader().loadTestsFromTestCase(JournalTestCase) |