Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix trying to transform erlang error coming from PlugCapture #406

Merged
merged 2 commits into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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