66class InfoException < Exception
77end
88
9+ # -------------------
10+ # Issue template
11+ # -------------------
12+
913macro error_template (* args )
10- error_template_helper(env, locale, {{* args}})
14+ error_template_helper(env, {{* args}})
1115end
1216
1317def github_details (summary : String , content : String )
@@ -22,11 +26,13 @@ def github_details(summary : String, content : String)
2226 return HTML .escape(details)
2327end
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,101 @@ def error_template_helper(env : HTTP::Server::Context, locale : String?, status_
7783 return templated " error"
7884end
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"
8696end
8797
98+ # -------------------
99+ # Atom feeds
100+ # -------------------
101+
88102macro error_atom (* args )
89- error_atom_helper(env, locale, {{* args}})
103+ error_atom_helper(env, {{* args}})
90104end
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>"
99115end
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>"
105122end
106123
124+ # -------------------
125+ # JSON
126+ # -------------------
127+
107128macro error_json (* args )
108- error_json_helper(env, locale, {{* args}})
129+ error_json_helper(env, {{* args}})
109130end
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 (
133+ env : HTTP ::Server ::Context ,
134+ status_code : Int32 ,
135+ exception : Exception ,
136+ additional_fields : Hash (String , Object ) | Nil = nil
137+ )
112138 if exception.is_a?(InfoException )
113- return error_json_helper(env, locale, status_code, exception.message || " " , additional_fields)
139+ return error_json_helper(env, status_code, exception.message || " " , additional_fields)
114140 end
141+
115142 env.response.content_type = " application/json"
116143 env.response.status_code = status_code
144+
117145 error_message = {" error" => exception.message, " errorBacktrace" => exception.inspect_with_backtrace}
146+
118147 if additional_fields
119148 error_message = error_message.merge(additional_fields)
120149 end
121- return error_message.to_json
122- end
123150
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 )
151+ return error_message.to_json
126152end
127153
128- def error_json_helper (env : HTTP ::Server ::Context , locale : String ?, status_code : Int32 , message : String , additional_fields : Hash (String , Object ) | Nil )
154+ def error_json_helper (
155+ env : HTTP ::Server ::Context ,
156+ status_code : Int32 ,
157+ message : String ,
158+ additional_fields : Hash (String , Object ) | Nil = nil
159+ )
129160 env.response.content_type = " application/json"
130161 env.response.status_code = status_code
162+
131163 error_message = {" error" => message}
164+
132165 if additional_fields
133166 error_message = error_message.merge(additional_fields)
134167 end
168+
135169 return error_message.to_json
136170end
137171
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 )
140- end
172+ # -------------------
173+ # Redirect
174+ # -------------------
141175
142- def error_redirect_helper (env : HTTP ::Server ::Context , locale : String ? )
176+ def error_redirect_helper (env : HTTP ::Server ::Context )
143177 request_path = env.request.path
144178
179+ locale = env.get(" preferences" ).as(Preferences ).locale
180+
145181 if request_path.starts_with?(" /search" ) || request_path.starts_with?(" /watch" ) ||
146182 request_path.starts_with?(" /channel" ) || request_path.starts_with?(" /playlist?list=PL" )
147183 next_steps_text = translate(locale, " next_steps_error_message" )
0 commit comments