Errors involving value objects provide more context

This commit is contained in:
John Wiegley 2010-06-10 08:16:59 -04:00
parent 7be17fd779
commit 77c5df4053
2 changed files with 45 additions and 9 deletions

View file

@ -78,6 +78,8 @@ value_t expr_t::real_calc(scope_t& scope)
} }
catch (const std::exception& err) { catch (const std::exception& err) {
if (locus) { if (locus) {
string current_context = error_context();
add_error_context(_("While evaluating value expression:")); add_error_context(_("While evaluating value expression:"));
add_error_context(op_context(ptr, locus)); add_error_context(op_context(ptr, locus));
@ -103,6 +105,9 @@ value_t expr_t::real_calc(scope_t& scope)
} }
add_error_context(out.str()); add_error_context(out.str());
} }
if (! current_context.empty())
add_error_context(current_context);
} }
throw; throw;
} }

View file

@ -122,7 +122,9 @@ value_t::operator bool() const
break; break;
} }
add_error_context(_("While taking boolean value of %1:") << *this);
throw_(value_error, _("Cannot determine truth of %1") << label()); throw_(value_error, _("Cannot determine truth of %1") << label());
return false; return false;
} }
@ -313,7 +315,9 @@ value_t value_t::number() const
break; break;
} }
add_error_context(_("While calling number() on %1:") << *this);
throw_(value_error, _("Cannot determine numeric value of %1") << label()); throw_(value_error, _("Cannot determine numeric value of %1") << label());
return false; return false;
} }
@ -335,6 +339,7 @@ value_t& value_t::operator+=(const value_t& val)
for (; i != end(); i++, j++) for (; i != end(); i++, j++)
*i += *j; *i += *j;
} else { } else {
add_error_context(_("While adding %1 to %2:") << *this << val);
throw_(value_error, _("Cannot add sequences of different lengths")); throw_(value_error, _("Cannot add sequences of different lengths"));
} }
} else { } else {
@ -447,7 +452,9 @@ value_t& value_t::operator+=(const value_t& val)
break; break;
} }
add_error_context(_("While adding %1 to %2:") << *this << val);
throw_(value_error, _("Cannot add %1 to %2") << val.label() << label()); throw_(value_error, _("Cannot add %1 to %2") << val.label() << label());
return *this; return *this;
} }
@ -464,6 +471,7 @@ value_t& value_t::operator-=(const value_t& val)
for (; i != end(); i++, j++) for (; i != end(); i++, j++)
*i -= *j; *i -= *j;
} else { } else {
add_error_context(_("While subtracting %1 to %2:") << *this << val);
throw_(value_error, _("Cannot subtract sequences of different lengths")); throw_(value_error, _("Cannot subtract sequences of different lengths"));
} }
} else { } else {
@ -586,6 +594,7 @@ value_t& value_t::operator-=(const value_t& val)
break; break;
} }
add_error_context(_("While subtracting %1 from %2:") << *this << val);
throw_(value_error, _("Cannot subtract %1 from %2") << val.label() << label()); throw_(value_error, _("Cannot subtract %1 from %2") << val.label() << label());
return *this; return *this;
@ -667,9 +676,7 @@ value_t& value_t::operator*=(const value_t& val)
break; break;
} }
DEBUG("value.multiply.error", "Left: " << *this); add_error_context(_("While multiplying %1 with %2:") << *this << val);
DEBUG("value.multiply.error", "Right: " << val);
throw_(value_error, _("Cannot multiply %1 with %2") << label() << val.label()); throw_(value_error, _("Cannot multiply %1 with %2") << label() << val.label());
return *this; return *this;
@ -747,6 +754,7 @@ value_t& value_t::operator/=(const value_t& val)
break; break;
} }
add_error_context(_("While dividing %1 by %2:") << *this << val);
throw_(value_error, _("Cannot divide %1 by %2") << label() << val.label()); throw_(value_error, _("Cannot divide %1 by %2") << label() << val.label());
return *this; return *this;
@ -832,6 +840,7 @@ bool value_t::is_equal_to(const value_t& val) const
break; break;
} }
add_error_context(_("While comparing equality of %1 to %2:") << *this << val);
throw_(value_error, _("Cannot compare %1 to %2") << label() << val.label()); throw_(value_error, _("Cannot compare %1 to %2") << label() << val.label());
return *this; return *this;
@ -958,6 +967,8 @@ bool value_t::is_less_than(const value_t& val) const
break; break;
} }
add_error_context(_("While comparing if %1 is less than %2:")
<< *this << val);
throw_(value_error, _("Cannot compare %1 to %2") << label() << val.label()); throw_(value_error, _("Cannot compare %1 to %2") << label() << val.label());
return *this; return *this;
@ -1078,6 +1089,8 @@ bool value_t::is_greater_than(const value_t& val) const
break; break;
} }
add_error_context(_("While comparing if %1 is greater than %2:")
<< *this << val);
throw_(value_error, _("Cannot compare %1 to %2") << label() << val.label()); throw_(value_error, _("Cannot compare %1 to %2") << label() << val.label());
return *this; return *this;
@ -1219,6 +1232,7 @@ void value_t::in_place_cast(type_t cast_type)
return; return;
} }
else { else {
add_error_context(_("While converting %1 to an amount:") << *this);
throw_(value_error, _("Cannot convert %1 with multiple commodities to %2") throw_(value_error, _("Cannot convert %1 with multiple commodities to %2")
<< label() << label(cast_type)); << label() << label(cast_type));
} }
@ -1266,6 +1280,7 @@ void value_t::in_place_cast(type_t cast_type)
break; break;
} }
add_error_context(_("While converting %1:") << *this);
throw_(value_error, throw_(value_error,
_("Cannot convert %1 to %2") << label() << label(cast_type)); _("Cannot convert %1 to %2") << label() << label(cast_type));
} }
@ -1300,6 +1315,7 @@ void value_t::in_place_negate()
break; break;
} }
add_error_context(_("While negating %1:") << *this);
throw_(value_error, _("Cannot negate %1") << label()); throw_(value_error, _("Cannot negate %1") << label());
} }
@ -1336,6 +1352,7 @@ void value_t::in_place_not()
break; break;
} }
add_error_context(_("While applying not to %1:") << *this);
throw_(value_error, _("Cannot 'not' %1") << label()); throw_(value_error, _("Cannot 'not' %1") << label());
} }
@ -1365,6 +1382,7 @@ bool value_t::is_realzero() const
return ! as_expr(); return ! as_expr();
default: default:
add_error_context(_("While applying is_realzero to %1:") << *this);
throw_(value_error, _("Cannot determine if %1 is really zero") << label()); throw_(value_error, _("Cannot determine if %1 is really zero") << label());
} }
return false; return false;
@ -1396,6 +1414,7 @@ bool value_t::is_zero() const
return ! as_expr(); return ! as_expr();
default: default:
add_error_context(_("While applying is_zero to %1:") << *this);
throw_(value_error, _("Cannot determine if %1 is zero") << label()); throw_(value_error, _("Cannot determine if %1 is zero") << label());
} }
return false; return false;
@ -1424,6 +1443,7 @@ value_t value_t::value(const optional<datetime_t>& moment,
break; break;
} }
add_error_context(_("While finding valuation of %1:") << *this);
throw_(value_error, _("Cannot find the value of %1") << label()); throw_(value_error, _("Cannot find the value of %1") << label());
return NULL_VALUE; return NULL_VALUE;
} }
@ -1511,6 +1531,7 @@ value_t value_t::abs() const
break; break;
} }
add_error_context(_("While taking abs of %1:") << *this);
throw_(value_error, _("Cannot abs %1") << label()); throw_(value_error, _("Cannot abs %1") << label());
return NULL_VALUE; return NULL_VALUE;
} }
@ -1537,6 +1558,7 @@ void value_t::in_place_round()
break; break;
} }
add_error_context(_("While rounding %1:") << *this);
throw_(value_error, _("Cannot set rounding for %1") << label()); throw_(value_error, _("Cannot set rounding for %1") << label());
} }
@ -1562,6 +1584,7 @@ void value_t::in_place_truncate()
break; break;
} }
add_error_context(_("While truncating %1:") << *this);
throw_(value_error, _("Cannot truncate %1") << label()); throw_(value_error, _("Cannot truncate %1") << label());
} }
@ -1587,6 +1610,7 @@ void value_t::in_place_floor()
break; break;
} }
add_error_context(_("While flooring %1:") << *this);
throw_(value_error, _("Cannot floor %1") << label()); throw_(value_error, _("Cannot floor %1") << label());
} }
@ -1612,32 +1636,38 @@ void value_t::in_place_unround()
break; break;
} }
add_error_context(_("While unrounding %1:") << *this);
throw_(value_error, _("Cannot unround %1") << label()); throw_(value_error, _("Cannot unround %1") << label());
} }
void value_t::annotate(const annotation_t& details) void value_t::annotate(const annotation_t& details)
{ {
if (is_amount()) if (is_amount()) {
as_amount_lval().annotate(details); as_amount_lval().annotate(details);
else } else {
add_error_context(_("While attempting to annotate %1:") << *this);
throw_(value_error, _("Cannot annotate %1") << label()); throw_(value_error, _("Cannot annotate %1") << label());
}
} }
bool value_t::has_annotation() const bool value_t::has_annotation() const
{ {
if (is_amount()) if (is_amount()) {
return as_amount().has_annotation(); return as_amount().has_annotation();
else } else {
add_error_context(_("While checking if %1 has annotations:") << *this);
throw_(value_error, throw_(value_error,
_("Cannot determine whether %1 is annotated") << label()); _("Cannot determine whether %1 is annotated") << label());
}
return false; return false;
} }
annotation_t& value_t::annotation() annotation_t& value_t::annotation()
{ {
if (is_amount()) if (is_amount()) {
return as_amount_lval().annotation(); return as_amount_lval().annotation();
else { } else {
add_error_context(_("While requesting the annotations of %1:") << *this);
throw_(value_error, _("Cannot request annotation of %1") << label()); throw_(value_error, _("Cannot request annotation of %1") << label());
return as_amount_lval().annotation(); // quiet g++ warning return as_amount_lval().annotation(); // quiet g++ warning
} }
@ -1776,6 +1806,7 @@ void value_t::print(std::ostream& out,
break; break;
default: default:
add_error_context(_("While printing %1:") << *this);
throw_(value_error, _("Cannot print %1") << label()); throw_(value_error, _("Cannot print %1") << label());
} }
} }