diff --git a/lib/sentry/plug.ex b/lib/sentry/plug.ex index c0daadd4..c09d4496 100644 --- a/lib/sentry/plug.ex +++ b/lib/sentry/plug.ex @@ -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 @@ -131,6 +141,8 @@ if Code.ensure_loaded?(Plug) do error_type: kind ) end + + defoverridable handle_errors: 2 end end diff --git a/test/plug_test.exs b/test/plug_test.exs index 3d5e40f3..59f8971f 100644 --- a/test/plug_test.exs +++ b/test/plug_test.exs @@ -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