Skip to content

Commit 9f6e4a8

Browse files
Merge pull request #387 from getsentry/error-feedback-options
allow configuring feedback form
2 parents 0097850 + e981f7c commit 9f6e4a8

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

lib/sentry/plug.ex

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,17 @@ if Code.ensure_loaded?(Plug) do
100100
101101
### Collect User Feedback after Error
102102
103-
Sentry allows collecting user feedback after they hit an error.
104-
Information about it is available [here](https://docs.sentry.io/enriching-error-data/user-feedback).
105-
If a Plug request experiences an error that Sentry is reporting, and the request
103+
Sentry allows collecting user feedback after they hit an error, and is configured under the
104+
`:collect_feedback` key. Information and configuration options are available
105+
[here](https://docs.sentry.io/enriching-error-data/user-feedback).
106+
107+
When enabled, if a Plug request experiences an error that Sentry is reporting and the request
106108
accepts the content-type "text/html" or "*/*", the feedback form will be rendered.
107-
The configuration is limited to the defaults at the moment, but it can be enabled with:
109+
The feedback form can be enabled by passing `[enabled: true]`. The form also supports
110+
all of the configuration in the Sentry documentation linked above. These options can be
111+
passed as a configuration map under the `:options` key. Example:
108112
109-
use Sentry.Plug, collect_feedback: [enabled: true]
113+
use Sentry.Plug, collect_feedback: [enabled: true, options: %{title: "Sorry about that!"}]
110114
"""
111115

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

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

145150
collect_feedback_enabled = unquote(collect_feedback_enabled)
151+
collect_feedback_opts = unquote(collect_feedback_opts)
146152
request = Sentry.Plug.build_request_interface_data(conn, opts)
147153
exception = Exception.normalize(kind, reason, stack)
148154

@@ -164,7 +170,7 @@ if Code.ensure_loaded?(Plug) do
164170
result: :sync
165171
)
166172

167-
render_sentry_feedback(conn, result)
173+
render_sentry_feedback(conn, result, collect_feedback_opts)
168174
else
169175
Sentry.capture_exception(
170176
exception,
@@ -176,7 +182,11 @@ if Code.ensure_loaded?(Plug) do
176182
end
177183
end
178184

179-
defp render_sentry_feedback(conn, {:ok, id}) do
185+
defp render_sentry_feedback(conn, {:ok, id}, opts) do
186+
encoded_opts =
187+
Map.put(opts, :eventId, id)
188+
|> Jason.encode!()
189+
180190
html = """
181191
<!DOCTYPE HTML>
182192
<html lang="en">
@@ -185,7 +195,7 @@ if Code.ensure_loaded?(Plug) do
185195
<script src="https://browser.sentry-cdn.com/5.9.1/bundle.min.js" integrity="sha384-/x1aHz0nKRd6zVUazsV6CbQvjJvr6zQL2CHbQZf3yoLkezyEtZUpqUNnOLW9Nt3v" crossorigin="anonymous"></script>
186196
<script>
187197
Sentry.init({ dsn: '#{Sentry.Config.dsn()}' });
188-
Sentry.showReportDialog({ eventId: '#{id}' })
198+
Sentry.showReportDialog(#{encoded_opts})
189199
</script>
190200
</head>
191201
<body>
@@ -197,7 +207,7 @@ if Code.ensure_loaded?(Plug) do
197207
|> Plug.Conn.send_resp(conn.status, html)
198208
end
199209

200-
defp render_sentry_feedback(_conn, _result), do: nil
210+
defp render_sentry_feedback(_conn, _result, _opts), do: nil
201211

202212
defoverridable handle_errors: 2
203213
end

test/plug_test.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ defmodule Sentry.PlugTest do
167167
defmodule CollectFeedbackApp do
168168
use Plug.Router
169169
use Plug.ErrorHandler
170-
use Sentry.Plug, collect_feedback: [enabled: true]
170+
use Sentry.Plug, collect_feedback: [enabled: true, options: %{title: "abc-123"}]
171171
plug :match
172172
plug :dispatch
173173
forward("/", to: Sentry.ExampleApp)
@@ -195,6 +195,7 @@ defmodule Sentry.PlugTest do
195195
assert {500, _headers, body} = sent_resp(conn)
196196
assert body =~ "340"
197197
assert body =~ "sentry-cdn"
198+
assert body =~ ~s{"title":"abc-123"}
198199
end
199200

200201
defp update_req_cookie(conn, name, value) do

0 commit comments

Comments
 (0)