Skip to content

Commit 7344588

Browse files
committed
add test for rendering feedback form in plug app
1 parent 2f6d81a commit 7344588

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

test/plug_capture_test.exs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,30 @@ defmodule Sentry.PlugCaptureTest do
138138
assert body =~ event_id
139139
assert body =~ ~s{"title":"Testing"}
140140
end
141+
142+
test "can render feedback form in Plug application" do
143+
bypass = Bypass.open()
144+
145+
Bypass.expect(bypass, fn conn ->
146+
{:ok, body, conn} = Plug.Conn.read_body(conn)
147+
_json = Jason.decode!(body)
148+
Plug.Conn.resp(conn, 200, ~s<{"id": "340"}>)
149+
end)
150+
151+
modify_env(:sentry, dsn: "http://public:secret@localhost:#{bypass.port}/1")
152+
153+
conn = conn(:get, "/error_route")
154+
155+
assert_raise(Plug.Conn.WrapperError, "** (RuntimeError) Error", fn ->
156+
Sentry.ExamplePlugApplication.call(conn, [])
157+
end)
158+
159+
{event_id, _} = Sentry.last_event_id_and_source()
160+
161+
assert_received {:plug_conn, :sent}
162+
assert {500, _headers, body} = sent_resp(conn)
163+
assert body =~ "sentry-cdn"
164+
assert body =~ event_id
165+
assert body =~ ~s{"title":"Testing"}
166+
end
141167
end

test/support/example_plug_application.ex

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
defmodule Sentry.ExamplePlugApplication do
2-
use Sentry.PlugCapture
32
use Plug.Router
3+
use Sentry.PlugCapture
4+
use Plug.ErrorHandler
45

56
plug Plug.Parsers, parsers: [:multipart, :urlencoded]
67
plug Sentry.PlugContext
@@ -29,4 +30,26 @@ defmodule Sentry.ExamplePlugApplication do
2930
_ = conn
3031
raise RuntimeError, "Error"
3132
end
33+
34+
def handle_errors(conn, %{kind: _kind, reason: _reason, stack: _stack}) do
35+
response = case Sentry.last_event_id_and_source() do
36+
{event_id, :plug} ->
37+
opts =
38+
%{title: "Testing", eventId: event_id}
39+
|> Jason.encode!()
40+
41+
"""
42+
<script src="https://browser.sentry-cdn.com/5.9.1/bundle.min.js" integrity="sha384-/x1aHz0nKRd6zVUazsV6CbQvjJvr6zQL2CHbQZf3yoLkezyEtZUpqUNnOLW9Nt3v" crossorigin="anonymous"></script>
43+
<script>
44+
Sentry.init({ dsn: '#{Sentry.Config.dsn()}' });
45+
Sentry.showReportDialog(#{opts})
46+
</script>
47+
"""
48+
49+
_ ->
50+
"error"
51+
end
52+
53+
send_resp(conn, conn.status, response)
54+
end
3255
end

0 commit comments

Comments
 (0)