Skip to content

Commit 06e0918

Browse files
dl-alexandreOpenCode
andcommitted
docs(live_view_hook): clarify connect_info requirements and rescue rationale
Expand the moduledoc to explain what each connect_info key provides and that the hook still works when keys are omitted. Add a comment to the try/rescue in get_connect_info_if_root/2 explaining why we rescue broadly (Bandit is optional and we must never crash the LiveView process). Co-Authored-By: OpenCode <noreply@opencode.ai>
1 parent 2e9fe8c commit 06e0918

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

lib/sentry/live_view_hook.ex

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,22 @@ if Code.ensure_loaded?(Phoenix.LiveView) do
1212
* The user agent and user's IP address
1313
* Breadcrumbs for events that happen within LiveView
1414
15-
To make this module work best, you'll need to fetch information from the LiveView's
16-
WebSocket. You can do that when calling the `socket/3` macro in your Phoenix endpoint.
17-
For example:
15+
To get the most complete request context, you'll need to fetch information from
16+
the LiveView's WebSocket. You can do that when calling the `socket/3` macro in your
17+
Phoenix endpoint. For example:
1818
1919
socket "/live", Phoenix.LiveView.Socket,
2020
websocket: [connect_info: [:peer_data, :uri, :user_agent]]
2121
22+
Each key in `connect_info` enriches the Sentry context as follows:
23+
24+
* `:uri` — overrides the request URL with the WebSocket connect URI
25+
* `:user_agent` — adds the user agent to extra context
26+
* `:peer_data` — adds the client's IP address to user context
27+
28+
The hook still works when these keys are omitted, but the corresponding context
29+
fields will not be populated.
30+
2231
## Examples
2332
2433
defmodule MyApp.UserLive do
@@ -157,8 +166,12 @@ if Code.ensure_loaded?(Phoenix.LiveView) do
157166
defp on_mount(params, %Phoenix.LiveView.Socket{} = socket) do
158167
Context.set_extra_context(%{socket_id: socket.id})
159168

160-
if uri = socket.host_uri do
161-
Context.set_request_context(%{url: URI.to_string(uri)})
169+
case socket.host_uri do
170+
%URI{} = uri ->
171+
Context.set_request_context(%{url: URI.to_string(uri)})
172+
173+
_ ->
174+
:ok
162175
end
163176

164177
Context.add_breadcrumb(%{
@@ -235,6 +248,9 @@ if Code.ensure_loaded?(Phoenix.LiveView) do
235248
defp get_connect_info_if_root(socket, key) do
236249
case socket.parent_pid do
237250
nil ->
251+
# Bandit raises when the transport is already closed (e.g. client disconnect
252+
# during mount). Because Bandit is an optional dependency, we rescue broadly
253+
# rather than matching on Bandit.TransportError directly.
238254
try do
239255
get_connect_info(socket, key)
240256
rescue

0 commit comments

Comments
 (0)