Skip to content

Commit e119149

Browse files
committed
Safely convert payloads to strings
For non-error tuples `Exception.normalize/3` returns the original payload, which may be anything. Some payloads such as tuples don't implement the `String.Chars` protocol. So cally `to_string/1` will fail. In such cases we fall back to `inspect/1`.
1 parent 428f4c7 commit e119149

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

lib/error_tracker.ex

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,19 @@ defmodule ErrorTracker do
232232
%struct{} = ex ->
233233
{to_string(struct), Exception.message(ex)}
234234

235-
other ->
236-
{to_string(kind), to_string(other)}
235+
payload ->
236+
# For non-error tuples `Exception.normalize/3` returns the original payload, which may be
237+
# anything. Some payloads such as tuples don't implement the `String.Chars` protocol. So
238+
# calling `to_string/1` will fail. In such cases we fall back to `inspect/1`.
239+
payload_as_string =
240+
try do
241+
to_string(payload)
242+
rescue
243+
Protocol.UndefinedError ->
244+
inspect(payload)
245+
end
246+
247+
{to_string(kind), payload_as_string}
237248
end
238249
end
239250

0 commit comments

Comments
 (0)