Skip to content

Commit 8e7a19a

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 8e7a19a

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

lib/error_tracker.ex

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,14 +229,18 @@ defmodule ErrorTracker do
229229

230230
defp normalize_exception({kind, ex}, stacktrace) do
231231
case Exception.normalize(kind, ex, stacktrace) do
232-
%struct{} = ex ->
233-
{to_string(struct), Exception.message(ex)}
234-
235-
other ->
236-
{to_string(kind), to_string(other)}
232+
%struct{} = ex -> {to_string(struct), Exception.message(ex)}
233+
payload -> {to_string(kind), safe_to_string(payload)}
237234
end
238235
end
239236

237+
defp safe_to_string(term) do
238+
to_string(term)
239+
rescue
240+
Protocol.UndefinedError ->
241+
inspect(term)
242+
end
243+
240244
defp bread_crumbs(exception) do
241245
case exception do
242246
{_kind, exception} -> bread_crumbs(exception)

0 commit comments

Comments
 (0)