2022-01-19 18:47:54 +01:00
# -------------------
# Issue template
# -------------------
2020-11-30 10:59:21 +01:00
macro error_template ( * args )
2023-11-08 09:19:24 +01:00
error_template_helper ( env , {{ args . splat }} )
2020-11-30 10:59:21 +01:00
end
2020-12-01 13:41:19 +01:00
def github_details ( summary : String , content : String )
details = %( \n <details> )
details += %( \n <summary> #{ summary } </summary> )
details += %( \n <p> )
details += %( \n \n ``` \n )
details += content . strip
details += %( \n ``` )
details += %( \n </p> )
details += %( \n </details> )
return HTML . escape ( details )
end
2022-01-19 18:47:54 +01:00
def error_template_helper ( env : HTTP :: Server :: Context , status_code : Int32 , exception : Exception )
2020-11-30 10:59:21 +01:00
if exception . is_a? ( InfoException )
2022-01-19 18:47:54 +01:00
return error_template_helper ( env , status_code , exception . message || " " )
2020-11-30 10:59:21 +01:00
end
2021-11-02 15:43:26 +01:00
2022-01-19 18:47:54 +01:00
locale = env . get ( " preferences " ) . as ( Preferences ) . locale
2021-01-04 05:24:08 +01:00
env . response . content_type = " text/html "
2020-11-30 10:59:21 +01:00
env . response . status_code = status_code
2021-11-02 15:43:26 +01:00
issue_title = " #{ exception . message } ( #{ exception . class } ) "
2022-02-08 01:48:44 +01:00
issue_template = <<-TEXT
Title : ` #{ HTML . escape ( issue_title ) } `
Date : ` #{ Time :: Format :: ISO_8601_DATE_TIME . format ( Time . utc ) } `
Route : ` #{ HTML . escape ( env . request . resource ) } `
Version : ` #{ SOFTWARE [ " version " ] } @ #{ SOFTWARE [ " branch " ] } `
TEXT
2022-05-01 13:40:02 +02:00
issue_template += github_details ( " Backtrace " , exception . inspect_with_backtrace )
2021-11-02 15:43:26 +01:00
# URLs for the error message below
2022-04-09 08:20:28 +02:00
url_faq = " https://github.com/iv-org/documentation/blob/master/docs/faq.md "
2021-11-02 15:43:26 +01:00
url_search_issues = " https://github.com/iv-org/invidious/issues "
2022-01-10 22:01:12 +01:00
url_switch = " https://redirect.invidious.io " + env . request . resource
2021-11-02 15:43:26 +01:00
url_new_issue = " https://github.com/iv-org/invidious/issues/new "
url_new_issue += " ?labels=bug&template=bug_report.md&title= "
url_new_issue += URI . encode_www_form ( " [Bug] " + issue_title )
2020-11-30 10:59:21 +01:00
error_message = <<-END_HTML
2021-11-02 15:43:26 +01:00
< div class = " error_message " >
< h2 > #{translate(locale, "crash_page_you_found_a_bug")}</h2>
< br / > < br / >
2022-01-10 22:01:12 +01:00
< p > < b > #{translate(locale, "crash_page_before_reporting")}</b></p>
2021-11-02 15:43:26 +01:00
< ul >
2022-01-17 21:47:29 +01:00
< li > #{translate(locale, "crash_page_refresh", env.request.resource)}</li>
< li > #{translate(locale, "crash_page_switch_instance", url_switch)}</li>
2021-11-02 15:43:26 +01:00
< li > #{translate(locale, "crash_page_read_the_faq", url_faq)}</li>
< li > #{translate(locale, "crash_page_search_issue", url_search_issues)}</li>
< / ul>
< br / >
< p > #{translate(locale, "crash_page_report_issue", url_new_issue)}</p>
< ! - - TODO : Add a " copy to clipboard " button - - >
< pre style = " padding: 20px; background: rgba(0, 0, 0, 0.12345); " > #{issue_template}</pre>
< / div>
2020-11-30 10:59:21 +01:00
END_HTML
2021-05-16 07:01:46 +02:00
2022-01-10 22:01:12 +01:00
# Don't show the usual "next steps" widget. The same options are
# proposed above the error message, just worded differently.
next_steps = " "
2021-05-16 07:01:46 +02:00
2020-11-30 10:59:21 +01:00
return templated " error "
end
2022-01-19 18:47:54 +01:00
def error_template_helper ( env : HTTP :: Server :: Context , status_code : Int32 , message : String )
2021-01-04 05:24:08 +01:00
env . response . content_type = " text/html "
2020-11-30 10:59:21 +01:00
env . response . status_code = status_code
2022-01-19 18:47:54 +01:00
locale = env . get ( " preferences " ) . as ( Preferences ) . locale
2020-11-30 10:59:21 +01:00
error_message = translate ( locale , message )
2022-01-19 18:47:54 +01:00
next_steps = error_redirect_helper ( env )
2020-11-30 10:59:21 +01:00
return templated " error "
end
2022-01-19 18:47:54 +01:00
# -------------------
# Atom feeds
# -------------------
2020-11-30 10:59:21 +01:00
macro error_atom ( * args )
2023-11-08 09:49:37 +01:00
error_atom_helper ( env , {{ args . splat }} )
2020-11-30 10:59:21 +01:00
end
2022-01-19 18:47:54 +01:00
def error_atom_helper ( env : HTTP :: Server :: Context , status_code : Int32 , exception : Exception )
2020-11-30 10:59:21 +01:00
if exception . is_a? ( InfoException )
2022-01-19 18:47:54 +01:00
return error_atom_helper ( env , status_code , exception . message || " " )
2020-11-30 10:59:21 +01:00
end
2022-01-19 18:47:54 +01:00
2020-11-30 10:59:21 +01:00
env . response . content_type = " application/atom+xml "
env . response . status_code = status_code
2022-01-19 18:47:54 +01:00
2020-11-30 10:59:21 +01:00
return " <error> #{ exception . inspect_with_backtrace } </error> "
end
2022-01-19 18:47:54 +01:00
def error_atom_helper ( env : HTTP :: Server :: Context , status_code : Int32 , message : String )
2020-11-30 10:59:21 +01:00
env . response . content_type = " application/atom+xml "
env . response . status_code = status_code
2022-01-19 18:47:54 +01:00
2020-11-30 10:59:21 +01:00
return " <error> #{ message } </error> "
end
2022-01-19 18:47:54 +01:00
# -------------------
# JSON
# -------------------
2020-11-30 10:59:21 +01:00
macro error_json ( * args )
2023-11-08 09:42:46 +01:00
error_json_helper ( env , {{ args . splat }} )
2020-11-30 10:59:21 +01:00
end
2022-01-19 22:15:43 +01:00
def error_json_helper (
env : HTTP :: Server :: Context ,
status_code : Int32 ,
exception : Exception ,
additional_fields : Hash ( String , Object ) | Nil = nil
)
2020-11-30 10:59:21 +01:00
if exception . is_a? ( InfoException )
2022-01-19 18:47:54 +01:00
return error_json_helper ( env , status_code , exception . message || " " , additional_fields )
2020-11-30 10:59:21 +01:00
end
2022-01-19 18:47:54 +01:00
2020-11-30 10:59:21 +01:00
env . response . content_type = " application/json "
env . response . status_code = status_code
2022-01-19 18:47:54 +01:00
2020-11-30 10:59:21 +01:00
error_message = { " error " = > exception . message , " errorBacktrace " = > exception . inspect_with_backtrace }
2022-01-19 18:47:54 +01:00
2020-11-30 10:59:21 +01:00
if additional_fields
error_message = error_message . merge ( additional_fields )
end
2022-01-19 18:47:54 +01:00
2020-11-30 10:59:21 +01:00
return error_message . to_json
end
2022-01-19 22:15:43 +01:00
def error_json_helper (
env : HTTP :: Server :: Context ,
status_code : Int32 ,
message : String ,
additional_fields : Hash ( String , Object ) | Nil = nil
)
2020-11-30 10:59:21 +01:00
env . response . content_type = " application/json "
env . response . status_code = status_code
2022-01-19 18:47:54 +01:00
2020-11-30 10:59:21 +01:00
error_message = { " error " = > message }
2022-01-19 18:47:54 +01:00
2020-11-30 10:59:21 +01:00
if additional_fields
error_message = error_message . merge ( additional_fields )
end
2022-01-19 18:47:54 +01:00
2020-11-30 10:59:21 +01:00
return error_message . to_json
end
2022-01-19 18:47:54 +01:00
# -------------------
# Redirect
# -------------------
def error_redirect_helper ( env : HTTP :: Server :: Context )
2021-05-16 07:01:46 +02:00
request_path = env . request . path
2022-01-19 18:47:54 +01:00
locale = env . get ( " preferences " ) . as ( Preferences ) . locale
2021-05-16 07:36:10 +02:00
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
2021-05-16 07:01:46 +02:00
else
return " "
end
2021-05-16 07:36:10 +02:00
end