Tree: raise an exception if we can't look up the tree

This commit is contained in:
Carlos Martín Nieto
2013-08-10 06:30:49 +02:00
parent 3a7d37bec8
commit 19ff0dd7d5

View File

@@ -44,8 +44,15 @@ defimpl Access, for: Geef.Tree do
end end
defexception Geef.TreeError, reason: nil do
def message(Geef.TreeError[reason: reason]) do
"tree error #{inspect reason}"
end
end
defimpl Enumerable, for: Geef.Tree do defimpl Enumerable, for: Geef.Tree do
alias Geef.Tree alias Geef.Tree
alias Geef.TreeError
def count(tree) do def count(tree) do
Tree.count(tree) Tree.count(tree)
@@ -67,8 +74,12 @@ defimpl Enumerable, for: Geef.Tree do
# Call the user-passed function and recurse with the next index # Call the user-passed function and recurse with the next index
defp reduce(tree, idx, count, acc, fun) do defp reduce(tree, idx, count, acc, fun) do
cur = Tree.nth(tree, idx) case Tree.nth(tree, idx) do
reduce(tree, idx + 1, count, fun.(cur, acc), fun) {:ok, entry} ->
reduce(tree, idx + 1, count, fun.(entry, acc), fun)
{:error, error} ->
raise TreeError, reason: error
end
end end
end end