Improve Backend Error page

Mostly handle things properly when the front-end is missing

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2021-03-09 19:39:54 +01:00
parent 2133287e26
commit 57662aa690
49 changed files with 4551 additions and 4284 deletions

View File

@@ -10,7 +10,6 @@
font-family: BlinkMacSystemFont, Roboto, Oxygen, Ubuntu, Cantarell, Segoe UI, Fira Sans, Droid Sans, Helvetica Neue, Helvetica, Arial, sans-serif;
background: #efeef4;
position: absolute;
text-align: center;
color: #3c376e;
width: 100%;
height: 100%;
@@ -18,6 +17,12 @@
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
margin: 0;
}
body.error .dialog {
text-align: center;
}
body.error .dialog h1 {
@@ -43,8 +48,30 @@
</div>
<div class="error__message">
<h1><%= gettext "We're sorry, but something went wrong on our end." %></h1>
<p><%= gettext "The Mobilizon server seems to be temporarily down." %></p>
<p>
<%= gettext("The Mobilizon server %{instance} seems to be temporarily down.", instance: "<b>#{@instance}</b>") |> raw %><br />
<%= if is_nil(@contact) do %>
<%= gettext("If the issue persists, you may try to contact the server administrator.") %>
<% else %>
<%= gettext("If the issue persists, you may contact the server administrator at %{contact}.", contact: cond do
String.contains?(@contact, "@") -> "<a href=\"mailto:#{@contact}\">#{@contact}</a>"
String.match?(@contact, ~r/^https?:\/\/.*/) -> "<a href=\"#{@contact}\">#{@contact}</a>"
true -> @contact
end) |> raw %>
<% end %>
</p>
</div>
</main>
<%= if length(@details) > 0 do %>
<details>
<summary style="font-size: 1.25rem;"><%= gettext("Technical details") %></summary>
<pre>
<%= for detail <- @details do %>
<%= detail %><br>
<% end %>
</pre>
</details>
<% end %>
</body>
</html>

View File

@@ -23,7 +23,12 @@ defmodule Mobilizon.Web.AuthView do
Tag.tag(:meta, name: "auth-user-actor-id", content: user_actor_id)
]
tags = Instance.build_tags() ++ info_tags
inject_tags(tags, get_locale(conn))
with tags <- Instance.build_tags() ++ info_tags,
{:ok, html} <- inject_tags(tags, get_locale(conn)) do
html
else
{:error, error} ->
return_error(conn, error)
end
end
end

View File

@@ -7,8 +7,13 @@ defmodule Mobilizon.Web.ErrorView do
import Mobilizon.Web.Views.Utils
def render("404.html", %{conn: conn}) do
tags = Instance.build_tags()
inject_tags(tags, get_locale(conn))
with tags <- Instance.build_tags(),
{:ok, html} <- inject_tags(tags, get_locale(conn)) do
html
else
{:error, error} ->
return_error(conn, error)
end
end
def render("404.json", _assigns) do
@@ -38,12 +43,18 @@ defmodule Mobilizon.Web.ErrorView do
}
end
def render("500.html", _assigns) do
def render("500.html", assigns) do
Mobilizon.Config.instance_config()
|> Keyword.get(:default_language, "en")
|> Gettext.put_locale()
render("500_page.html", %{})
assigns =
assigns
|> Map.update(:details, [], & &1)
|> Map.put(:instance, Mobilizon.Config.instance_name())
|> Map.put(:contact, Mobilizon.Config.contact())
render("500_page.html", assigns)
end
# In case no render clause matches or no

View File

@@ -63,16 +63,26 @@ defmodule Mobilizon.Web.PageView do
def render(page, %{object: object, conn: conn} = _assigns)
when page in ["actor.html", "event.html", "comment.html", "post.html"] do
locale = get_locale(conn)
tags = object |> Metadata.build_tags(locale)
inject_tags(tags, locale)
with locale <- get_locale(conn),
tags <- object |> Metadata.build_tags(locale),
{:ok, html} <- inject_tags(tags, locale) do
html
else
{:error, error} ->
return_error(conn, error)
end
end
# Discussions are private, no need to embed metadata
def render("discussion.html", params), do: render("index.html", params)
def render("index.html", %{conn: conn}) do
tags = Instance.build_tags()
inject_tags(tags, get_locale(conn))
with tags <- Instance.build_tags(),
{:ok, html} <- inject_tags(tags, get_locale(conn)) do
html
else
{:error, error} ->
return_error(conn, error)
end
end
end

View File

@@ -4,18 +4,38 @@ defmodule Mobilizon.Web.Views.Utils do
"""
alias Mobilizon.Service.Metadata.Utils, as: MetadataUtils
import Mobilizon.Web.Gettext, only: [dgettext: 2]
import Plug.Conn, only: [put_status: 2, halt: 1]
@index_file_path Path.join(Application.app_dir(:mobilizon, "priv/static"), "index.html")
# sobelow_skip ["Traversal.FileModule"]
@spec inject_tags(Enum.t(), String.t()) :: {:safe, String.t()}
@spec inject_tags(Enum.t(), String.t()) :: {:ok, {:safe, String.t()}}
def inject_tags(tags, locale \\ "en") do
with {:ok, index_content} <- File.read(index_file_path()) do
do_replacements(index_content, MetadataUtils.stringify_tags(tags), locale)
with {:exists, true} <- {:exists, File.exists?(@index_file_path)},
{:ok, index_content} <- File.read(@index_file_path),
safe <- do_replacements(index_content, MetadataUtils.stringify_tags(tags), locale) do
{:ok, {:safe, safe}}
else
{:exists, false} -> {:error, :index_not_found}
end
end
@spec index_file_path :: String.t()
defp index_file_path do
Path.join(Application.app_dir(:mobilizon, "priv/static"), "index.html")
@spec return_error(Plug.Conn.t(), atom()) :: Plug.Conn.t()
def return_error(conn, error) do
conn
|> put_status(500)
|> Phoenix.Controller.put_view(Mobilizon.Web.ErrorView)
|> Phoenix.Controller.render("500.html", %{details: details(error)})
|> halt()
end
defp details(:index_not_found) do
[dgettext("errors", "Index file not found. You need to recompile the front-end.")]
end
defp details(_) do
[]
end
@spec replace_meta(String.t(), String.t()) :: String.t()
@@ -25,12 +45,11 @@ defmodule Mobilizon.Web.Views.Utils do
|> String.replace("<meta name=\"server-injected-data\" />", tags)
end
@spec do_replacements(String.t(), String.t(), String.t()) :: {:safe, String.t()}
@spec do_replacements(String.t(), String.t(), String.t()) :: String.t()
defp do_replacements(index_content, tags, locale) do
index_content
|> replace_meta(tags)
|> String.replace("<html lang=\"en\">", "<html lang=\"#{locale}\">")
|> (&{:safe, &1}).()
end
@spec get_locale(Conn.t()) :: String.t()