Skip to content

Commit 1a9381c

Browse files
committed
Ensure compatibility with Jason and JSON
1 parent 5f5d814 commit 1a9381c

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

lib/error_tracker/schemas/occurrence.ex

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,23 @@ defmodule ErrorTracker.Occurrence do
5353
:sqlite -> Application.get_env(:ecto_sqlite3, :json_library, Jason)
5454
end)
5555

56-
case json_encoder.encode_to_iodata(context) do
57-
{:ok, _} ->
58-
put_change(changeset, :context, context)
59-
60-
{:error, _} ->
61-
Logger.warning(
62-
"[ErrorTracker] Context has been ignored: it is not serializable to JSON."
63-
)
64-
65-
put_change(changeset, :context, %{
66-
error: "Context not stored because it contains information not serializable to JSON."
67-
})
68-
end
56+
validated_context =
57+
try do
58+
_iodata = json_encoder.encode_to_iodata!(context)
59+
context
60+
rescue
61+
_e in Protocol.UndefinedError ->
62+
Logger.warning(
63+
"[ErrorTracker] Context has been ignored: it is not serializable to JSON."
64+
)
65+
66+
%{
67+
error:
68+
"Context not stored because it contains information not serializable to JSON."
69+
}
70+
end
71+
72+
put_change(changeset, :context, validated_context)
6973
else
7074
changeset
7175
end

0 commit comments

Comments
 (0)