Skip to content

Commit 65dc888

Browse files
committed
Do not rely on conditional module definition
The downside of conditional module definition is that it may not work as expected in umbrellas. Given the Sentry Logger configuration in an umbrella project is shared across all apps, if we do: config :logger, :backends, [Sentry.LoggerBackend] then `:sentry` needs to be defined on all dependencies. However, if a dependency is not a web application, Sentry may be compiled before Plug! And if it is compiled before Plug, it wouldn't define Sentry.PlugCapture and friends, which means the web app in the umbrella wouldn't have those modules available.
1 parent 103c902 commit 65dc888

File tree

3 files changed

+199
-201
lines changed

3 files changed

+199
-201
lines changed

lib/sentry/plug_capture.ex

Lines changed: 41 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,49 @@
1-
if Code.ensure_loaded?(Plug) do
2-
defmodule Sentry.PlugCapture do
3-
@moduledoc """
4-
Provides basic functionality to handle and send errors occurring within
5-
Plug applications, including Phoenix.
6-
It is intended for usage with `Sentry.PlugContext`.
7-
8-
#### Usage
9-
In a Phoenix application, it is important to use this module before
10-
the Phoenix endpoint itself. It should be added to your endpoint.ex:
11-
12-
13-
defmodule MyApp.Endpoint
14-
use Sentry.PlugCapture
15-
use Phoenix.Endpoint, otp_app: :my_app
16-
# ...
17-
end
1+
defmodule Sentry.PlugCapture do
2+
@moduledoc """
3+
Provides basic functionality to handle and send errors occurring within
4+
Plug applications, including Phoenix.
5+
It is intended for usage with `Sentry.PlugContext`.
186
19-
In a Plug application, it can be added below your router:
7+
#### Usage
8+
In a Phoenix application, it is important to use this module before
9+
the Phoenix endpoint itself. It should be added to your endpoint.ex:
2010
21-
defmodule MyApp.PlugRouter do
22-
use Plug.Router
23-
use Sentry.PlugCapture
24-
# ...
25-
end
26-
"""
27-
defmacro __using__(_opts) do
28-
quote do
29-
@before_compile Sentry.PlugCapture
11+
12+
defmodule MyApp.Endpoint
13+
use Sentry.PlugCapture
14+
use Phoenix.Endpoint, otp_app: :my_app
15+
# ...
3016
end
17+
18+
In a Plug application, it can be added below your router:
19+
20+
defmodule MyApp.PlugRouter do
21+
use Plug.Router
22+
use Sentry.PlugCapture
23+
# ...
24+
end
25+
"""
26+
defmacro __using__(_opts) do
27+
quote do
28+
@before_compile Sentry.PlugCapture
3129
end
30+
end
3231

33-
defmacro __before_compile__(_) do
34-
quote do
35-
defoverridable call: 2
36-
37-
def call(conn, opts) do
38-
try do
39-
super(conn, opts)
40-
rescue
41-
e in Plug.Conn.WrapperError ->
42-
Sentry.capture_exception(e.reason, stacktrace: e.stack, event_source: :plug)
43-
Plug.Conn.WrapperError.reraise(e)
44-
45-
e ->
46-
Sentry.capture_exception(e, stacktrace: __STACKTRACE__, event_source: :plug)
47-
:erlang.raise(:error, e, __STACKTRACE__)
48-
end
32+
defmacro __before_compile__(_) do
33+
quote do
34+
defoverridable call: 2
35+
36+
def call(conn, opts) do
37+
try do
38+
super(conn, opts)
39+
rescue
40+
e in Plug.Conn.WrapperError ->
41+
Sentry.capture_exception(e.reason, stacktrace: e.stack, event_source: :plug)
42+
Plug.Conn.WrapperError.reraise(e)
43+
44+
e ->
45+
Sentry.capture_exception(e, stacktrace: __STACKTRACE__, event_source: :plug)
46+
:erlang.raise(:error, e, __STACKTRACE__)
4947
end
5048
end
5149
end

0 commit comments

Comments
 (0)