diff --git a/src/invidious/errors/errors.cr b/src/invidious/errors/errors.cr index f970e234..2a9cbefd 100644 --- a/src/invidious/errors/errors.cr +++ b/src/invidious/errors/errors.cr @@ -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 += %(\n#{summary}) details += %(\n

) @@ -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 - on GitHub - and include the following text in your message: -

#{issue_template}
- 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 -

#{next_steps_text}

- - END_HTML - else - return "" - end + return templated "error_pages/generic_wrapper" end diff --git a/src/invidious/views/components/error_pages/next_steps.ecr b/src/invidious/views/components/error_pages/next_steps.ecr new file mode 100644 index 00000000..e90590a6 --- /dev/null +++ b/src/invidious/views/components/error_pages/next_steps.ecr @@ -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") +%> + +

<%=next_steps_text%>

+ + +<% end %> \ No newline at end of file diff --git a/src/invidious/views/error_pages/generic.ecr b/src/invidious/views/error_pages/generic.ecr new file mode 100644 index 00000000..e38a4bf3 --- /dev/null +++ b/src/invidious/views/error_pages/generic.ecr @@ -0,0 +1,11 @@ + +

Looks like you've found a bug in Invidious. Please open a new issue on GitHub and include the following text in your message:

+ +
+Title: `<%=exception.message%> (<%=exception.class%>)`
+Date: `<%=Time::Format::ISO_8601_DATE_TIME.format(Time.utc)%>`
+Route: `<%=env.request.resource%>`
+Version: `<%=env.request.resource%>`
+<%=backtrace%>
+
+ diff --git a/src/invidious/views/error.ecr b/src/invidious/views/error_pages/generic_wrapper.ecr similarity index 70% rename from src/invidious/views/error.ecr rename to src/invidious/views/error_pages/generic_wrapper.ecr index 04eb74d5..e6729a23 100644 --- a/src/invidious/views/error.ecr +++ b/src/invidious/views/error_pages/generic_wrapper.ecr @@ -4,5 +4,5 @@
<%= error_message %> - <%= next_steps %> + <%= rendered "components/error_pages/next_steps" %>