Skip to content

Commit

Permalink
Fix on_mount LiveView hook when given :not_mounted_at_router (#742)
Browse files Browse the repository at this point in the history
Co-authored-by: Savannah Manning <[email protected]>
  • Loading branch information
savhappy and Savannah Manning authored Jun 27, 2024
1 parent eb44262 commit 185b929
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/sentry/live_view_hook.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ if Code.ensure_loaded?(Phoenix.LiveView) do
# https://develop.sentry.dev/sdk/event-payloads/request/

@doc false
@spec on_mount(:default, map(), map(), struct()) :: {:cont, struct()}
def on_mount(:default, params, _session, socket), do: on_mount(params, socket)
@spec on_mount(:default, map() | :not_mounted_at_router, map(), struct()) :: {:cont, struct()}
def on_mount(:default, %{} = params, _session, socket), do: on_mount(params, socket)
def on_mount(:default, :not_mounted_at_router, _session, socket), do: {:cont, socket}

## Helpers

Expand Down
38 changes: 38 additions & 0 deletions test/sentry/live_view_hook_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,40 @@ defmodule SentryTest.LiveComponent do
end
end

defmodule SentryTest.DeadLive do
use Phoenix.LiveView
on_mount Sentry.LiveViewHook

@impl true
def render(assigns) do
~H"""
<div>I'm being live_rendered!</div>
"""
end
end

defmodule SentryTest.PageController do
use Phoenix.Controller
use Phoenix.Component

def page(conn, _params) do
assigns = %{conn: conn}

rendered = ~H"""
I'm a controller! <br><%= live_render(@conn, SentryTest.DeadLive, id: "live-render-id") %>
"""

content = Phoenix.HTML.Safe.to_iodata(rendered)
text(conn, content)
end
end

defmodule SentryTest.Router do
use Phoenix.Router
import Phoenix.LiveView.Router

scope "/" do
get "/dead_test", SentryTest.PageController, :page
live "/hook_test", SentryTest.Live
end
end
Expand Down Expand Up @@ -126,6 +155,15 @@ defmodule Sentry.LiveViewHookTest do
assert html =~ "I&#39;m a LiveComponent"
end

test "does not log an error when a liveview is a child of a non-live phoenix controller/view",
%{conn: conn} do
conn = get(conn, "/dead_test")

assert response = text_response(conn, 200)
assert response =~ "I'm being live_rendered!"
assert Logger.metadata() == []
end

defp get_sentry_context(view) do
{:dictionary, pdict} = Process.info(view.pid, :dictionary)

Expand Down

0 comments on commit 185b929

Please sign in to comment.