diff --git a/lib/rdf.ex b/lib/rdf.ex index c98df3e..1c0013a 100644 --- a/lib/rdf.ex +++ b/lib/rdf.ex @@ -1,8 +1,6 @@ defmodule RDF do alias RDF.{Vocabulary, Literal, BlankNode, Triple} - defmodule InvalidURIError, do: defexception [:message] - @doc """ Generator function for URIs from strings or term atoms of a `RDF.Vocabulary`. @@ -27,7 +25,7 @@ defmodule RDF do if uri?(parsed_uri) do parsed_uri else - raise InvalidURIError, ~s(string "#{string}" is not a valid URI) + raise RDF.InvalidURIError, ~s(string "#{string}" is not a valid URI) end end diff --git a/lib/rdf/exceptions.ex b/lib/rdf/exceptions.ex new file mode 100644 index 0000000..47214d9 --- /dev/null +++ b/lib/rdf/exceptions.ex @@ -0,0 +1,48 @@ +defmodule RDF.InvalidURIError do + defexception [:message] +end + +defmodule RDF.InvalidLiteralError do + defexception [:message] +end + +defmodule RDF.Triple.InvalidSubjectError do + defexception [:subject] + + def message(%{subject: subject}) do + "'#{inspect(subject)}' is not a valid subject of a RDF.Triple" + end +end + +defmodule RDF.Triple.InvalidPredicateError do + defexception [:predicate] + + def message(%{predicate: predicate}) do + "'#{inspect(predicate)}' is not a valid predicate of a RDF.Triple" + end +end + + +defmodule RDF.Vocabulary.InvalidBaseURIError do + defexception [:message] +end + +defmodule RDF.Vocabulary.UndefinedTermError do + defexception [:message] +end + +defmodule RDF.Vocabulary.InvalidTermError do + defexception [:message] +end + + +defmodule RDF.InvalidRepoURLError do + defexception [:message, :url] + + def exception(opts) do + url = Keyword.fetch!(opts, :url) + msg = Keyword.fetch!(opts, :message) + msg = "invalid url #{url}, #{msg}" + %__MODULE__{message: msg, url: url} + end +end diff --git a/lib/rdf/literal.ex b/lib/rdf/literal.ex index 616a5fe..4ac3f5f 100644 --- a/lib/rdf/literal.ex +++ b/lib/rdf/literal.ex @@ -8,8 +8,6 @@ defmodule RDF.Literal do alias RDF.{XSD, RDFS} - defmodule InvalidLiteralError, do: defexception [:message] - @doc """ Creates a new `RDF.Literal` of the given value and tries to infer an appropriate XSD datatype. @@ -57,7 +55,7 @@ defmodule RDF.Literal do # def new(value) when is_reference(value), do: def new(value) do - raise InvalidLiteralError, "#{inspect value} not convertible to a RDF.Literal" + raise RDF.InvalidLiteralError, "#{inspect value} not convertible to a RDF.Literal" end def new(value, language: language) when is_binary(value) do diff --git a/lib/rdf/triple.ex b/lib/rdf/triple.ex index ce274aa..acfb296 100644 --- a/lib/rdf/triple.ex +++ b/lib/rdf/triple.ex @@ -16,18 +16,6 @@ defmodule RDF.Triple do @type convertible_predicate :: predicate | atom | String.t @type convertible_object :: object | atom | String.t # TODO: all basic Elixir types convertible to Literals - defmodule InvalidSubjectError do - defexception [:subject] - def message(%{subject: subject}), - do: "'#{inspect(subject)}' is not a valid subject of a RDF.Triple" - end - - defmodule InvalidPredicateError do - defexception [:predicate] - def message(%{predicate: predicate}), - do: "'#{inspect(predicate)}' is not a valid predicate of a RDF.Triple" - end - @doc """ Creates a `RDF.Triple` with proper RDF values. @@ -68,13 +56,13 @@ defmodule RDF.Triple do def convert_subject(uri = %URI{}), do: uri def convert_subject(bnode = %BlankNode{}), do: bnode def convert_subject(uri) when is_atom(uri) or is_binary(uri), do: RDF.uri(uri) - def convert_subject(arg), do: raise InvalidSubjectError, subject: arg + def convert_subject(arg), do: raise RDF.Triple.InvalidSubjectError, subject: arg @doc false def convert_predicate(uri) def convert_predicate(uri = %URI{}), do: uri def convert_predicate(uri) when is_atom(uri) or is_binary(uri), do: RDF.uri(uri) - def convert_predicate(arg), do: raise InvalidPredicateError, predicate: arg + def convert_predicate(arg), do: raise RDF.Triple.InvalidPredicateError, predicate: arg @doc false def convert_object(uri) diff --git a/lib/rdf/vocabulary.ex b/lib/rdf/vocabulary.ex index 9d8ee08..9942f13 100644 --- a/lib/rdf/vocabulary.ex +++ b/lib/rdf/vocabulary.ex @@ -67,10 +67,6 @@ defmodule RDF.Vocabulary do # or RDF.URI.Namespace? `__base_uri__` and `__terms__` ... """ - defmodule InvalidBaseURIError, do: defexception [:message] - defmodule UndefinedTermError, do: defexception [:message] - defmodule InvalidTermError, do: defexception [:message] - defmacro __using__(opts) do quote bind_quoted: [opts: opts], unquote: true do import unquote(__MODULE__) @@ -87,9 +83,9 @@ defmodule RDF.Vocabulary do # or RDF.URI.Namespace? @base_uri base_uri else :error -> - raise InvalidBaseURIError, "required base_uri missing" + raise RDF.Vocabulary.InvalidBaseURIError, "required base_uri missing" false -> - raise InvalidBaseURIError, + raise RDF.Vocabulary.InvalidBaseURIError, "a base_uri without a trailing '/' or '#' is invalid" end @@ -113,7 +109,7 @@ defmodule RDF.Vocabulary do # or RDF.URI.Namespace? if Enum.member?(@terms, term) do RDF.Vocabulary.term_to_uri(@base_uri, term) else - raise UndefinedTermError, + raise RDF.Vocabulary.UndefinedTermError, "undefined term #{term} in strict vocabulary #{__MODULE__}" end end @@ -158,7 +154,7 @@ defmodule RDF.Vocabulary do # or RDF.URI.Namespace? |> Enum.map(&String.reverse/1) |> Enum.map(&String.to_existing_atom/1) do [term, vocabulary] -> vocabulary.uri(term) - _ -> raise InvalidTermError, "" + _ -> raise RDF.Vocabulary.InvalidTermError, "" end end diff --git a/test/unit/description_test.exs b/test/unit/description_test.exs index 5700dab..c3c19ec 100644 --- a/test/unit/description_test.exs +++ b/test/unit/description_test.exs @@ -143,7 +143,7 @@ defmodule RDF.DescriptionTest do Description.add(description, {"not a URI", uri(EX.Object)}) end - assert_raise RDF.Literal.InvalidLiteralError, fn -> + assert_raise RDF.InvalidLiteralError, fn -> Description.add(description, {EX.prop, self}) end end diff --git a/test/unit/graph_test.exs b/test/unit/graph_test.exs index 2693d8c..1acb192 100644 --- a/test/unit/graph_test.exs +++ b/test/unit/graph_test.exs @@ -103,7 +103,7 @@ defmodule RDF.GraphTest do assert_raise RDF.InvalidURIError, fn -> Graph.add(graph, {"not a URI", EX.predicate, uri(EX.Object)}) end - assert_raise RDF.Literal.InvalidLiteralError, fn -> + assert_raise RDF.InvalidLiteralError, fn -> Graph.add(graph, {EX.Subject, EX.prop, self}) end end