Skip to content

Commit 7b5d2ee

Browse files
committed
Use a tagged :ok/:error tuple
1 parent cb9593b commit 7b5d2ee

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

lib/exqlite/sqlite3.ex

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -489,12 +489,19 @@ defmodule Exqlite.Sqlite3 do
489489

490490
defp convert_with_type_extensions([extension | other_extensions], val) do
491491
case extension.convert(val) do
492-
nil -> convert_with_type_extensions(other_extensions, val)
493-
convert -> convert
492+
nil ->
493+
convert_with_type_extensions(other_extensions, val)
494+
495+
{:ok, converted} ->
496+
converted
497+
498+
{:error, reason} ->
499+
raise ArgumentError,
500+
"Failed conversion by TypeExtension #{extension}: #{inspect(val)}. Reason: #{inspect(reason)}."
494501
end
495502
end
496503

497-
defp type_extensions() do
504+
defp type_extensions do
498505
Application.get_env(:exqlite, :type_extensions, [])
499506
end
500507
end

lib/exqlite/extension.ex renamed to lib/exqlite/type_extension.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ defmodule Exqlite.TypeExtension do
66

77
@doc """
88
Takes a value and convers it to data suitable for storage in the database.
9+
10+
Returns a tagged :ok/:error tuple. If the value is not convertable by this
11+
extension, returns nil.
912
"""
10-
@callback convert(value :: term) :: term
13+
@callback convert(value :: term) :: {:ok, term} | {:error, reason :: term} | nil
1114
end

0 commit comments

Comments
 (0)