Use function body rescue where possible

This commit is contained in:
Marcel Otto 2018-05-16 01:59:46 +02:00
parent fc4a86483d
commit b4c3dc8d98
3 changed files with 20 additions and 26 deletions

View file

@ -97,11 +97,9 @@ defmodule RDF.IRI do
def absolute?(%URI{scheme: nil}), do: false def absolute?(%URI{scheme: nil}), do: false
def absolute?(%URI{scheme: _}), do: true def absolute?(%URI{scheme: _}), do: true
def absolute?(qname) when is_atom(qname) and not qname in [nil, true, false] do def absolute?(qname) when is_atom(qname) and not qname in [nil, true, false] do
try do qname |> Namespace.resolve_term |> absolute?()
qname |> Namespace.resolve_term |> absolute?() rescue
rescue _ -> false
_ -> false
end
end end
def absolute?(_), do: false def absolute?(_), do: false

View file

@ -55,13 +55,11 @@ defmodule RDF.Serialization do
def format(name) def format(name)
def format(name) when is_binary(name) do def format(name) when is_binary(name) do
try do name
name |> String.to_existing_atom
|> String.to_existing_atom |> format()
|> format() rescue
rescue ArgumentError -> nil
ArgumentError -> nil
end
end end
def format(name) do def format(name) do

View file

@ -46,22 +46,20 @@ defmodule RDF.Turtle.Decoder do
def parse(tokens), do: tokens |> :turtle_parser.parse def parse(tokens), do: tokens |> :turtle_parser.parse
defp build_graph(ast, base) do defp build_graph(ast, base) do
try do {graph, _} =
{graph, _} = Enum.reduce ast, {RDF.Graph.new, %State{base_iri: base}}, fn
Enum.reduce ast, {RDF.Graph.new, %State{base_iri: base}}, fn {:triples, triples_ast}, {graph, state} ->
{:triples, triples_ast}, {graph, state} -> with {statements, state} = triples(triples_ast, state) do
with {statements, state} = triples(triples_ast, state) do {RDF.Graph.add(graph, statements), state}
{RDF.Graph.add(graph, statements), state} end
end
{:directive, directive_ast}, {graph, state} -> {:directive, directive_ast}, {graph, state} ->
{graph, directive(directive_ast, state)} {graph, directive(directive_ast, state)}
end end
{:ok, graph} {:ok, graph}
rescue rescue
error -> {:error, Exception.message(error)} error -> {:error, Exception.message(error)}
end
end end
defp directive({:prefix, {:prefix_ns, _, ns}, iri}, state) do defp directive({:prefix, {:prefix_ns, _, ns}, iri}, state) do