|
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`. |
18 | 6 |
|
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: |
20 | 10 |
|
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 | + # ... |
30 | 16 | 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 |
31 | 29 | end |
| 30 | + end |
32 | 31 |
|
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__) |
49 | 47 | end |
50 | 48 | end |
51 | 49 | end |
|
0 commit comments