Show user feedback form on server errors (#2617)

* Move Endpoint errors setup to common config

* Implement naive Sentry link resolver

* Implement error report e-mail

* Delete static sentry script

* Implement user feedback form on server errors

* Re-arrange pipe

* Use Sentry.Config.dsn() where applicable

* Fix typo

* Use Map.replace/3
pull/2626/head r2023.01.25-46
Adam Rutkowski 4 years ago committed by GitHub
parent 5bb53c7e35
commit ad12e1ef31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

File diff suppressed because one or more lines are too long

@ -3,7 +3,13 @@ import Config
config :plausible,
ecto_repos: [Plausible.Repo, Plausible.ClickhouseRepo]
config :plausible, PlausibleWeb.Endpoint, pubsub_server: Plausible.PubSub
config :plausible, PlausibleWeb.Endpoint,
pubsub_server: Plausible.PubSub,
render_errors: [
view: PlausibleWeb.ErrorView,
layout: {PlausibleWeb.LayoutView, "focus.html"},
accepts: ~w(html json)
]
# Configures Elixir's Logger
config :logger, :console,

@ -1,10 +1,6 @@
import Config
config :plausible, PlausibleWeb.Endpoint,
render_errors: [
view: PlausibleWeb.ErrorView,
accepts: ~w(html json)
],
server: true,
debug_errors: true,
code_reloader: true,

@ -60,7 +60,7 @@ defmodule Plausible.Application do
end
defp maybe_add_sentry_pool(pool_config) do
case Application.get_env(:sentry, :dsn) do
case Sentry.Config.dsn() do
dsn when is_binary(dsn) ->
Map.put(pool_config, dsn, size: 50)

@ -0,0 +1,26 @@
defmodule PlausibleWeb.ErrorReportController do
use PlausibleWeb, :controller
plug PlausibleWeb.RequireAccountPlug
def submit_error_report(conn, %{
"error" => %{"trace_id" => trace_id, "user_feedback" => feedback}
}) do
reported_by = "#{conn.assigns.current_user.name} <#{conn.assigns.current_user.email}>"
email_template = PlausibleWeb.Email.error_report(reported_by, trace_id, feedback)
Plausible.Mailer.deliver_later(email_template)
thanks(conn)
end
def submit_error_report(conn, _params) do
thanks(conn)
end
defp thanks(conn) do
conn
|> put_view(PlausibleWeb.ErrorView)
|> render("server_error_report_thanks.html", layout: {PlausibleWeb.LayoutView, "focus.html"})
end
end

@ -320,6 +320,21 @@ defmodule PlausibleWeb.Email do
})
end
def error_report(reported_by, trace_id, feedback) do
Map.new()
|> Map.put(:layout, nil)
|> base_email()
|> to("bugs@plausible.io")
|> put_param("ReplyTo", reported_by)
|> tag("sentry")
|> subject("Feedback to Sentry Trace #{trace_id}")
|> render("error_report_email.html", %{
reported_by: reported_by,
feedback: feedback,
trace_id: trace_id
})
end
def base_email(), do: base_email(%{layout: "base_email.html"})
def base_email(%{layout: layout}) do
@ -334,7 +349,6 @@ defmodule PlausibleWeb.Email do
defp maybe_put_layout(email, nil), do: email
defp maybe_put_layout(email, layout) do
email
|> put_html_layout({PlausibleWeb.LayoutView, layout})
put_html_layout(email, {PlausibleWeb.LayoutView, layout})
end
end

@ -139,6 +139,7 @@ defmodule PlausibleWeb.Router do
post "/password/request-reset", AuthController, :password_reset_request
get "/password/reset", AuthController, :password_reset_form
post "/password/reset", AuthController, :password_reset
post "/error_report", ErrorReportController, :submit_error_report
end
scope "/", PlausibleWeb do

@ -0,0 +1,12 @@
<h1>Error report</h1>
<p>
Reported by: <%= @reported_by %>
<br/>
Sentry trace: <a href="<%= sentry_link(@trace_id) %>"><%= @trace_id %></a>
<br/>
</p>
<h2>User feedback:</h2>
<pre>
<%= @feedback %>
</pre>
<br/>

@ -0,0 +1,56 @@
<div class="w-full max-w-3xl mt-4 mx-auto flex">
<div class="max-w-lg w-full mx-auto bg-white dark:bg-gray-800 shadow-lg rounded px-8 pt-6 pb-8 mb-4 mt-8" >
<h2 class="text-xl font-black dark:text-gray-100">Oops, sorry about that...</h2>
<div class="rounded-md bg-yellow-50 dark:bg-transparent dark:border border-yellow-200 p-4 mt-4">
<div class="flex">
<div class="flex-shrink-0">
<svg class="h-5 w-5 text-yellow-400 dark:text-yellow-300" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
<path fill-rule="evenodd" d="M8.257 3.099c.765-1.36 2.722-1.36 3.486 0l5.58 9.92c.75 1.334-.213 2.98-1.742 2.98H4.42c-1.53 0-2.493-1.646-1.743-2.98l5.58-9.92zM11 13a1 1 0 11-2 0 1 1 0 012 0zm-1-8a1 1 0 00-1 1v3a1 1 0 002 0V6a1 1 0 00-1-1z" clip-rule="evenodd" />
</svg>
</div>
<div class="ml-3">
<h3 class="text-sm font-medium text-yellow-800 dark:text-yellow-400">
There has been a server error.
<%= if !assigns[:selfhosted?] do %>
But don't worry, we're on it!
<% end %>
</h3>
<div class="mt-2 text-sm text-yellow-700 dark:text-yellow-300">
<p>
<%= if assigns[:trace_id] && !assigns[:selfhosted?] do %>
If you would like to help, tell us what you were trying to do. Our development team will receive your report.
<% else %>
We have been notified.
<% end %>
</p>
</div>
</div>
</div>
</div>
<%= if !assigns[:selfhosted?] do %>
<%= if assigns[:trace_id] do %>
<div class="my-6">
<%= form_for :error, Routes.error_report_path(PlausibleWeb.Endpoint, :submit_error_report), fn f -> %>
<%= hidden_input f, :trace_id, value: @trace_id %>
<%= label f, "What happened?", class: "block text-sm font-medium text-gray-700 dark:text-gray-300" %>
<%= textarea f, :user_feedback, rows: 5, class: "block w-full max-w-xl border-gray-300 dark:border-gray-700 resize-none shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md dark:bg-gray-900 dark:text-gray-300", placeholder: "I clicked on 'X' and then hit 'Confirm'..." %>
<%= submit "Send feedback →", class: "button mt-4 w-full" %>
<% end %>
</div>
<% end %>
<div class="mb-6 mt-12">
<p class="text-sm text-center">
<%= link("Home page", to: "/", class: "text-indigo-500 mr-8") %>
<%= link("Status page", to: "https://status.plausible.io", class: "mr-8 text-indigo-500") %>
<%= link("Contact us", to: "https://plausible.io/contact", class: "text-indigo-500") %>
</p>
</div>
<% end %>
</div>
</div>

@ -0,0 +1,20 @@
<div class="w-full max-w-3xl mt-4 mx-auto flex">
<div class="max-w-lg w-full mx-auto bg-white dark:bg-gray-800 shadow-lg rounded px-8 pt-6 pb-8 mb-4 mt-8" >
<h2 class="text-xl font-black dark:text-gray-100">Thank you!</h2>
<div class="rounded-md dark:bg-transparent mt-4">
<div class="flex">
<h3 class="text-sm font-medium dark:text-gray-100">
Your report has been submitted.
</h3>
</div>
</div>
<div class="mb-6 mt-12">
<p class="text-sm text-center">
<%= link("Home page", to: "/", class: "text-indigo-500 mr-8") %>
<%= link("Status page", to: "https://status.plausible.io", class: "mr-8 text-indigo-500") %>
<%= link("Contact us", to: "https://plausible.io/contact", class: "text-indigo-500") %>
</p>
</div>
</div>
</div>

@ -18,4 +18,20 @@ defmodule PlausibleWeb.EmailView do
def date_format(date) do
Timex.format!(date, "{D} {Mshort} {YYYY}")
end
def sentry_link(trace_id, dsn \\ Sentry.Config.dsn()) do
search_query = URI.encode_query(%{query: trace_id})
path = "/organizations/sentry/issues/"
if is_binary(dsn) do
dsn
|> URI.parse()
|> Map.replace(:userinfo, nil)
|> Map.replace(:path, path)
|> Map.replace(:query, search_query)
|> URI.to_string()
else
""
end
end
end

@ -22,41 +22,25 @@ defmodule PlausibleWeb.ErrorView do
)
end
def render("500.html", assigns) do
case Sentry.get_last_event_id_and_source() do
{event_id, :plug} when is_binary(event_id) ->
current_user = assigns[:current_user]
opts =
%{
eventId: event_id,
user: %{
name: current_user && current_user.name,
email: current_user && current_user.email
}
}
|> Jason.encode!()
~E"""
<script src="<%= Routes.static_path(@conn, "/js/sentry-bundle-5.9.1-min.js") %>" integrity="sha384-/x1aHz0nKRd6zVUazsV6CbQvjJvr6zQL2CHbQZf3yoLkezyEtZUpqUNnOLW9Nt3v" crossorigin="anonymous"></script>
<script>
Sentry.init({ dsn: '<%= Sentry.Config.dsn() %>' });
Sentry.showReportDialog(<%= raw opts %>)
</script>
"""
def render(<<"5", _error_5xx::binary-size(2), ".html">>, assigns) do
current_user = assigns[:current_user]
selfhosted? = Plausible.Release.selfhost?()
last_event = Sentry.get_last_event_id_and_source()
case {selfhosted?, current_user, last_event} do
{false, current_user, {event_id, :plug}}
when is_binary(event_id) and not is_nil(current_user) ->
opts = %{
trace_id: event_id,
user_name: current_user.name,
user_email: current_user.email,
selfhosted?: selfhosted?
}
render("server_error.html", Map.merge(opts, assigns))
_ ->
render(
"error.html",
Map.merge(
%{
layout: false,
status: 500,
message: "Oops! Looks like we're having server issues"
},
assigns
)
)
render("server_error.html", Map.put(assigns, :selfhosted?, selfhosted?))
end
end

@ -0,0 +1,152 @@
defmodule PlausibleWeb.ErrorReportControllerTest do
use PlausibleWeb.ConnCase, async: true
use Bamboo.Test
import Phoenix.View
alias PlausibleWeb.Endpoint
alias PlausibleWeb.ErrorView
alias PlausibleWeb.EmailView
describe "building sentry links" do
test "no dsn configured" do
assert EmailView.sentry_link("some-trace") == ""
end
test "with dsn" do
sample_dsn = "https://foobarbaz@somehost.example.com/1"
assert EmailView.sentry_link("some-trace", sample_dsn) ==
"https://somehost.example.com/organizations/sentry/issues/?query=some-trace"
end
end
describe "Logged in" do
setup [:create_user, :log_in]
for error <- ["500", "502", "503", "504"] do
test "renders the form when sentry metadata present: #{error}", %{user: user} do
Sentry.put_last_event_id_and_source("some-event-id", :plug)
action_path = Routes.error_report_path(Endpoint, :submit_error_report)
assert html =
render_to_string(ErrorView, unquote(error) <> ".html", %{
current_user: user
})
assert form_exists?(html, action_path)
assert submit_button(html, "form[action=\"#{action_path}\"]")
assert element_exists?(
html,
"input[type=\"hidden\"][name=\"error[trace_id]\"][value=\"some-event-id\"]"
)
end
end
test "submitting the feedback form for authenticated user", %{conn: conn, user: user} do
action_path = Routes.error_report_path(Endpoint, :submit_error_report)
conn =
post(conn, action_path, %{
"error" => %{"trace_id" => "some-trace-id", "user_feedback" => "Guiz pls fix"}
})
assert html = html_response(conn, 200)
assert html =~ "Your report has been submitted"
reply_to = "#{user.name} <#{user.email}>"
assert_delivered_email_matches(%{
to: [nil: "bugs@plausible.io"],
subject: "Feedback to Sentry Trace some-trace-id",
private: %{
message_params: %{
"ReplyTo" => ^reply_to
}
}
})
end
test "submitting no feedback for authenticated user", %{conn: conn} do
action_path = Routes.error_report_path(Endpoint, :submit_error_report)
conn =
post(conn, action_path, %{
"error" => %{"trace_id" => "some-trace-id"}
})
assert html = html_response(conn, 200)
assert html =~ "Your report has been submitted"
refute_email_delivered_with(%{
subject: "Feedback to Sentry Trace some-trace-id"
})
end
test "email renders properly" do
assert email = PlausibleWeb.Email.error_report("Alice Bob", "some-trace", "hello world")
text =
email
|> Map.fetch!(:html_body)
|> Floki.parse_document!()
|> Floki.text()
assert text =~ "Reported by: Alice Bob"
assert text =~ "Sentry trace: some-trace"
assert text =~ "User feedback:\nhello world"
end
end
describe "Not logged in" do
test "submitting the feedback form for unauthenticated user", %{conn: conn} do
action_path = Routes.error_report_path(Endpoint, :submit_error_report)
conn =
post(conn, action_path, %{
"error" => %{"trace_id" => "some-trace-id", "user_feedback" => "Guiz pls fix"}
})
assert redirected_to(conn) == "/login"
end
for error <- ["500", "502", "503", "504"] do
test "renders the error page, no form, when sentry metadata absent: #{error}", %{conn: conn} do
conn =
conn
|> bypass_through(PlausibleWeb.Router, [:browser])
|> get("/")
assert html = render_to_string(ErrorView, unquote(error) <> ".html", %{conn: conn})
text = html |> Floki.parse_document!() |> Floki.text()
assert text =~ "There has been a server error"
assert text =~ "But don't worry, we're on it!"
action_path = Routes.error_report_path(Endpoint, :submit_error_report)
refute form_exists?(html, action_path)
end
end
end
defp form_exists?(html, action_path) do
element_exists?(html, "form[action=\"" <> action_path <> "\"]")
end
defp element_exists?(html, selector) do
html
|> find(selector)
|> Enum.empty?()
|> Kernel.not()
end
defp submit_button(html, form) do
find(html, "#{form} button[type=\"submit\"]")
end
defp find(html, value) do
html
|> Floki.parse_document!()
|> Floki.find(value)
end
end
Loading…
Cancel
Save