summaryrefslogtreecommitdiff
path: root/src/textual.cc
diff options
context:
space:
mode:
authorAlexis Hildebrandt <afh@surryhill.net>2014-12-28 13:42:02 +0100
committerAlexis Hildebrandt <afh@surryhill.net>2014-12-28 13:42:02 +0100
commit0462c887ae5814d08de7d2a860c41bb1a07bee52 (patch)
tree0e231a62e3b8e23a8bbf9a1229ea1a178c376333 /src/textual.cc
parente716980c7d4ae2a2852a7cd0e5c0cf52014ed787 (diff)
downloadfork-ledger-0462c887ae5814d08de7d2a860c41bb1a07bee52.tar.gz
fork-ledger-0462c887ae5814d08de7d2a860c41bb1a07bee52.tar.bz2
fork-ledger-0462c887ae5814d08de7d2a860c41bb1a07bee52.zip
Improve error handling when parsing year directives
A literal Y directive or ‘year’ directive with an empty or invalid argument, e.g. ‘2o14’ (that is a small letter ‘oh’ instead of a zero) would fail with the following rather unhelpful error message: Error: bad lexical cast: source type value could not be interpreted as target
Diffstat (limited to 'src/textual.cc')
-rw-r--r--src/textual.cc23
1 files changed, 14 insertions, 9 deletions
diff --git a/src/textual.cc b/src/textual.cc
index 5862c26c..f0df3b66 100644
--- a/src/textual.cc
+++ b/src/textual.cc
@@ -419,7 +419,9 @@ void instance_t::read_next_directive(bool& error_flag)
price_xact_directive(line);
break;
case 'Y': // set the current year
- apply_year_directive(line + 1);
+ if (std::strlen(line+1) == 0)
+ throw_(parse_error, _f("Directive '%1%' requires an argument") % line[0]);
+ apply_year_directive(line+1);
break;
}
}
@@ -863,14 +865,17 @@ void instance_t::apply_rate_directive(char * line)
void instance_t::apply_year_directive(char * line)
{
- apply_stack.push_front(application_t("year", epoch));
-
- // This must be set to the last day of the year, otherwise partial
- // dates like "11/01" will refer to last year's november, not the
- // current year.
- unsigned short year(lexical_cast<unsigned short>(skip_ws(line)));
- DEBUG("times.epoch", "Setting current year to " << year);
- epoch = datetime_t(date_t(year, 12, 31));
+ try {
+ unsigned short year(lexical_cast<unsigned short>(skip_ws(line)));
+ apply_stack.push_front(application_t("year", epoch));
+ DEBUG("times.epoch", "Setting current year to " << year);
+ // This must be set to the last day of the year, otherwise partial
+ // dates like "11/01" will refer to last year's november, not the
+ // current year.
+ epoch = datetime_t(date_t(year, 12, 31));
+ } catch(bad_lexical_cast &) {
+ throw_(parse_error, _f("Argument '%1%' not a valid year") % skip_ws(line));
+ }
}
void instance_t::end_apply_directive(char * kind)