summaryrefslogtreecommitdiffstats
path: root/src/invidious/helpers/errors.cr
blob: 900cb0c632ced2075fa9756266f718388fbe9d64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# -------------------
#  Issue template
# -------------------

macro error_template(*args)
  error_template_helper(env, {{args.splat}})
end

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

def error_template_helper(env : HTTP::Server::Context, status_code : Int32, exception : Exception)
  if exception.is_a?(InfoException)
    return error_template_helper(env, status_code, exception.message || "")
  end

  locale = env.get("preferences").as(Preferences).locale

  env.response.content_type = "text/html"
  env.response.status_code = status_code

  issue_title = "#{exception.message} (#{exception.class})"

  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

  issue_template += github_details("Backtrace", exception.inspect_with_backtrace)

  # URLs for the error message below
  url_faq = "https://github.com/iv-org/documentation/blob/master/docs/faq.md"
  url_search_issues = "https://github.com/iv-org/invidious/issues"
  url_search_issues += "?q=is:issue+is:open+"
  url_search_issues += URI.encode_www_form("[Bug] #{issue_title}")

  url_switch = "https://redirect.invidious.io" + env.request.resource

  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)

  error_message = <<-END_HTML
    <div class="error_message">
      <h2>#{translate(locale, "crash_page_you_found_a_bug")}</h2>
      <br/><br/>

      <p><b>#{translate(locale, "crash_page_before_reporting")}</b></p>
      <ul>
        <li>#{translate(locale, "crash_page_refresh", env.request.resource)}</li>
        <li>#{translate(locale, "crash_page_switch_instance", url_switch)}</li>
        <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>
  END_HTML

  # Don't show the usual "next steps" widget. The same options are
  # proposed above the error message, just worded differently.
  next_steps = ""

  return templated "error"
end

def error_template_helper(env : HTTP::Server::Context, status_code : Int32, message : String)
  env.response.content_type = "text/html"
  env.response.status_code = status_code

  locale = env.get("preferences").as(Preferences).locale

  error_message = translate(locale, message)
  next_steps = error_redirect_helper(env)

  return templated "error"
end

# -------------------
#  Atom feeds
# -------------------

macro error_atom(*args)
  error_atom_helper(env, {{args.splat}})
end

def error_atom_helper(env : HTTP::Server::Context, status_code : Int32, exception : Exception)
  if exception.is_a?(InfoException)
    return error_atom_helper(env, status_code, exception.message || "")
  end

  env.response.content_type = "application/atom+xml"
  env.response.status_code = status_code

  return "<error>#{exception.inspect_with_backtrace}</error>"
end

def error_atom_helper(env : HTTP::Server::Context, status_code : Int32, message : String)
  env.response.content_type = "application/atom+xml"
  env.response.status_code = status_code

  return "<error>#{message}</error>"
end

# -------------------
#  JSON
# -------------------

macro error_json(*args)
  error_json_helper(env, {{args.splat}})
end

def error_json_helper(
  env : HTTP::Server::Context,
  status_code : Int32,
  exception : Exception,
  additional_fields : Hash(String, Object) | Nil = nil,
)
  if exception.is_a?(InfoException)
    return error_json_helper(env, status_code, exception.message || "", additional_fields)
  end

  env.response.content_type = "application/json"
  env.response.status_code = status_code

  error_message = {"error" => exception.message, "errorBacktrace" => exception.inspect_with_backtrace}

  if additional_fields
    error_message = error_message.merge(additional_fields)
  end

  return error_message.to_json
end

def error_json_helper(
  env : HTTP::Server::Context,
  status_code : Int32,
  message : String,
  additional_fields : Hash(String, Object) | Nil = nil,
)
  env.response.content_type = "application/json"
  env.response.status_code = status_code

  error_message = {"error" => message}

  if additional_fields
    error_message = error_message.merge(additional_fields)
  end

  return error_message.to_json
end

# -------------------
#  Redirect
# -------------------

def error_redirect_helper(env : HTTP::Server::Context)
  request_path = env.request.path

  locale = env.get("preferences").as(Preferences).locale

  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 rel="noreferrer noopener" href="https://youtube.com#{env.request.resource}">#{go_to_youtube}</a>
        </li>
      </ul>
    END_HTML
  else
    return ""
  end
end