core: add error handling for RDF.Namespace term resolution

This commit is contained in:
Marcel Otto 2017-06-10 04:26:11 +02:00
parent 7c34231bb5
commit fd45a9ed6c
2 changed files with 21 additions and 5 deletions

View file

@ -46,15 +46,28 @@ defmodule RDF.Namespace do
def resolve_term(expr)
def resolve_term(uri = %URI{}), do: uri
def resolve_term(uri = %URI{}),
do: uri
def resolve_term(namespaced_term) when is_atom(namespaced_term),
do: namespaced_term |> to_string() |> do_resolve_term()
def resolve_term(namespaced_atom) when is_atom(namespaced_atom) do
defp do_resolve_term("Elixir." <> _ = namespaced_term) do
{term, namespace} =
namespaced_atom
namespaced_term
|> Module.split
|> List.pop_at(-1)
{term, namespace} = {String.to_atom(term), Module.concat(namespace)}
namespace.__resolve_term__(term)
if Keyword.has_key?(namespace.__info__(:functions), :__resolve_term__) do
namespace.__resolve_term__(term)
else
raise RDF.Namespace.UndefinedTermError,
"#{namespaced_term} is not a term on a RDF.Namespace"
end
end
defp do_resolve_term(namespaced_term) do
raise RDF.Namespace.UndefinedTermError,
"#{namespaced_term} is not a term on a RDF.Namespace"
end
end

View file

@ -384,11 +384,14 @@ defmodule RDF.Vocabulary.NamespaceTest do
end
@tag skip: "TODO: Can we make RDF.uri(:foo) an undefined function call with guards or in another way?"
test "resolving an unqualified term raises an error" do
assert_raise RDF.Namespace.UndefinedTermError, fn -> RDF.uri(:foo) end
end
test "resolving an non-RDF.Namespace module" do
assert_raise RDF.Namespace.UndefinedTermError, fn -> RDF.uri(ExUnit.Test) end
end
describe "term resolution in a strict vocab namespace" do
alias TestNS.{ExampleFromGraph, ExampleFromNTriplesFile, StrictExampleFromTerms}