diff --git a/app/helpers/body_trackers_helper.rb b/app/helpers/body_trackers_helper.rb
index ef34b8a..8b41cf8 100644
--- a/app/helpers/body_trackers_helper.rb
+++ b/app/helpers/body_trackers_helper.rb
@@ -1,6 +1,13 @@
module BodyTrackersHelper
def format_value(value)
amount, unitname = value
- amount.nil? ? '-' : "#{amount} [#{unitname || '-'}]"
+ case
+ when amount.nil?
+ '-'
+ when amount.nan?
+ '?'
+ else
+ "#{amount} [#{unitname || '-'}]"
+ end
end
end
diff --git a/app/models/formula.rb b/app/models/formula.rb
index ee18b5a..489817e 100644
--- a/app/models/formula.rb
+++ b/app/models/formula.rb
@@ -30,9 +30,6 @@ class Formula < ActiveRecord::Base
args << get_binding(quantities, args, length).eval(code)
end
args.last.map { |v| [v, nil] }
- rescue Exception => e
- puts e.message
- [[nil, nil]] * length
end
private
diff --git a/app/views/ingredients/_nutrients.html.erb b/app/views/ingredients/_nutrients.html.erb
index 2c3e039..a3f3582 100644
--- a/app/views/ingredients/_nutrients.html.erb
+++ b/app/views/ingredients/_nutrients.html.erb
@@ -4,6 +4,8 @@
<% if @ingredients.any? %>
<%= render partial: 'ingredients/options' %>
+ <%= error_messages_for *@quantities.map { |q| q.formula } %>
+
diff --git a/config/locales/en.yml b/config/locales/en.yml
index ed11c96..c355f94 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -42,6 +42,8 @@ en:
disallowed_keyword: 'includes disallowed keyword: "%{keyword}"'
disallowed_method: 'includes disallowed method call: "%{method}"'
unknown_quantity: 'contains undefined quantity: %{quantity}'
+ computation_failed: 'computation failed for "%{quantity}": %{description}
+ (%{count} values missing)'
body_trackers:
index:
heading: 'Summary'
diff --git a/lib/body_tracking/items_with_quantities.rb b/lib/body_tracking/items_with_quantities.rb
index c618707..c948047 100644
--- a/lib/body_tracking/items_with_quantities.rb
+++ b/lib/body_tracking/items_with_quantities.rb
@@ -67,7 +67,8 @@ module BodyTracking
q, deps = unchecked_q.shift
# quantity not computable (no formula) or not requiring calculation/computed
- if !q.formula || !q.formula.valid? || (subitems[q.name].length == items.count)
+ if !q.formula || q.formula.errors.any? || !q.formula.valid? ||
+ (subitems[q.name].length == items.count)
completed_q[q.name] = subitems.delete(q.name) { {} }
completed_q[q.name].default = [nil, nil]
next
@@ -85,8 +86,14 @@ module BodyTracking
output_ids = items.select { |i| subitems[q.name][i.id].nil? }.map(&:id)
input_q = q.formula.quantities
inputs = input_q.map { |i_q| [i_q, completed_q[i_q.name].values_at(*output_ids)] }
- q.formula.calculate(inputs.to_h).each_with_index do |result, index|
- subitems[q.name][output_ids[index]] = result
+ begin
+ calculated = q.formula.calculate(inputs.to_h)
+ rescue Exception => e
+ output_ids.each { |oid| subitems[q.name][oid] = BigDecimal::NAN }
+ q.formula.errors.add(:code, :computation_failed,
+ {quantity: q.name, description: e.message, count: output_ids.size})
+ else
+ output_ids.each_with_index { |oid, idx| subitems[q.name][oid] = calculated[idx] }
end
unchecked_q.unshift([q, deps])
next