File tree Expand file tree Collapse file tree 4 files changed +83
-0
lines changed Expand file tree Collapse file tree 4 files changed +83
-0
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 1+ defmodule Sentry.ErrorView do
2+ def render ( _ , _ ) do
3+ "error"
4+ end
5+ end
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ Code.require_file("test/support/test_before_send_event.exs")
44Code . require_file ( "test/support/test_filter.exs" )
55Code . require_file ( "test/support/test_gen_server.exs" )
66Code . require_file ( "test/support/test_client.exs" )
7+ Code . require_file ( "test/support/test_error_view.exs" )
78
89ExUnit . start ( )
910Application . ensure_all_started ( :bypass )
You can’t perform that action at this time.
0 commit comments