Skip to content

Commit b634558

Browse files
committed
add phoenix endpoint and test
1 parent 79d0a88 commit b634558

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed

lib/sentry/phoenix_endpoint.ex

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
defmodule Sentry.Phoenix.Endpoint do
2+
defmacro __using__(_opts) do
3+
quote do
4+
@before_compile Sentry.Phoenix.Endpoint
5+
end
6+
end
7+
8+
defmacro __before_compile__(_) do
9+
quote do
10+
defoverridable call: 2
11+
12+
def call(conn, opts) do
13+
try do
14+
super(conn, opts)
15+
catch
16+
kind, reason ->
17+
stacktrace = System.stacktrace()
18+
request = Sentry.Plug.build_request_interface_data(conn, [])
19+
exception = Exception.normalize(kind, reason, stacktrace)
20+
21+
Sentry.capture_exception(
22+
exception,
23+
stacktrace: stacktrace,
24+
request: request,
25+
event_source: :endpoint,
26+
error_type: kind
27+
)
28+
29+
:erlang.raise(kind, reason, stacktrace)
30+
end
31+
end
32+
end
33+
end
34+
end

test/phoenix_endpoint_test.exs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
defmodule Sentry.PhoenixEndpointTest do
2+
use ExUnit.Case
3+
use Plug.Test
4+
import Sentry.TestEnvironmentHelper
5+
6+
Application.put_env(
7+
:sentry,
8+
__MODULE__.Endpoint,
9+
render_errors: [view: Sentry.ErrorView, accepts: ~w(html)]
10+
)
11+
12+
defmodule Endpoint do
13+
use Phoenix.Endpoint, otp_app: :sentry
14+
use Sentry.Phoenix.Endpoint
15+
plug(:error)
16+
plug(Sentry.ExampleApp)
17+
18+
def error(_conn, _opts) do
19+
raise "EndpointError"
20+
end
21+
end
22+
23+
test "reports errors occurring in Phoenix Endpoint" do
24+
bypass = Bypass.open()
25+
26+
Bypass.expect(bypass, fn conn ->
27+
{:ok, body, conn} = Plug.Conn.read_body(conn)
28+
json = Poison.decode!(body)
29+
assert json["culprit"] == "Sentry.PhoenixEndpointTest.Endpoint.error/2"
30+
assert json["message"] == "(RuntimeError) EndpointError"
31+
Plug.Conn.resp(conn, 200, ~s<{"id": "340"}>)
32+
end)
33+
34+
modify_env(:sentry, dsn: "http://public:secret@localhost:#{bypass.port}/1")
35+
modify_env(:phoenix, format_encoders: [])
36+
{:ok, _} = Endpoint.start_link()
37+
38+
assert_raise RuntimeError, "EndpointError", fn ->
39+
conn(:get, "/")
40+
|> Endpoint.call([])
41+
end
42+
end
43+
end

test/support/test_error_view.exs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
defmodule Sentry.ErrorView do
2+
def render(_, _) do
3+
"error"
4+
end
5+
end

test/test_helper.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Code.require_file("test/support/test_before_send_event.exs")
44
Code.require_file("test/support/test_filter.exs")
55
Code.require_file("test/support/test_gen_server.exs")
66
Code.require_file("test/support/test_client.exs")
7+
Code.require_file("test/support/test_error_view.exs")
78

89
ExUnit.start()
910
Application.ensure_all_started(:bypass)

0 commit comments

Comments
 (0)