Skip to content

Commit c7b74aa

Browse files
committed
Remove useless 'locale' argument from error template functions
1 parent 5e3c9cf commit c7b74aa

File tree

1 file changed

+52
-18
lines changed

1 file changed

+52
-18
lines changed

src/invidious/helpers/errors.cr

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
class InfoException < Exception
77
end
88

9+
# -------------------
10+
# Issue template
11+
# -------------------
12+
913
macro error_template(*args)
10-
error_template_helper(env, locale, {{*args}})
14+
error_template_helper(env, {{*args}})
1115
end
1216

1317
def github_details(summary : String, content : String)
@@ -22,11 +26,13 @@ def github_details(summary : String, content : String)
2226
return HTML.escape(details)
2327
end
2428

25-
def error_template_helper(env : HTTP::Server::Context, locale : String?, status_code : Int32, exception : Exception)
29+
def error_template_helper(env : HTTP::Server::Context, status_code : Int32, exception : Exception)
2630
if exception.is_a?(InfoException)
27-
return error_template_helper(env, locale, status_code, exception.message || "")
31+
return error_template_helper(env, status_code, exception.message || "")
2832
end
2933

34+
locale = env.get("preferences").as(Preferences).locale
35+
3036
env.response.content_type = "text/html"
3137
env.response.status_code = status_code
3238

@@ -77,71 +83,99 @@ def error_template_helper(env : HTTP::Server::Context, locale : String?, status_
7783
return templated "error"
7884
end
7985

80-
def error_template_helper(env : HTTP::Server::Context, locale : String?, status_code : Int32, message : String)
86+
def error_template_helper(env : HTTP::Server::Context, status_code : Int32, message : String)
8187
env.response.content_type = "text/html"
8288
env.response.status_code = status_code
89+
90+
locale = env.get("preferences").as(Preferences).locale
91+
8392
error_message = translate(locale, message)
84-
next_steps = error_redirect_helper(env, locale)
93+
next_steps = error_redirect_helper(env)
94+
8595
return templated "error"
8696
end
8797

98+
# -------------------
99+
# Atom feeds
100+
# -------------------
101+
88102
macro error_atom(*args)
89-
error_atom_helper(env, locale, {{*args}})
103+
error_atom_helper(env, {{*args}})
90104
end
91105

92-
def error_atom_helper(env : HTTP::Server::Context, locale : String?, status_code : Int32, exception : Exception)
106+
def error_atom_helper(env : HTTP::Server::Context, status_code : Int32, exception : Exception)
93107
if exception.is_a?(InfoException)
94-
return error_atom_helper(env, locale, status_code, exception.message || "")
108+
return error_atom_helper(env, status_code, exception.message || "")
95109
end
110+
96111
env.response.content_type = "application/atom+xml"
97112
env.response.status_code = status_code
113+
98114
return "<error>#{exception.inspect_with_backtrace}</error>"
99115
end
100116

101-
def error_atom_helper(env : HTTP::Server::Context, locale : String?, status_code : Int32, message : String)
117+
def error_atom_helper(env : HTTP::Server::Context, status_code : Int32, message : String)
102118
env.response.content_type = "application/atom+xml"
103119
env.response.status_code = status_code
120+
104121
return "<error>#{message}</error>"
105122
end
106123

124+
# -------------------
125+
# JSON
126+
# -------------------
127+
107128
macro error_json(*args)
108-
error_json_helper(env, locale, {{*args}})
129+
error_json_helper(env, {{*args}})
109130
end
110131

111-
def error_json_helper(env : HTTP::Server::Context, locale : String?, status_code : Int32, exception : Exception, additional_fields : Hash(String, Object) | Nil)
132+
def error_json_helper(env : HTTP::Server::Context, status_code : Int32, exception : Exception, additional_fields : Hash(String, Object) | Nil)
112133
if exception.is_a?(InfoException)
113-
return error_json_helper(env, locale, status_code, exception.message || "", additional_fields)
134+
return error_json_helper(env, status_code, exception.message || "", additional_fields)
114135
end
136+
115137
env.response.content_type = "application/json"
116138
env.response.status_code = status_code
139+
117140
error_message = {"error" => exception.message, "errorBacktrace" => exception.inspect_with_backtrace}
141+
118142
if additional_fields
119143
error_message = error_message.merge(additional_fields)
120144
end
145+
121146
return error_message.to_json
122147
end
123148

124-
def error_json_helper(env : HTTP::Server::Context, locale : String?, status_code : Int32, exception : Exception)
125-
return error_json_helper(env, locale, status_code, exception, nil)
149+
def error_json_helper(env : HTTP::Server::Context, status_code : Int32, exception : Exception)
150+
return error_json_helper(env, status_code, exception, nil)
126151
end
127152

128-
def error_json_helper(env : HTTP::Server::Context, locale : String?, status_code : Int32, message : String, additional_fields : Hash(String, Object) | Nil)
153+
def error_json_helper(env : HTTP::Server::Context, status_code : Int32, message : String, additional_fields : Hash(String, Object) | Nil)
129154
env.response.content_type = "application/json"
130155
env.response.status_code = status_code
156+
131157
error_message = {"error" => message}
158+
132159
if additional_fields
133160
error_message = error_message.merge(additional_fields)
134161
end
162+
135163
return error_message.to_json
136164
end
137165

138-
def error_json_helper(env : HTTP::Server::Context, locale : String?, status_code : Int32, message : String)
139-
error_json_helper(env, locale, status_code, message, nil)
166+
def error_json_helper(env : HTTP::Server::Context, status_code : Int32, message : String)
167+
error_json_helper(env, status_code, message, nil)
140168
end
141169

142-
def error_redirect_helper(env : HTTP::Server::Context, locale : String?)
170+
# -------------------
171+
# Redirect
172+
# -------------------
173+
174+
def error_redirect_helper(env : HTTP::Server::Context)
143175
request_path = env.request.path
144176

177+
locale = env.get("preferences").as(Preferences).locale
178+
145179
if request_path.starts_with?("/search") || request_path.starts_with?("/watch") ||
146180
request_path.starts_with?("/channel") || request_path.starts_with?("/playlist?list=PL")
147181
next_steps_text = translate(locale, "next_steps_error_message")

0 commit comments

Comments
 (0)