You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Official Sentry Client for Elixir which provides a simple API to capture exceptions, automatically handle Plug Exceptions and provides a backend for the Elixir Logger.
Elixir 1.7 and Erlang/OTP 21 significantly changed how errors are transmitted (See "Erlang/OTP logger integration" [here](https://elixir-lang.org/blog/2018/07/25/elixir-v1-7-0-released/)). Sentry integrated heavily with Erlang's `:error_logger` module, but it is no longer the suggested path towards handling errors.
7
+
The Official Sentry Client for Elixir which provides a simple API to capture exceptions, automatically handle Plug Exceptions and provides a backend for the Elixir Logger. This documentation represents unreleased features, for documentation on the current release, see [here](https://hexdocs.pm/sentry/readme.html).
13
8
14
-
Sentry 7.x requires Elixir 1.7 and Sentry 6.x will be maintained for applications running prior versions. Documentation for Sentry 6.x can be found [here](https://hexdocs.pm/sentry/6.4.2/readme.html).
15
-
16
-
If you would like to upgrade a project to use Sentry 7.x, see [here](https://gist.github.com/mitchellhenke/4ab6dd8d0ebeaaf9821fb625e0037a4d).
17
9
18
10
## Installation
19
11
@@ -30,26 +22,67 @@ defp deps do
30
22
end
31
23
```
32
24
33
-
### Setup with Plug or Phoenix
25
+
### Setup with Plug and Phoenix
26
+
Capturing errors in Plug applications is done with `Sentry.PlugContext` and `Sentry.PlugCapture`. `Sentry.PlugContext` adds contextual metadata from the current request which is then included in errors that are captured and reported by `Sentry.PlugCapture`.
34
27
35
-
In your Plug.Router or Phoenix.Router, add the following lines:
28
+
If you are using Phoenix, first add `Sentry.PlugCapture` above the `use Phoenix.Endpoint` line in your endpoint file. `Sentry.PlugContext` should be added below `Plug.Parsers`.
36
29
37
30
```diff
38
-
# lib/my_app_web/router.ex
39
-
defmodule MyAppWeb.Router do
40
-
use MyAppWeb, :router
41
-
+ use Plug.ErrorHandler
42
-
+ use Sentry.Plug
31
+
defmodule MyAppWeb.Endpoint
32
+
+ use Sentry.PlugCapture
33
+
use Phoenix.Endpoint, otp_app: :my_app
34
+
# ...
35
+
plug Plug.Parsers,
36
+
parsers: [:urlencoded, :multipart, :json],
37
+
pass: ["*/*"],
38
+
json_decoder: Phoenix.json_library()
39
+
40
+
+ plug Sentry.PlugContext
43
41
```
44
42
45
-
If you are using Phoenix, you can also include [Sentry.Phoenix.Endpoint](https://hexdocs.pm/sentry/Sentry.Phoenix.Endpoint.html) in your Endpoint. This module captures errors occurring in the Phoenix pipeline before the request reaches the Router:
43
+
If you are in a non-Phoenix Plug application, add `Sentry.PlugCapture` at the top of your Plug application, and add `Sentry.PlugContext` below `Plug.Parsers` (if it is in your stack).
46
44
47
45
```diff
48
-
use Phoenix.Endpoint, otp_app: :my_app
49
-
+use Sentry.Phoenix.Endpoint
46
+
defmodule MyApp.Router do
47
+
+ use Sentry.PlugCapture
48
+
use Plug.Router
49
+
# ...
50
+
plug Plug.Parsers,
51
+
parsers: [:urlencoded, :multipart]
52
+
+ plug Sentry.PlugContext
50
53
```
51
54
52
-
More information on why this may be necessary can be found here: https://github.com/getsentry/sentry-elixir/issues/229 and https://github.com/phoenixframework/phoenix/issues/2791
55
+
#### Capturing User Feedback
56
+
57
+
If you would like to capture user feedback as described [here](https://docs.sentry.io/enriching-error-data/user-feedback), the `Sentry.get_last_event_id_and_source()` function can be used to see if Sentry has sent an event within the current Plug process, and the source of that event. `:plug` will be the source for events coming from `Sentry.PlugCapture`. The options described in the Sentry documentation linked above can be encoded into the response as well.
58
+
59
+
An example Phoenix application setup that wanted to display the user feedback form on 500 responses on requests accepting HTML could look like:
60
+
61
+
```elixir
62
+
defmoduleMyAppWeb.ErrorViewdo
63
+
# ...
64
+
defrender("500.html", _assigns) do
65
+
caseSentry.get_last_event_id_and_source() do
66
+
{event_id, :plug} whenis_binary(event_id) ->
67
+
opts =
68
+
# can do %{eventId: event_id, title: "My custom title"}
0 commit comments