Skip to content

Commit

Permalink
allow configuring feedback form
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellhenke committed Mar 2, 2020
1 parent 0097850 commit e981f7c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
28 changes: 19 additions & 9 deletions lib/sentry/plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,17 @@ if Code.ensure_loaded?(Plug) do
### Collect User Feedback after Error
Sentry allows collecting user feedback after they hit an error.
Information about it is available [here](https://docs.sentry.io/enriching-error-data/user-feedback).
If a Plug request experiences an error that Sentry is reporting, and the request
Sentry allows collecting user feedback after they hit an error, and is configured under the
`:collect_feedback` key. Information and configuration options are available
[here](https://docs.sentry.io/enriching-error-data/user-feedback).
When enabled, if a Plug request experiences an error that Sentry is reporting and the request
accepts the content-type "text/html" or "*/*", the feedback form will be rendered.
The configuration is limited to the defaults at the moment, but it can be enabled with:
The feedback form can be enabled by passing `[enabled: true]`. The form also supports
all of the configuration in the Sentry documentation linked above. These options can be
passed as a configuration map under the `:options` key. Example:
use Sentry.Plug, collect_feedback: [enabled: true]
use Sentry.Plug, collect_feedback: [enabled: true, options: %{title: "Sorry about that!"}]
"""

@default_plug_request_id_header "x-request-id"
Expand All @@ -120,6 +124,7 @@ if Code.ensure_loaded?(Plug) do
request_id_header = Keyword.get(env, :request_id_header)
collect_feedback = Keyword.get(env, :collect_feedback, [])
collect_feedback_enabled = Keyword.get(collect_feedback, :enabled, false)
collect_feedback_opts = Keyword.get(collect_feedback, :options, Macro.escape(%{}))

quote do
# Ignore 404s for Plug routes
Expand All @@ -143,6 +148,7 @@ if Code.ensure_loaded?(Plug) do
]

collect_feedback_enabled = unquote(collect_feedback_enabled)
collect_feedback_opts = unquote(collect_feedback_opts)
request = Sentry.Plug.build_request_interface_data(conn, opts)
exception = Exception.normalize(kind, reason, stack)

Expand All @@ -164,7 +170,7 @@ if Code.ensure_loaded?(Plug) do
result: :sync
)

render_sentry_feedback(conn, result)
render_sentry_feedback(conn, result, collect_feedback_opts)
else
Sentry.capture_exception(
exception,
Expand All @@ -176,7 +182,11 @@ if Code.ensure_loaded?(Plug) do
end
end

defp render_sentry_feedback(conn, {:ok, id}) do
defp render_sentry_feedback(conn, {:ok, id}, opts) do
encoded_opts =
Map.put(opts, :eventId, id)
|> Jason.encode!()

html = """
<!DOCTYPE HTML>
<html lang="en">
Expand All @@ -185,7 +195,7 @@ if Code.ensure_loaded?(Plug) do
<script src="https://browser.sentry-cdn.com/5.9.1/bundle.min.js" integrity="sha384-/x1aHz0nKRd6zVUazsV6CbQvjJvr6zQL2CHbQZf3yoLkezyEtZUpqUNnOLW9Nt3v" crossorigin="anonymous"></script>
<script>
Sentry.init({ dsn: '#{Sentry.Config.dsn()}' });
Sentry.showReportDialog({ eventId: '#{id}' })
Sentry.showReportDialog(#{encoded_opts})
</script>
</head>
<body>
Expand All @@ -197,7 +207,7 @@ if Code.ensure_loaded?(Plug) do
|> Plug.Conn.send_resp(conn.status, html)
end

defp render_sentry_feedback(_conn, _result), do: nil
defp render_sentry_feedback(_conn, _result, _opts), do: nil

defoverridable handle_errors: 2
end
Expand Down
3 changes: 2 additions & 1 deletion test/plug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ defmodule Sentry.PlugTest do
defmodule CollectFeedbackApp do
use Plug.Router
use Plug.ErrorHandler
use Sentry.Plug, collect_feedback: [enabled: true]
use Sentry.Plug, collect_feedback: [enabled: true, options: %{title: "abc-123"}]
plug :match
plug :dispatch
forward("/", to: Sentry.ExampleApp)
Expand Down Expand Up @@ -195,6 +195,7 @@ defmodule Sentry.PlugTest do
assert {500, _headers, body} = sent_resp(conn)
assert body =~ "340"
assert body =~ "sentry-cdn"
assert body =~ ~s{"title":"abc-123"}
end

defp update_req_cookie(conn, name, value) do
Expand Down

0 comments on commit e981f7c

Please sign in to comment.