Skip to content

Commit

Permalink
Merge pull request #406 from getsentry/fix-erlang-error-in-plug-capture
Browse files Browse the repository at this point in the history
Fix trying to transform erlang error coming from PlugCapture
  • Loading branch information
mitchellhenke authored Jun 30, 2020
2 parents eebbb97 + 0d49ce0 commit 3ed8f1a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/sentry/plug_capture.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ defmodule Sentry.PlugCapture do
super(conn, opts)
rescue
e in Plug.Conn.WrapperError ->
Sentry.capture_exception(e.reason, stacktrace: e.stack, event_source: :plug)
exception = Exception.normalize(:error, e.reason, e.stack)
Sentry.capture_exception(exception, stacktrace: e.stack, event_source: :plug)
Plug.Conn.WrapperError.reraise(e)

e ->
Expand Down
34 changes: 34 additions & 0 deletions test/plug_capture_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@ defmodule Sentry.PlugCaptureTest do
defmodule PhoenixController do
use Phoenix.Controller
def error(_conn, _params), do: raise("PhoenixError")

def assigns(conn, _params) do
_test = conn.assigns2.test
end
end

defmodule PhoenixRouter do
use Phoenix.Router

get "/error_route", PhoenixController, :error
get "/assigns_route", PhoenixController, :assigns
end

defmodule PhoenixEndpoint do
use Sentry.PlugCapture
use Phoenix.Endpoint, otp_app: :sentry
use Plug.Debugger, otp_app: :sentry

plug Plug.Parsers,
parsers: [:urlencoded, :multipart, :json],
Expand Down Expand Up @@ -215,4 +221,32 @@ defmodule Sentry.PlugCaptureTest do
assert body =~ event_id
assert body =~ ~s{"title":"Testing"}
end

test "handles Erlang error in Plug.Conn.WrapperError" do
bypass = Bypass.open()

Bypass.expect(bypass, fn conn ->
{:ok, body, conn} = Plug.Conn.read_body(conn)
json = Jason.decode!(body)
assert json["culprit"] == "Sentry.PlugCaptureTest.PhoenixController.assigns/2"
Plug.Conn.resp(conn, 200, ~s<{"id": "340"}>)
end)

modify_env(:sentry,
dsn: "http://public:secret@localhost:#{bypass.port}/1",
"#{__MODULE__.PhoenixEndpoint}": [
render_errors: [view: Sentry.ErrorView, accepts: ~w(html)]
]
)

{:ok, _} = PhoenixEndpoint.start_link()

capture_log(fn ->
assert_raise KeyError, fn ->
conn(:get, "/assigns_route")
|> Plug.Conn.put_req_header("throw", "throw")
|> PhoenixEndpoint.call([])
end
end)
end
end

0 comments on commit 3ed8f1a

Please sign in to comment.