Skip to content

Commit 185b929

Browse files
savhappySavannah Manning
andauthored
Fix on_mount LiveView hook when given :not_mounted_at_router (#742)
Co-authored-by: Savannah Manning <[email protected]>
1 parent eb44262 commit 185b929

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

lib/sentry/live_view_hook.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ if Code.ensure_loaded?(Phoenix.LiveView) do
5353
# https://develop.sentry.dev/sdk/event-payloads/request/
5454

5555
@doc false
56-
@spec on_mount(:default, map(), map(), struct()) :: {:cont, struct()}
57-
def on_mount(:default, params, _session, socket), do: on_mount(params, socket)
56+
@spec on_mount(:default, map() | :not_mounted_at_router, map(), struct()) :: {:cont, struct()}
57+
def on_mount(:default, %{} = params, _session, socket), do: on_mount(params, socket)
58+
def on_mount(:default, :not_mounted_at_router, _session, socket), do: {:cont, socket}
5859

5960
## Helpers
6061

test/sentry/live_view_hook_test.exs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,40 @@ defmodule SentryTest.LiveComponent do
3131
end
3232
end
3333

34+
defmodule SentryTest.DeadLive do
35+
use Phoenix.LiveView
36+
on_mount Sentry.LiveViewHook
37+
38+
@impl true
39+
def render(assigns) do
40+
~H"""
41+
<div>I'm being live_rendered!</div>
42+
"""
43+
end
44+
end
45+
46+
defmodule SentryTest.PageController do
47+
use Phoenix.Controller
48+
use Phoenix.Component
49+
50+
def page(conn, _params) do
51+
assigns = %{conn: conn}
52+
53+
rendered = ~H"""
54+
I'm a controller! <br><%= live_render(@conn, SentryTest.DeadLive, id: "live-render-id") %>
55+
"""
56+
57+
content = Phoenix.HTML.Safe.to_iodata(rendered)
58+
text(conn, content)
59+
end
60+
end
61+
3462
defmodule SentryTest.Router do
3563
use Phoenix.Router
3664
import Phoenix.LiveView.Router
3765

3866
scope "/" do
67+
get "/dead_test", SentryTest.PageController, :page
3968
live "/hook_test", SentryTest.Live
4069
end
4170
end
@@ -126,6 +155,15 @@ defmodule Sentry.LiveViewHookTest do
126155
assert html =~ "I&#39;m a LiveComponent"
127156
end
128157

158+
test "does not log an error when a liveview is a child of a non-live phoenix controller/view",
159+
%{conn: conn} do
160+
conn = get(conn, "/dead_test")
161+
162+
assert response = text_response(conn, 200)
163+
assert response =~ "I'm being live_rendered!"
164+
assert Logger.metadata() == []
165+
end
166+
129167
defp get_sentry_context(view) do
130168
{:dictionary, pdict} = Process.info(view.pid, :dictionary)
131169

0 commit comments

Comments
 (0)