Extract HTML rendering from errors.cr to sep views

This commit is contained in:
syeopite 2021-11-14 07:17:25 -08:00
parent 41b7555519
commit 072977537f
No known key found for this signature in database
GPG key ID: 6FA616E5A5294A82
4 changed files with 53 additions and 51 deletions

View file

@ -1,8 +1,8 @@
macro error_template(*args)
error_template_helper(env, locale, {{*args}})
generic_error_template_helper(env, locale, {{*args}})
end
def github_details(summary : String, content : String)
def github_details_backtrace(summary : String, content : String)
details = %(\n<details>)
details += %(\n<summary>#{summary}</summary>)
details += %(\n<p>)
@ -14,63 +14,29 @@ def github_details(summary : String, content : String)
return HTML.escape(details)
end
def error_template_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, exception : Exception)
def generic_error_template_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, exception : Exception)
if exception.is_a?(InfoException)
return error_template_helper(env, locale, status_code, exception.message || "")
return generic_error_template_helper(env, locale, status_code, exception.message || "")
end
env.response.content_type = "text/html"
env.response.status_code = status_code
issue_template = %(Title: `#{exception.message} (#{exception.class})`)
issue_template += %(\nDate: `#{Time::Format::ISO_8601_DATE_TIME.format(Time.utc)}`)
issue_template += %(\nRoute: `#{env.request.resource}`)
issue_template += %(\nVersion: `#{SOFTWARE["version"]} @ #{SOFTWARE["branch"]}`)
# issue_template += github_details("Preferences", env.get("preferences").as(Preferences).to_pretty_json)
issue_template += github_details("Backtrace", exception.inspect_with_backtrace)
error_message = <<-END_HTML
Looks like you've found a bug in Invidious. Please open a new issue
<a href="https://github.com/iv-org/invidious/issues">on GitHub</a>
and include the following text in your message:
<pre style="padding: 20px; background: rgba(0, 0, 0, 0.12345);">#{issue_template}</pre>
END_HTML
next_steps = error_redirect_helper(env, locale)
# HTML rendering.
# We're only keeping the github details creation in here in order to
# avoid manually writing escaped HTML.
backtrace = github_details_backtrace("Backtrace", exception.inspect_with_backtrace)
error_message = rendered "error_pages/generic"
return templated "error"
return templated "error_pages/generic_wrapper"
end
def error_template_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, message : String)
# Handles InfoExceptions
#
# This is mostly for backward compatibility with the `error_template(401, "Message")` types
def generic_error_template_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, message : String)
env.response.content_type = "text/html"
env.response.status_code = status_code
error_message = translate(locale, message)
next_steps = error_redirect_helper(env, locale)
return templated "error"
end
def error_redirect_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil)
request_path = env.request.path
if request_path.starts_with?("/search") || request_path.starts_with?("/watch") ||
request_path.starts_with?("/channel") || request_path.starts_with?("/playlist?list=PL")
next_steps_text = translate(locale, "next_steps_error_message")
refresh = translate(locale, "next_steps_error_message_refresh")
go_to_youtube = translate(locale, "next_steps_error_message_go_to_youtube")
switch_instance = translate(locale, "Switch Invidious Instance")
return <<-END_HTML
<p style="margin-bottom: 4px;">#{next_steps_text}</p>
<ul>
<li>
<a href="#{env.request.resource}">#{refresh}</a>
</li>
<li>
<a href="/redirect?referer=#{env.get("current_page")}">#{switch_instance}</a>
</li>
<li>
<a href="https://youtube.com#{env.request.resource}">#{go_to_youtube}</a>
</li>
</ul>
END_HTML
else
return ""
end
return templated "error_pages/generic_wrapper"
end

View file

@ -0,0 +1,25 @@
<% if env.request.path.starts_with?("/search") || env.request.path.starts_with?("/watch") ||
env.request.path.starts_with?("/channel") || env.request.path.starts_with?("/playlist?list=PL")
%>
<%-
next_steps_text = translate(locale, "next_steps_error_message")
refresh = translate(locale, "next_steps_error_message_refresh")
go_to_youtube = translate(locale, "next_steps_error_message_go_to_youtube")
switch_instance = translate(locale, "Switch Invidious Instance")
%>
<p style="margin-bottom: 4px;"><%=next_steps_text%></p>
<ul>
<li>
<a href="<%=env.request.resource%>"><%=refresh%></a>
</li>
<li>
<a href="/redirect?referer=<%=env.get("current_page")%>"><%=switch_instance%></a>
</li>
<li>
<a href="https://youtube.com<%=env.request.resource%>"><%=go_to_youtube%></a>
</li>
</ul>
<% end %>

View file

@ -0,0 +1,11 @@
<p> Looks like you've found a bug in Invidious. Please open a new issue <a href="https://github.com/iv-org/invidious/issues">on GitHub</a> and include the following text in your message: </p>
<pre style="padding: 20px; background: rgba(0, 0, 0, 0.12345);">
Title: `<%=exception.message%> (<%=exception.class%>)`
Date: `<%=Time::Format::ISO_8601_DATE_TIME.format(Time.utc)%>`
Route: `<%=env.request.resource%>`
Version: `<%=env.request.resource%>`
<%=backtrace%>
</details>
</pre>

View file

@ -4,5 +4,5 @@
<div class="h-box">
<%= error_message %>
<%= next_steps %>
<%= rendered "components/error_pages/next_steps" %>
</div>