File tree Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Original file line number Diff line number Diff line change @@ -15,6 +15,16 @@ if Code.ensure_loaded?(Plug) do
15
15
use Plug.ErrorHandler
16
16
use Sentry.Plug
17
17
18
+ Note that using Sentry.Plug will override default behaviour of Plug.ErrorHandler - it will
19
+ no longer write "Something went wrong" as a response.
20
+ If you want to retain this behaviour, or in general to add custom logic on top of sending event to sentry,
21
+ you can do it like this:
22
+
23
+ defp handle_errors(conn, %{kind: _kind, reason: _reason, stack: _stack} = error) do
24
+ super(conn, error)
25
+ send_resp(conn, conn.status, "Something went wrong")
26
+ end
27
+
18
28
### Sending Post Body Params
19
29
20
30
In order to send post body parameters you should first scrub them of sensitive
@@ -131,6 +141,8 @@ if Code.ensure_loaded?(Plug) do
131
141
error_type: kind
132
142
)
133
143
end
144
+
145
+ defoverridable handle_errors: 2
134
146
end
135
147
end
136
148
Original file line number Diff line number Diff line change @@ -17,6 +17,38 @@ defmodule Sentry.PlugTest do
17
17
)
18
18
end
19
19
20
+ test "overriding handle_errors/2" do
21
+ Code . compile_string ( """
22
+ defmodule DefaultConfigApp do
23
+ use Plug.Router
24
+ use Plug.ErrorHandler
25
+ use Sentry.Plug
26
+ plug :match
27
+ plug :dispatch
28
+ forward("/", to: Sentry.ExampleApp)
29
+
30
+ defp handle_errors(conn, %{kind: _kind, reason: _reason, stack: _stack} = error) do
31
+ super(conn, error)
32
+ send_resp(conn, conn.status, "Something went terribly wrong")
33
+ end
34
+ end
35
+ """ )
36
+
37
+ bypass = Bypass . open ( )
38
+
39
+ Bypass . expect ( bypass , fn conn ->
40
+ Plug.Conn . resp ( conn , 200 , ~s< {"id": "340"}> )
41
+ end )
42
+
43
+ modify_env ( :sentry , dsn: "http://public:secret@localhost:#{ bypass . port } /1" )
44
+
45
+ conn = conn ( :post , "/error_route" , % { } )
46
+ assert_raise ( RuntimeError , "Error" , fn ->
47
+ DefaultConfigApp . call ( conn , [ ] )
48
+ end )
49
+ assert { 500 , _headers , "Something went terribly wrong" } = sent_resp ( conn )
50
+ end
51
+
20
52
test "default data scrubbing" do
21
53
Code . compile_string ( """
22
54
defmodule DefaultConfigApp do
You can’t perform that action at this time.
0 commit comments