Skip to content

Commit 142dbdc

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 142dbdc

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

lib/sentry/live_view_hook.ex

Lines changed: 15 additions & 3 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
@@ -235,6 +244,9 @@ if Code.ensure_loaded?(Phoenix.LiveView) do
235244
defp get_connect_info_if_root(socket, key) do
236245
case socket.parent_pid do
237246
nil ->
247+
# Bandit raises when the transport is already closed (e.g. client disconnect
248+
# during mount). Because Bandit is an optional dependency, we rescue broadly
249+
# rather than matching on Bandit.TransportError directly.
238250
try do
239251
get_connect_info(socket, key)
240252
rescue

0 commit comments

Comments
 (0)