core: collect all exceptions in a dedicated file

This commit is contained in:
Marcel Otto 2016-11-04 22:13:06 +01:00
parent c192b49bfd
commit 4ebc3e63c4
7 changed files with 58 additions and 30 deletions

View file

@ -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

48
lib/rdf/exceptions.ex Normal file
View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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