Keryx: a preview that cannot be encoded falls back rather than raising

2cdebbe · Gabriel Morell · 2026-10-14 21:16

11 files +10 -1
Message
{commit_body(@commit)}

Files changed

modified apps/room_sanctum/lib/room_sanctum_web/live/keryx_live/show.ex
+4 −1
@@ -67,7 +67,10 @@ defmodule RoomSanctumWeb.KeryxLive.Show do
67 67 |> Makeup.highlight(lexer: Makeup.Lexers.JsonLexer)
68 68 |> Phoenix.HTML.raw()
69 69 rescue
70 − Protocol.UndefinedError ->
70 + # Anything, not only a missing encoder: Ecto's encoders for an unloaded
71 + # association or a schema's __meta__ raise a plain RuntimeError, and a
72 + # preview is not worth the page.
73 + _ ->
71 74 # If Jason encoding fails, use inspect as fallback
72 75 inspected = inspect(data, pretty: true, limit: 2048)
73 76 String.slice(inspected, 0..2048) <> if String.length(inspected) > 2048, do: "...", else: ""
modified apps/room_sanctum/test/room_sanctum_web/live/keryx_show_test.exs
+6 −0
@@ -87,4 +87,10 @@ defmodule RoomSanctumWeb.KeryxItemPreviewTest do
87 87 test "falls back to inspect for what JSON cannot say" do
88 88 assert Show.format_item_preview(%{at: {1, 2}}) =~ "{1, 2}"
89 89 end
90 +
91 + test "falls back for an encoder that raises rather than being missing" do
92 + unloaded = %Ecto.Association.NotLoaded{__field__: :trip, __owner__: Trip, __cardinality__: :one}
93 +
94 + assert Show.format_item_preview(%{trip: unloaded}) =~ "NotLoaded"
95 + end
90 96 end

Parents: 8a95d37