-
-
Notifications
You must be signed in to change notification settings - Fork 199
/
Copy pathexample_plug_application.ex
71 lines (59 loc) · 1.74 KB
/
example_plug_application.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
defmodule Sentry.ExamplePlugApplication do
use Plug.Router
use Sentry.PlugCapture
use Plug.ErrorHandler
import ExUnit.Assertions
alias Sentry.TestHelpers
plug Plug.Parsers, parsers: [:multipart, :urlencoded]
plug Sentry.PlugContext
plug :match
plug :dispatch
@spec child_spec(keyword()) :: Supervisor.child_spec()
def child_spec(opts \\ []) do
case Keyword.get(opts, :server, :cowboy) do
:cowboy ->
Supervisor.child_spec(
{Plug.Cowboy, scheme: :http, plug: __MODULE__, options: [port: 8003]},
[]
)
:bandit ->
Supervisor.child_spec({Bandit, plug: __MODULE__, port: 8003}, [])
end
end
get "/exit_route" do
_ = conn
exit(:test)
end
get "/throw_route" do
_ = conn
throw(:test)
end
get "/spawn_error_route" do
{_pid, ref} = spawn_monitor(fn -> raise "Error" end)
assert_receive {:DOWN, ^ref, _, _, _}
send_resp(conn, 200, "")
end
match "/error_route" do
_ = conn
raise RuntimeError, "Error"
end
def handle_errors(conn, %{kind: _kind, reason: _reason, stack: _stack}) do
response =
case Sentry.get_last_event_id_and_source() do
{event_id, :plug} ->
opts =
%{title: "Testing", eventId: event_id}
|> TestHelpers.encode!()
"""
<script src="https://browser.sentry-cdn.com/5.9.1/bundle.min.js" integrity="sha384-/x1aHz0nKRd6zVUazsV6CbQvjJvr6zQL2CHbQZf3yoLkezyEtZUpqUNnOLW9Nt3v" crossorigin="anonymous"></script>
<script>
Sentry.init({ dsn: '#{inspect(Sentry.Config.dsn())}' });
Sentry.showReportDialog(#{opts})
</script>
"""
_ ->
"error"
end
send_resp(conn, conn.status, response)
end
end