Skip to content
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
12 changes: 12 additions & 0 deletions lib/sentry/plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ if Code.ensure_loaded?(Plug) do
use Plug.ErrorHandler
use Sentry.Plug

Note that using Sentry.Plug will override default behaviour of Plug.ErrorHandler - it will
no longer write "Something went wrong" as a response.
If you want to retain this behaviour, or in general to add custom logic on top of sending event to sentry,
you can do it like this:

defp handle_errors(conn, %{kind: _kind, reason: _reason, stack: _stack} = error) do
super(conn, error)
send_resp(conn, conn.status, "Something went wrong")
end

### Sending Post Body Params

In order to send post body parameters you should first scrub them of sensitive
Expand Down Expand Up @@ -131,6 +141,8 @@ if Code.ensure_loaded?(Plug) do
error_type: kind
)
end

defoverridable handle_errors: 2
end
end

Expand Down
34 changes: 34 additions & 0 deletions test/plug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,40 @@ defmodule Sentry.PlugTest do
)
end

test "overriding handle_errors/2" do
Code.compile_string("""
defmodule DefaultConfigApp do
use Plug.Router
use Plug.ErrorHandler
use Sentry.Plug
plug :match
plug :dispatch
forward("/", to: Sentry.ExampleApp)

defp handle_errors(conn, %{kind: _kind, reason: _reason, stack: _stack} = error) do
super(conn, error)
send_resp(conn, conn.status, "Something went terribly wrong")
end
end
""")

bypass = Bypass.open()

Bypass.expect(bypass, fn conn ->
Plug.Conn.resp(conn, 200, ~s<{"id": "340"}>)
end)

modify_env(:sentry, dsn: "http://public:secret@localhost:#{bypass.port}/1")

conn = conn(:post, "/error_route", %{})

assert_raise(RuntimeError, "Error", fn ->
DefaultConfigApp.call(conn, [])
end)

assert {500, _headers, "Something went terribly wrong"} = sent_resp(conn)
end

test "default data scrubbing" do
Code.compile_string("""
defmodule DefaultConfigApp do
Expand Down