Skip to content

Commit 3ed8f1a

Browse files
Merge pull request #406 from getsentry/fix-erlang-error-in-plug-capture
Fix trying to transform erlang error coming from PlugCapture
2 parents eebbb97 + 0d49ce0 commit 3ed8f1a

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

lib/sentry/plug_capture.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ defmodule Sentry.PlugCapture do
3838
super(conn, opts)
3939
rescue
4040
e in Plug.Conn.WrapperError ->
41-
Sentry.capture_exception(e.reason, stacktrace: e.stack, event_source: :plug)
41+
exception = Exception.normalize(:error, e.reason, e.stack)
42+
Sentry.capture_exception(exception, stacktrace: e.stack, event_source: :plug)
4243
Plug.Conn.WrapperError.reraise(e)
4344

4445
e ->

test/plug_capture_test.exs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,23 @@ defmodule Sentry.PlugCaptureTest do
77
defmodule PhoenixController do
88
use Phoenix.Controller
99
def error(_conn, _params), do: raise("PhoenixError")
10+
11+
def assigns(conn, _params) do
12+
_test = conn.assigns2.test
13+
end
1014
end
1115

1216
defmodule PhoenixRouter do
1317
use Phoenix.Router
1418

1519
get "/error_route", PhoenixController, :error
20+
get "/assigns_route", PhoenixController, :assigns
1621
end
1722

1823
defmodule PhoenixEndpoint do
1924
use Sentry.PlugCapture
2025
use Phoenix.Endpoint, otp_app: :sentry
26+
use Plug.Debugger, otp_app: :sentry
2127

2228
plug Plug.Parsers,
2329
parsers: [:urlencoded, :multipart, :json],
@@ -215,4 +221,32 @@ defmodule Sentry.PlugCaptureTest do
215221
assert body =~ event_id
216222
assert body =~ ~s{"title":"Testing"}
217223
end
224+
225+
test "handles Erlang error in Plug.Conn.WrapperError" do
226+
bypass = Bypass.open()
227+
228+
Bypass.expect(bypass, fn conn ->
229+
{:ok, body, conn} = Plug.Conn.read_body(conn)
230+
json = Jason.decode!(body)
231+
assert json["culprit"] == "Sentry.PlugCaptureTest.PhoenixController.assigns/2"
232+
Plug.Conn.resp(conn, 200, ~s<{"id": "340"}>)
233+
end)
234+
235+
modify_env(:sentry,
236+
dsn: "http://public:secret@localhost:#{bypass.port}/1",
237+
"#{__MODULE__.PhoenixEndpoint}": [
238+
render_errors: [view: Sentry.ErrorView, accepts: ~w(html)]
239+
]
240+
)
241+
242+
{:ok, _} = PhoenixEndpoint.start_link()
243+
244+
capture_log(fn ->
245+
assert_raise KeyError, fn ->
246+
conn(:get, "/assigns_route")
247+
|> Plug.Conn.put_req_header("throw", "throw")
248+
|> PhoenixEndpoint.call([])
249+
end
250+
end)
251+
end
218252
end

0 commit comments

Comments
 (0)