Invidious is an alternative front-end to YouTube
Add backtraces to errors (#1498)
saltycrys3dac33ffbaec
diff --git a/src/invidious.cr b/src/invidious.crindex a9a3a963..9c3dffb2 100644--- a/src/invidious.cr+++ b/src/invidious.cr@@ -367,9 +367,7 @@ get "/search" do |env|beginsearch_query, count, videos = process_search_query(query, page, user, region: nil)rescue ex- error_message = ex.message- env.response.status_code = 500- next templated "error"+ next error_template(500, ex)endenv.set "search", query@@ -387,9 +385,7 @@ get "/login" do |env|endif !config.login_enabled- error_message = "Login has been disabled by administrator."- env.response.status_code = 400- next templated "error"+ next error_template(400, "Login has been disabled by administrator.")endreferer = get_referer(env, "/feed/subscriptions")@@ -416,9 +412,7 @@ post "/login" do |env|referer = get_referer(env, "/feed/subscriptions")if !config.login_enabled- error_message = "Login has been disabled by administrator."- env.response.status_code = 403- next templated "error"+ next error_template(403, "Login has been disabled by administrator.")end# https://stackoverflow.com/a/574698@@ -499,9 +493,7 @@ post "/login" do |env|headers["Cookie"] = URI.decode_www_form(headers["Cookie"])if challenge_results[0][3]?.try &.== 7- error_message = translate(locale, "Account has temporarily been disabled")- env.response.status_code = 423- next templated "error"+ next error_template(423, "Account has temporarily been disabled")endif token = challenge_results[0][-1]?.try &.[-1]?.try &.as_h?.try &.["5001"]?.try &.[-1].as_a?.try &.[-1].as_s@@ -515,9 +507,7 @@ post "/login" do |env|endif challenge_results[0][-1]?.try &.[5] == "INCORRECT_ANSWER_ENTERED"- error_message = translate(locale, "Incorrect password")- env.response.status_code = 401- next templated "error"+ next error_template(401, "Incorrect password")endprompt_type = challenge_results[0][-1]?.try &.[0].as_a?.try &.[0][2]?@@ -550,9 +540,7 @@ post "/login" do |env|endif tfa[5] == "QUOTA_EXCEEDED"- error_message = translate(locale, "Quota exceeded, try again in a few hours")- env.response.status_code = 423- next templated "error"+ next error_template(423, "Quota exceeded, try again in a few hours")endif !tfa_code@@ -608,9 +596,7 @@ post "/login" do |env|},}.to_jsonelse- error_message = translate(locale, "Unable to log in, make sure two-factor authentication (Authenticator or SMS) is turned on.")- env.response.status_code = 500- next templated "error"+ next error_template(500, "Unable to log in, make sure two-factor authentication (Authenticator or SMS) is turned on.")endtraceback << "Submitting challenge..."@@ -621,9 +607,7 @@ post "/login" do |env|if (challenge_results[0][-1]?.try &.[5] == "INCORRECT_ANSWER_ENTERED") ||(challenge_results[0][-1]?.try &.[5] == "INVALID_INPUT")- error_message = translate(locale, "Invalid TFA code")- env.response.status_code = 401- next templated "error"+ next error_template(401, "Invalid TFA code")endtraceback << "done.<br/>"@@ -702,29 +686,22 @@ post "/login" do |env|traceback.rewind# error_message = translate(locale, "Login failed. This may be because two-factor authentication is not turned on for your account.")error_message = %(#{ex.message}<br/>Traceback:<br/><div style="padding-left:2em" id="traceback">#{traceback.gets_to_end}</div>)- env.response.status_code = 500- next templated "error"+ next error_template(500, error_message)endwhen "invidious"if !email- error_message = translate(locale, "User ID is a required field")- env.response.status_code = 401- next templated "error"+ next error_template(401, "User ID is a required field")endif !password- error_message = translate(locale, "Password is a required field")- env.response.status_code = 401- next templated "error"+ next error_template(401, "Password is a required field")enduser = PG_DB.query_one?("SELECT * FROM users WHERE email = $1", email, as: User)if userif !user.password- error_message = translate(locale, "Please sign in using 'Log in with Google'")- env.response.status_code = 400- next templated "error"+ next error_template(400, "Please sign in using 'Log in with Google'")endif Crypto::Bcrypt::Password.new(user.password.not_nil!).verify(password.byte_slice(0, 55))@@ -745,9 +722,7 @@ post "/login" do |env|secure: secure, http_only: true)endelse- error_message = translate(locale, "Wrong username or password")- env.response.status_code = 401- next templated "error"+ next error_template(401, "Wrong username or password")end# Since this user has already registered, we don't want to overwrite their preferences@@ -758,22 +733,16 @@ post "/login" do |env|endelseif !config.registration_enabled- error_message = "Registration has been disabled by administrator."- env.response.status_code = 400- next templated "error"+ next error_template(400, "Registration has been disabled by administrator.")endif password.empty?- error_message = translate(locale, "Password cannot be empty")- env.response.status_code = 401- next templated "error"+ next error_template(401, "Password cannot be empty")end# See https://security.stackexchange.com/a/39851if password.bytesize > 55- error_message = translate(locale, "Password should not be longer than 55 characters")- env.response.status_code = 400- next templated "error"+ next error_template(400, "Password cannot be longer than 55 characters")endpassword = password.byte_slice(0, 55)@@ -815,28 +784,28 @@ post "/login" do |env|beginvalidate_request(tokens[0], answer, env.request, HMAC_KEY, PG_DB, locale)rescue ex- error_message = ex.message- env.response.status_code = 400- next templated "error"+ next error_template(400, ex)endelse # "text"answer = Digest::MD5.hexdigest(answer.downcase.strip)- found_valid_captcha = false+ if tokens.empty?+ next error_template(500, "Erroneous CAPTCHA")+ end- error_message = translate(locale, "Erroneous CAPTCHA")+ found_valid_captcha = false+ error_exception = Exception.newtokens.each_with_index do |token, i|beginvalidate_request(token, answer, env.request, HMAC_KEY, PG_DB, locale)found_valid_captcha = truerescue ex- error_message = ex.message+ error_exception = exendendif !found_valid_captcha- env.response.status_code = 500- next templated "error"+ next error_template(500, error_exception)endendend@@ -902,9 +871,7 @@ post "/signout" do |env|beginvalidate_request(token, sid, env.request, HMAC_KEY, PG_DB, locale)rescue ex- error_message = ex.message- env.response.status_code = 400- next templated "error"+ next error_template(400, ex)endPG_DB.exec("DELETE FROM session_ids * WHERE id = $1", sid)@@ -1182,9 +1149,7 @@ post "/watch_ajax" do |env|if redirectnext env.redirect refererelse- error_message = {"error" => "No such user"}.to_json- env.response.status_code = 403- next error_message+ next error_json(403, "No such user")endend@@ -1201,13 +1166,10 @@ post "/watch_ajax" do |env|beginvalidate_request(token, sid, env.request, HMAC_KEY, PG_DB, locale)rescue ex- env.response.status_code = 400if redirect- error_message = ex.message- next templated "error"+ next error_template(400, ex)else- error_message = {"error" => ex.message}.to_json- next error_message+ next error_json(400, ex)endend@@ -1227,9 +1189,7 @@ post "/watch_ajax" do |env|when "action_mark_unwatched"PG_DB.exec("UPDATE users SET watched = array_remove(watched, $1) WHERE email = $2", id, user.email)else- error_message = {"error" => "Unsupported action #{action}"}.to_json- env.response.status_code = 400- next error_message+ next error_json(400, "Unsupported action #{action}")endif redirect@@ -1259,9 +1219,7 @@ get "/modify_notifications" do |env|if redirectnext env.redirect refererelse- error_message = {"error" => "No such user"}.to_json- env.response.status_code = 403- next error_message+ next error_json(403, "No such user")endend@@ -1334,9 +1292,7 @@ post "/subscription_ajax" do |env|if redirectnext env.redirect refererelse- error_message = {"error" => "No such user"}.to_json- env.response.status_code = 403- next error_message+ next error_json(403, "No such user")endend@@ -1348,13 +1304,9 @@ post "/subscription_ajax" do |env|validate_request(token, sid, env.request, HMAC_KEY, PG_DB, locale)rescue exif redirect- error_message = ex.message- env.response.status_code = 400- next templated "error"+ next error_template(400, ex)else- error_message = {"error" => ex.message}.to_json- env.response.status_code = 400- next error_message+ next error_json(400, ex)endend@@ -1384,9 +1336,7 @@ post "/subscription_ajax" do |env|when "action_remove_subscriptions"PG_DB.exec("UPDATE users SET feed_needs_update = true, subscriptions = array_remove(subscriptions, $1) WHERE email = $2", channel_id, email)else- error_message = {"error" => "Unsupported action #{action}"}.to_json- env.response.status_code = 400- next error_message+ next error_json(400, "Unsupported action #{action}")endif redirect@@ -1569,7 +1519,7 @@ post "/data_control" do |env|PG_DB.exec("UPDATE playlists SET description = $1 WHERE id = $2", description, playlist.id)videos = item["videos"]?.try &.as_a?.try &.each_with_index do |video_id, idx|- raise "Playlist cannot have more than 500 videos" if idx > 500+ raise InfoException.new("Playlist cannot have more than 500 videos") if idx > 500video_id = video_id.try &.as_s?next if !video_id@@ -1706,51 +1656,37 @@ post "/change_password" do |env|# We don't store passwords for Google accountsif !user.password- error_message = "Cannot change password for Google accounts"- env.response.status_code = 400- next templated "error"+ next error_template(400, "Cannot change password for Google accounts")endbeginvalidate_request(token, sid, env.request, HMAC_KEY, PG_DB, locale)rescue ex- error_message = ex.message- env.response.status_code = 400- next templated "error"+ next error_template(400, ex)endpassword = env.params.body["password"]?if !password- error_message = translate(locale, "Password is a required field")- env.response.status_code = 401- next templated "error"+ next error_template(401, "Password is a required field")endnew_passwords = env.params.body.select { |k, v| k.match(/^new_password\[\d+\]$/) }.map { |k, v| v }if new_passwords.size <= 1 || new_passwords.uniq.size != 1- error_message = translate(locale, "New passwords must match")- env.response.status_code = 400- next templated "error"+ next error_template(400, "New passwords must match")endnew_password = new_passwords.uniq[0]if new_password.empty?- error_message = translate(locale, "Password cannot be empty")- env.response.status_code = 401- next templated "error"+ next error_template(401, "Password cannot be empty")endif new_password.bytesize > 55- error_message = translate(locale, "Password should not be longer than 55 characters")- env.response.status_code = 400- next templated "error"+ next error_template(400, "Password cannot be longer than 55 characters")endif !Crypto::Bcrypt::Password.new(user.password.not_nil!).verify(password.byte_slice(0, 55))- error_message = translate(locale, "Incorrect password")- env.response.status_code = 401- next templated "error"+ next error_template(401, "Incorrect password")endnew_password = Crypto::Bcrypt::Password.create(new_password, cost: 10)@@ -1795,9 +1731,7 @@ post "/delete_account" do |env|beginvalidate_request(token, sid, env.request, HMAC_KEY, PG_DB, locale)rescue ex- error_message = ex.message- env.response.status_code = 400- next templated "error"+ next error_template(400, ex)endview_name = "subscriptions_#{sha256(user.email)}"@@ -1849,9 +1783,7 @@ post "/clear_watch_history" do |env|beginvalidate_request(token, sid, env.request, HMAC_KEY, PG_DB, locale)rescue ex- error_message = ex.message- env.response.status_code = 400- next templated "error"+ next error_template(400, ex)endPG_DB.exec("UPDATE users SET watched = '{}' WHERE email = $1", user.email)@@ -1904,9 +1836,7 @@ post "/authorize_token" do |env|beginvalidate_request(token, sid, env.request, HMAC_KEY, PG_DB, locale)rescue ex- error_message = ex.message- env.response.status_code = 400- next templated "error"+ next error_template(400, ex)endscopes = env.params.body.select { |k, v| k.match(/^scopes\[\d+\]$/) }.map { |k, v| v }@@ -1969,9 +1899,7 @@ post "/token_ajax" do |env|if redirectnext env.redirect refererelse- error_message = {"error" => "No such user"}.to_json- env.response.status_code = 403- next error_message+ next error_json(403, "No such user")endend@@ -1983,13 +1911,9 @@ post "/token_ajax" do |env|validate_request(token, sid, env.request, HMAC_KEY, PG_DB, locale)rescue exif redirect- error_message = ex.message- env.response.status_code = 400- next templated "error"+ next error_template(400, ex)else- error_message = {"error" => ex.message}.to_json- env.response.status_code = 400- next error_message+ next error_json(400, ex)endend@@ -2006,9 +1930,7 @@ post "/token_ajax" do |env|when .starts_with? "action_revoke_token"PG_DB.exec("DELETE FROM session_ids * WHERE id = $1 AND email = $2", session, user.email)else- error_message = {"error" => "Unsupported action #{action}"}.to_json- env.response.status_code = 400- next error_message+ next error_json(400, "Unsupported action #{action}")endif redirect@@ -2048,9 +1970,7 @@ get "/feed/trending" do |env|begintrending, plid = fetch_trending(trending_type, region, locale)rescue ex- error_message = "#{ex.message}"- env.response.status_code = 500- next templated "error"+ next error_template(500, ex)endtemplated "trending"@@ -2145,9 +2065,7 @@ get "/feed/channel/:ucid" do |env|rescue ex : ChannelRedirectnext env.redirect env.request.resource.gsub(ucid, ex.channel_id)rescue ex- error_message = ex.message- env.response.status_code = 500- next error_message+ next error_atom(500, ex)endresponse = YT_POOL.client &.get("/feeds/videos.xml?channel_id=#{channel.ucid}")@@ -2558,9 +2476,7 @@ get "/channel/:ucid" do |env|rescue ex : ChannelRedirectnext env.redirect env.request.resource.gsub(ucid, ex.channel_id)rescue ex- error_message = ex.message- env.response.status_code = 500- next templated "error"+ next error_template(500, ex)endif channel.auto_generated@@ -2627,9 +2543,7 @@ get "/channel/:ucid/playlists" do |env|rescue ex : ChannelRedirectnext env.redirect env.request.resource.gsub(ucid, ex.channel_id)rescue ex- error_message = ex.message- env.response.status_code = 500- next templated "error"+ next error_template(500, ex)endif channel.auto_generated@@ -2667,9 +2581,7 @@ get "/channel/:ucid/community" do |env|rescue ex : ChannelRedirectnext env.redirect env.request.resource.gsub(ucid, ex.channel_id)rescue ex- error_message = ex.message- env.response.status_code = 500- next templated "error"+ next error_template(500, ex)endif !channel.tabs.includes? "community"@@ -2678,9 +2590,11 @@ get "/channel/:ucid/community" do |env|beginitems = JSON.parse(fetch_channel_community(ucid, continuation, locale, "json", thin_mode))- rescue ex+ rescue ex : InfoExceptionenv.response.status_code = 500error_message = ex.message+ rescue ex+ next error_template(500, ex)endenv.set "search", "channel:#{channel.ucid} "@@ -2690,12 +2604,11 @@ end# API Endpointsget "/api/v1/stats" do |env|+ locale = LOCALES[env.get("preferences").as(Preferences).locale]?env.response.content_type = "application/json"if !config.statistics_enabled- error_message = {"error" => "Statistics are not enabled."}.to_json- env.response.status_code = 400- next error_message+ next error_json(400, "Statistics are not enabled.")endInvidious::Jobs::StatisticsRefreshJob::STATISTICS.to_json@@ -2715,10 +2628,8 @@ get "/api/v1/storyboards/:id" do |env|beginvideo = get_video(id, PG_DB, region: region)rescue ex : VideoRedirect- error_message = {"error" => "Video is unavailable", "videoId" => ex.video_id}.to_json- env.response.status_code = 302env.response.headers["Location"] = env.request.resource.gsub(id, ex.video_id)- next error_message+ next error_json(302, "Video is unavailable", {"videoId" => ex.video_id})rescue exenv.response.status_code = 500next@@ -2803,10 +2714,8 @@ get "/api/v1/captions/:id" do |env|beginvideo = get_video(id, PG_DB, region: region)rescue ex : VideoRedirect- error_message = {"error" => "Video is unavailable", "videoId" => ex.video_id}.to_json- env.response.status_code = 302env.response.headers["Location"] = env.request.resource.gsub(id, ex.video_id)- next error_message+ next error_json(302, "Video is unavailable", {"videoId" => ex.video_id})rescue exenv.response.status_code = 500next@@ -2938,9 +2847,7 @@ get "/api/v1/comments/:id" do |env|begincomments = fetch_youtube_comments(id, PG_DB, continuation, format, locale, thin_mode, region, sort_by: sort_by)rescue ex- error_message = {"error" => ex.message}.to_json- env.response.status_code = 500- next error_message+ next error_json(500, ex)endnext comments@@ -2983,13 +2890,7 @@ endget "/api/v1/insights/:id" do |env|locale = LOCALES[env.get("preferences").as(Preferences).locale]?-- id = env.params.url["id"]- env.response.content_type = "application/json"-- error_message = {"error" => "YouTube has removed publicly available analytics."}.to_json- env.response.status_code = 410- error_message+ next error_json(410, "YouTube has removed publicly available analytics.")endget "/api/v1/annotations/:id" do |env|@@ -3078,14 +2979,10 @@ get "/api/v1/videos/:id" do |env|beginvideo = get_video(id, PG_DB, region: region)rescue ex : VideoRedirect- error_message = {"error" => "Video is unavailable", "videoId" => ex.video_id}.to_json- env.response.status_code = 302env.response.headers["Location"] = env.request.resource.gsub(id, ex.video_id)- next error_message+ next error_json(302, "Video is unavailable", {"videoId" => ex.video_id})rescue ex- error_message = {"error" => ex.message}.to_json- env.response.status_code = 500- next error_message+ next error_json(500, ex)endvideo.to_json(locale)@@ -3102,9 +2999,7 @@ get "/api/v1/trending" do |env|begintrending, plid = fetch_trending(trending_type, region, locale)rescue ex- error_message = {"error" => ex.message}.to_json- env.response.status_code = 500- next error_message+ next error_json(500, ex)endvideos = JSON.build do |json|@@ -3151,14 +3046,10 @@ get "/api/v1/channels/:ucid" do |env|beginchannel = get_about_info(ucid, locale)rescue ex : ChannelRedirect- error_message = {"error" => "Channel is unavailable", "authorId" => ex.channel_id}.to_json- env.response.status_code = 302env.response.headers["Location"] = env.request.resource.gsub(ucid, ex.channel_id)- next error_message+ next error_json(302, "Channel is unavailable", {"authorId" => ex.channel_id})rescue ex- error_message = {"error" => ex.message}.to_json- env.response.status_code = 500- next error_message+ next error_json(500, ex)endpage = 1@@ -3169,9 +3060,7 @@ get "/api/v1/channels/:ucid" do |env|begincount, videos = get_60_videos(channel.ucid, channel.author, page, channel.auto_generated, sort_by)rescue ex- error_message = {"error" => ex.message}.to_json- env.response.status_code = 500- next error_message+ next error_json(500, ex)endend@@ -3286,22 +3175,16 @@ endbeginchannel = get_about_info(ucid, locale)rescue ex : ChannelRedirect- error_message = {"error" => "Channel is unavailable", "authorId" => ex.channel_id}.to_json- env.response.status_code = 302env.response.headers["Location"] = env.request.resource.gsub(ucid, ex.channel_id)- next error_message+ next error_json(302, "Channel is unavailable", {"authorId" => ex.channel_id})rescue ex- error_message = {"error" => ex.message}.to_json- env.response.status_code = 500- next error_message+ next error_json(500, ex)endbegincount, videos = get_60_videos(channel.ucid, channel.author, page, channel.auto_generated, sort_by)rescue ex- error_message = {"error" => ex.message}.to_json- env.response.status_code = 500- next error_message+ next error_json(500, ex)endJSON.build do |json|@@ -3325,9 +3208,7 @@ endbeginvideos = get_latest_videos(ucid)rescue ex- error_message = {"error" => ex.message}.to_json- env.response.status_code = 500- next error_message+ next error_json(500, ex)endJSON.build do |json|@@ -3355,14 +3236,10 @@ endbeginchannel = get_about_info(ucid, locale)rescue ex : ChannelRedirect- error_message = {"error" => "Channel is unavailable", "authorId" => ex.channel_id}.to_json- env.response.status_code = 302env.response.headers["Location"] = env.request.resource.gsub(ucid, ex.channel_id)- next error_message+ next error_json(302, "Channel is unavailable", {"authorId" => ex.channel_id})rescue ex- error_message = {"error" => ex.message}.to_json- env.response.status_code = 500- next error_message+ next error_json(500, ex)enditems, continuation = fetch_channel_playlists(channel.ucid, channel.author, channel.auto_generated, continuation, sort_by)@@ -3403,9 +3280,7 @@ endbeginfetch_channel_community(ucid, continuation, locale, format, thin_mode)rescue ex- env.response.status_code = 400- error_message = {"error" => ex.message}.to_json- next error_message+ next error_json(500, ex)endendend@@ -3463,9 +3338,7 @@ get "/api/v1/search" do |env|beginsearch_params = produce_search_params(sort_by, date, content_type, duration, features)rescue ex- env.response.status_code = 400- error_message = {"error" => ex.message}.to_json- next error_message+ next error_json(400, ex)endcount, search_results = search(query, page, search_params, region).as(Tuple)@@ -3508,9 +3381,7 @@ get "/api/v1/search/suggestions" do |env|endendrescue ex- env.response.status_code = 500- error_message = {"error" => ex.message}.to_json- next error_message+ next error_json(500, ex)endend@@ -3537,16 +3408,12 @@ endbeginplaylist = get_playlist(PG_DB, plid, locale)rescue ex- env.response.status_code = 404- error_message = {"error" => "Playlist does not exist."}.to_json- next error_message+ next error_json(404, "Playlist does not exist.")enduser = env.get?("user").try &.as(User)if !playlist || playlist.privacy.private? && playlist.author != user.try &.email- env.response.status_code = 404- error_message = {"error" => "Playlist does not exist."}.to_json- next error_message+ next error_json(404, "Playlist does not exist.")endresponse = playlist.to_json(offset, locale, continuation: continuation)@@ -3590,9 +3457,7 @@ get "/api/v1/mixes/:rdid" do |env|mix.videos = mix.videos[index..-1]rescue ex- error_message = {"error" => ex.message}.to_json- env.response.status_code = 500- next error_message+ next error_json(500, ex)endresponse = JSON.build do |json|@@ -3794,22 +3659,16 @@ post "/api/v1/auth/playlists" do |env|title = env.params.json["title"]?.try &.as(String).delete("<>").byte_slice(0, 150)if !title- error_message = {"error" => "Invalid title."}.to_json- env.response.status_code = 400- next error_message+ next error_json(400, "Invalid title.")endprivacy = env.params.json["privacy"]?.try { |privacy| PlaylistPrivacy.parse(privacy.as(String).downcase) }if !privacy- error_message = {"error" => "Invalid privacy setting."}.to_json- env.response.status_code = 400- next error_message+ next error_json(400, "Invalid privacy setting.")endif PG_DB.query_one("SELECT count(*) FROM playlists WHERE author = $1", user.email, as: Int64) >= 100- error_message = {"error" => "User cannot have more than 100 playlists."}.to_json- env.response.status_code = 400- next error_message+ next error_json(400, "User cannot have more than 100 playlists.")endplaylist = create_playlist(PG_DB, title, privacy, user)@@ -3831,15 +3690,11 @@ patch "/api/v1/auth/playlists/:plid" do |env|playlist = PG_DB.query_one?("SELECT * FROM playlists WHERE id = $1", plid, as: InvidiousPlaylist)if !playlist || playlist.author != user.email && playlist.privacy.private?- env.response.status_code = 404- error_message = {"error" => "Playlist does not exist."}.to_json- next error_message+ next error_json(404, "Playlist does not exist.")endif playlist.author != user.email- env.response.status_code = 403- error_message = {"error" => "Invalid user"}.to_json- next error_message+ next error_json(403, "Invalid user")endtitle = env.params.json["title"].try &.as(String).delete("<>").byte_slice(0, 150) || playlist.title@@ -3859,6 +3714,8 @@ patch "/api/v1/auth/playlists/:plid" do |env|enddelete "/api/v1/auth/playlists/:plid" do |env|+ locale = LOCALES[env.get("preferences").as(Preferences).locale]?+env.response.content_type = "application/json"user = env.get("user").as(User)@@ -3866,15 +3723,11 @@ delete "/api/v1/auth/playlists/:plid" do |env|playlist = PG_DB.query_one?("SELECT * FROM playlists WHERE id = $1", plid, as: InvidiousPlaylist)if !playlist || playlist.author != user.email && playlist.privacy.private?- env.response.status_code = 404- error_message = {"error" => "Playlist does not exist."}.to_json- next error_message+ next error_json(404, "Playlist does not exist.")endif playlist.author != user.email- env.response.status_code = 403- error_message = {"error" => "Invalid user"}.to_json- next error_message+ next error_json(403, "Invalid user")endPG_DB.exec("DELETE FROM playlist_videos * WHERE plid = $1", plid)@@ -3893,36 +3746,26 @@ post "/api/v1/auth/playlists/:plid/videos" do |env|playlist = PG_DB.query_one?("SELECT * FROM playlists WHERE id = $1", plid, as: InvidiousPlaylist)if !playlist || playlist.author != user.email && playlist.privacy.private?- env.response.status_code = 404- error_message = {"error" => "Playlist does not exist."}.to_json- next error_message+ next error_json(404, "Playlist does not exist.")endif playlist.author != user.email- env.response.status_code = 403- error_message = {"error" => "Invalid user"}.to_json- next error_message+ next error_json(403, "Invalid user")endif playlist.index.size >= 500- env.response.status_code = 400- error_message = {"error" => "Playlist cannot have more than 500 videos"}.to_json- next error_message+ next error_json(400, "Playlist cannot have more than 500 videos")endvideo_id = env.params.json["videoId"].try &.as(String)if !video_id- env.response.status_code = 403- error_message = {"error" => "Invalid videoId"}.to_json- next error_message+ next error_json(403, "Invalid videoId")endbeginvideo = get_video(video_id, PG_DB)rescue ex- error_message = {"error" => ex.message}.to_json- env.response.status_code = 500- next error_message+ next error_json(500, ex)endplaylist_video = PlaylistVideo.new({@@ -3949,6 +3792,8 @@ post "/api/v1/auth/playlists/:plid/videos" do |env|enddelete "/api/v1/auth/playlists/:plid/videos/:index" do |env|+ locale = LOCALES[env.get("preferences").as(Preferences).locale]?+env.response.content_type = "application/json"user = env.get("user").as(User)@@ -3957,21 +3802,15 @@ delete "/api/v1/auth/playlists/:plid/videos/:index" do |env|playlist = PG_DB.query_one?("SELECT * FROM playlists WHERE id = $1", plid, as: InvidiousPlaylist)if !playlist || playlist.author != user.email && playlist.privacy.private?- env.response.status_code = 404- error_message = {"error" => "Playlist does not exist."}.to_json- next error_message+ next error_json(404, "Playlist does not exist.")endif playlist.author != user.email- env.response.status_code = 403- error_message = {"error" => "Invalid user"}.to_json- next error_message+ next error_json(403, "Invalid user")endif !playlist.index.includes? index- env.response.status_code = 404- error_message = {"error" => "Playlist does not contain index"}.to_json- next error_message+ next error_json(404, "Playlist does not contain index")endPG_DB.exec("DELETE FROM playlist_videos * WHERE index = $1", index)@@ -4017,9 +3856,7 @@ post "/api/v1/auth/tokens/register" do |env|callback_url = env.params.json["callbackUrl"]?.try &.as(String)expire = env.params.json["expire"]?.try &.as(Int64)else- error_message = {"error" => "Invalid or missing header 'Content-Type'"}.to_json- env.response.status_code = 400- next error_message+ next error_json(400, "Invalid or missing header 'Content-Type'")endif callback_url && callback_url.empty?@@ -4069,6 +3906,7 @@ post "/api/v1/auth/tokens/register" do |env|endpost "/api/v1/auth/tokens/unregister" do |env|+ locale = LOCALES[env.get("preferences").as(Preferences).locale]?env.response.content_type = "application/json"user = env.get("user").as(User)scopes = env.get("scopes").as(Array(String))@@ -4082,9 +3920,7 @@ post "/api/v1/auth/tokens/unregister" do |env|elsif scopes_include_scope(scopes, "GET:tokens")PG_DB.exec("DELETE FROM session_ids * WHERE id = $1", session)else- error_message = {"error" => "Cannot revoke session #{session}"}.to_json- env.response.status_code = 400- next error_message+ next error_json(400, "Cannot revoke session #{session}")endenv.response.status_code = 204@@ -4408,6 +4244,7 @@ get "/videoplayback/*" do |env|endget "/videoplayback" do |env|+ locale = LOCALES[env.get("preferences").as(Preferences).locale]?query_params = env.params.queryfvip = query_params["fvip"]? || "3"@@ -4474,9 +4311,7 @@ get "/videoplayback" do |env|if url.includes? "&file=seg.ts"if CONFIG.disabled?("livestreams")- env.response.status_code = 403- error_message = "Administrator has disabled this endpoint."- next templated "error"+ next error_template(403, "Administrator has disabled this endpoint.")endbegin@@ -4508,9 +4343,7 @@ get "/videoplayback" do |env|elseif query_params["title"]? && CONFIG.disabled?("downloads") ||CONFIG.disabled?("dash")- env.response.status_code = 403- error_message = "Administrator has disabled this endpoint."- next templated "error"+ next error_template(403, "Administrator has disabled this endpoint.")endcontent_length = nil@@ -4851,14 +4684,9 @@ error 404 do |env|halt env, status_code: 302end-error 500 do |env|- error_message = <<-END_HTML- Looks like you've found a bug in Invidious. Feel free to open a new issue- <a href="https://github.com/iv-org/invidious/issues">here</a>- or send an email to- <a href="mailto:#{CONFIG.admin_email}">#{CONFIG.admin_email}</a>.- END_HTML- templated "error"+error 500 do |env, ex|+ locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ error_template(500, ex)endstatic_headers do |response, filepath, filestat|diff --git a/src/invidious/channels.cr b/src/invidious/channels.crindex 392c44ee..82921ac7 100644--- a/src/invidious/channels.cr+++ b/src/invidious/channels.cr@@ -208,7 +208,7 @@ def fetch_channel(ucid, db, pull_all_videos = true, locale = nil)author = rss.xpath_node(%q(//feed/title))if !author- raise translate(locale, "Deleted or invalid channel")+ raise InfoException.new("Deleted or invalid channel")endauthor = author.content@@ -226,13 +226,14 @@ def fetch_channel(ucid, db, pull_all_videos = true, locale = nil)videos = [] of SearchVideobegininitial_data = JSON.parse(response.body).as_a.find &.["response"]?- raise "Could not extract JSON" if !initial_data+ raise InfoException.new("Could not extract channel JSON") if !initial_datavideos = extract_videos(initial_data.as_h, author, ucid)rescue exif response.body.includes?("To continue with your YouTube experience, please fill out the form below.") ||response.body.includes?("https://www.google.com/sorry/index")- raise "Could not extract channel info. Instance is likely blocked."+ raise InfoException.new("Could not extract channel info. Instance is likely blocked.")end+ raise exendrss.xpath_nodes("//feed/entry").each do |entry|@@ -287,7 +288,7 @@ def fetch_channel(ucid, db, pull_all_videos = true, locale = nil)loop doresponse = get_channel_videos_response(ucid, page, auto_generated: auto_generated)initial_data = JSON.parse(response.body).as_a.find &.["response"]?- raise "Could not extract JSON" if !initial_data+ raise InfoException.new("Could not extract channel JSON") if !initial_datavideos = extract_videos(initial_data.as_h, author, ucid)count = videos.size@@ -507,8 +508,7 @@ def fetch_channel_community(ucid, continuation, locale, format, thin_mode)endif response.status_code != 200- error_message = translate(locale, "This channel does not exist.")- raise error_message+ raise InfoException.new("This channel does not exist.")enducid = response.body.match(/https:\/\/www.youtube.com\/channel\/(?<ucid>UC[a-zA-Z0-9_-]{22})/).not_nil!["ucid"]@@ -518,7 +518,7 @@ def fetch_channel_community(ucid, continuation, locale, format, thin_mode)body = initial_data["contents"]?.try &.["twoColumnBrowseResultsRenderer"]["tabs"].as_a.select { |tab| tab["tabRenderer"]?.try &.["selected"].as_bool.== true }[0]?if !body- raise "Could not extract community tab."+ raise InfoException.new("Could not extract community tab.")endbody = body["tabRenderer"]["content"]["sectionListRenderer"]["contents"][0]["itemSectionRenderer"]@@ -540,7 +540,7 @@ def fetch_channel_community(ucid, continuation, locale, format, thin_mode)body["response"]["continuationContents"]["backstageCommentsContinuation"]?if !body- raise "Could not extract continuation."+ raise InfoException.new("Could not extract continuation.")endend@@ -551,7 +551,7 @@ def fetch_channel_community(ucid, continuation, locale, format, thin_mode)error_message = (message["text"]["simpleText"]? ||message["text"]["runs"]?.try &.[0]?.try &.["text"]?).try &.as_s || ""- raise error_message+ raise InfoException.new(error_message)endresponse = JSON.build do |json|@@ -786,21 +786,19 @@ def get_about_info(ucid, locale)endif result.status_code != 200- error_message = translate(locale, "This channel does not exist.")- raise error_message+ raise InfoException.new("This channel does not exist.")endabout = XML.parse_html(result.body)if about.xpath_node(%q(//div[contains(@class, "channel-empty-message")]))- error_message = translate(locale, "This channel does not exist.")- raise error_message+ raise InfoException.new("This channel does not exist.")endinitdata = extract_initial_data(result.body)if initdata.empty?error_message = about.xpath_node(%q(//div[@class="yt-alert-content"])).try &.content.striperror_message ||= translate(locale, "Could not get channel info.")- raise error_message+ raise InfoException.new(error_message)endauthor = about.xpath_node(%q(//meta[@name="title"])).not_nil!["content"]diff --git a/src/invidious/comments.cr b/src/invidious/comments.crindex 407cef78..ec203c41 100644--- a/src/invidious/comments.cr+++ b/src/invidious/comments.cr@@ -92,7 +92,7 @@ def fetch_youtube_comments(id, db, cursor, format, locale, thin_mode, region, soresponse = JSON.parse(response.body)if !response["response"]["continuationContents"]?- raise translate(locale, "Could not fetch comments")+ raise InfoException.new("Could not fetch comments")endresponse = response["response"]["continuationContents"]@@ -266,7 +266,7 @@ def fetch_reddit_comments(id, sort_by = "confidence")thread = result[0].data.as(RedditListing).children[0].data.as(RedditLink)else- raise "Got error code #{search_results.status_code}"+ raise InfoException.new("Could not fetch comments")endcomments = result[1].data.as(RedditListing).childrendiff --git a/src/invidious/helpers/errors.cr b/src/invidious/helpers/errors.crnew file mode 100644index 00000000..927a398f--- /dev/null+++ b/src/invidious/helpers/errors.cr@@ -0,0 +1,90 @@+# InfoExceptions are for displaying information to the user.+#+# An InfoException might or might not indicate that something went wrong.+# Historically Invidious didn't differentiate between these two options, so to+# maintain previous functionality InfoExceptions do not print backtraces.+class InfoException < Exception+end++macro error_template(*args)+ error_template_helper(env, config, locale, {{*args}})+end++def error_template_helper(env : HTTP::Server::Context, config : Config, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, exception : Exception)+ if exception.is_a?(InfoException)+ return error_template_helper(env, config, locale, status_code, exception.message || "")+ end+ env.response.status_code = status_code+ error_message = <<-END_HTML+ Looks like you've found a bug in Invidious. Feel free to open a new issue+ <a href="https://github.com/iv-org/invidious/issues">here</a>+ or send an email to+ <a href="mailto:#{CONFIG.admin_email}">#{CONFIG.admin_email}</a>.+ <br>+ <br>+ <br>+ Please include the following text in your message:+ <pre style="padding: 20px; background: rgba(0, 0, 0, 0.12345);">#{exception.inspect_with_backtrace}</pre>+ END_HTML+ return templated "error"+end++def error_template_helper(env : HTTP::Server::Context, config : Config, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, message : String)+ env.response.status_code = status_code+ error_message = translate(locale, message)+ return templated "error"+end++macro error_atom(*args)+ error_atom_helper(env, config, locale, {{*args}})+end++def error_atom_helper(env : HTTP::Server::Context, config : Config, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, exception : Exception)+ if exception.is_a?(InfoException)+ return error_atom_helper(env, config, locale, 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, config : Config, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, message : String)+ env.response.content_type = "application/atom+xml"+ env.response.status_code = status_code+ return "<error>#{message}</error>"+end++macro error_json(*args)+ error_json_helper(env, config, locale, {{*args}})+end++def error_json_helper(env : HTTP::Server::Context, config : Config, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, exception : Exception, additional_fields : Hash(String, Object) | Nil)+ if exception.is_a?(InfoException)+ return error_json_helper(env, config, locale, 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, config : Config, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, exception : Exception)+ return error_json_helper(env, config, locale, status_code, exception, nil)+end++def error_json_helper(env : HTTP::Server::Context, config : Config, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, message : String, additional_fields : Hash(String, Object) | 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++def error_json_helper(env : HTTP::Server::Context, config : Config, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, message : String)+ error_json_helper(env, config, locale, status_code, message, nil)+enddiff --git a/src/invidious/helpers/tokens.cr b/src/invidious/helpers/tokens.crindex 39aae367..a09ce90b 100644--- a/src/invidious/helpers/tokens.cr+++ b/src/invidious/helpers/tokens.cr@@ -70,33 +70,33 @@ def validate_request(token, session, request, key, db, locale = nil)when JSON::Anytoken = token.as_hwhen Nil- raise translate(locale, "Hidden field \"token\" is a required field")+ raise InfoException.new("Hidden field \"token\" is a required field")endexpire = token["expire"]?.try &.as_iif expire.try &.< Time.utc.to_unix- raise translate(locale, "Token is expired, please try again")+ raise InfoException.new("Token is expired, please try again")endif token["session"] != session- raise translate(locale, "Erroneous token")+ raise InfoException.new("Erroneous token")endscopes = token["scopes"].as_a.map { |v| v.as_s }scope = "#{request.method}:#{request.path.lchop("/api/v1/auth/").lstrip("/")}"if !scopes_include_scope(scopes, scope)- raise translate(locale, "Invalid scope")+ raise InfoException.new("Invalid scope")endif !Crypto::Subtle.constant_time_compare(token["signature"].to_s, sign_token(key, token))- raise translate(locale, "Invalid signature")+ raise InfoException.new("Invalid signature")endif token["nonce"]? && (nonce = db.query_one?("SELECT * FROM nonces WHERE nonce = $1", token["nonce"], as: {String, Time}))if nonce[1] > Time.utcdb.exec("UPDATE nonces SET expire = $1 WHERE nonce = $2", Time.utc(1990, 1, 1), nonce[0])else- raise translate(locale, "Erroneous token")+ raise InfoException.new("Erroneous token")endenddiff --git a/src/invidious/mixes.cr b/src/invidious/mixes.crindex c69eb0c4..55b01174 100644--- a/src/invidious/mixes.cr+++ b/src/invidious/mixes.cr@@ -30,7 +30,7 @@ def fetch_mix(rdid, video_id, cookies = nil, locale = nil)initial_data = extract_initial_data(response.body)if !initial_data["contents"]["twoColumnWatchNextResults"]["playlist"]?- raise translate(locale, "Could not create mix.")+ raise InfoException.new("Could not create mix.")endplaylist = initial_data["contents"]["twoColumnWatchNextResults"]["playlist"]["playlist"]diff --git a/src/invidious/playlists.cr b/src/invidious/playlists.crindex c984a12a..2389de49 100644--- a/src/invidious/playlists.cr+++ b/src/invidious/playlists.cr@@ -338,7 +338,7 @@ def get_playlist(db, plid, locale, refresh = true, force_refresh = false)if playlist = db.query_one?("SELECT * FROM playlists WHERE id = $1", plid, as: InvidiousPlaylist)return playlistelse- raise "Playlist does not exist."+ raise InfoException.new("Playlist does not exist.")endelsereturn fetch_playlist(plid, locale)@@ -353,16 +353,16 @@ def fetch_playlist(plid, locale)response = YT_POOL.client &.get("/playlist?list=#{plid}&hl=en")if response.status_code != 200if response.headers["location"]?.try &.includes? "/sorry/index"- raise "Could not extract playlist info. Instance is likely blocked."+ raise InfoException.new("Could not extract playlist info. Instance is likely blocked.")else- raise translate(locale, "Not a playlist.")+ raise InfoException.new("Not a playlist.")endendinitial_data = extract_initial_data(response.body)playlist_info = initial_data["sidebar"]?.try &.["playlistSidebarRenderer"]?.try &.["items"]?.try &.[0]["playlistSidebarPrimaryInfoRenderer"]?- raise "Could not extract playlist info" if !playlist_info+ raise InfoException.new("Could not extract playlist info") if !playlist_infotitle = playlist_info["title"]?.try &.["runs"][0]?.try &.["text"]?.try &.as_s || ""desc_item = playlist_info["description"]?@@ -390,7 +390,7 @@ def fetch_playlist(plid, locale)author_info = initial_data["sidebar"]?.try &.["playlistSidebarRenderer"]?.try &.["items"]?.try &.[1]["playlistSidebarSecondaryInfoRenderer"]?.try &.["videoOwner"]["videoOwnerRenderer"]?- raise "Could not extract author info" if !author_info+ raise InfoException.new("Could not extract author info") if !author_infoauthor_thumbnail = author_info["thumbnail"]["thumbnails"][0]["url"]?.try &.as_s || ""author = author_info["title"]["runs"][0]["text"]?.try &.as_s || ""diff --git a/src/invidious/routes/embed/index.cr b/src/invidious/routes/embed/index.crindex 79c91d86..32a4966b 100644--- a/src/invidious/routes/embed/index.cr+++ b/src/invidious/routes/embed/index.cr@@ -8,9 +8,7 @@ class Invidious::Routes::Embed::Index < Invidious::Routes::BaseRouteoffset = env.params.query["index"]?.try &.to_i? || 0videos = get_playlist_videos(PG_DB, playlist, offset: offset, locale: locale)rescue ex- error_message = ex.message- env.response.status_code = 500- return templated "error"+ return error_template(500, ex)endurl = "/embed/#{videos[0].id}?#{env.params.query}"diff --git a/src/invidious/routes/embed/show.cr b/src/invidious/routes/embed/show.crindex 23c2b86f..8a655556 100644--- a/src/invidious/routes/embed/show.cr+++ b/src/invidious/routes/embed/show.cr@@ -38,9 +38,7 @@ class Invidious::Routes::Embed::Show < Invidious::Routes::BaseRouteoffset = env.params.query["index"]?.try &.to_i? || 0videos = get_playlist_videos(PG_DB, playlist, offset: offset, locale: locale)rescue ex- error_message = ex.message- env.response.status_code = 500- return templated "error"+ return error_template(500, ex)endurl = "/embed/#{videos[0].id}"@@ -63,8 +61,7 @@ class Invidious::Routes::Embed::Show < Invidious::Routes::BaseRouteenv.params.query.delete_all("channel")if !video_id || video_id == "live_stream"- error_message = "Video is unavailable."- return templated "error"+ return error_template(500, "Video is unavailable.")endurl = "/embed/#{video_id}"@@ -100,9 +97,7 @@ class Invidious::Routes::Embed::Show < Invidious::Routes::BaseRouterescue ex : VideoRedirectreturn env.redirect env.request.resource.gsub(id, ex.video_id)rescue ex- error_message = ex.message- env.response.status_code = 500- return templated "error"+ return error_template(500, ex)endif preferences.annotations_subscribed &&diff --git a/src/invidious/routes/playlists.cr b/src/invidious/routes/playlists.crindex 52acf266..6c899054 100644--- a/src/invidious/routes/playlists.cr+++ b/src/invidious/routes/playlists.cr@@ -56,26 +56,21 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoutebeginvalidate_request(token, sid, env.request, HMAC_KEY, PG_DB, locale)rescue ex- error_message = ex.message- env.response.status_code = 400- return templated "error"+ return error_template(400, ex)endtitle = env.params.body["title"]?.try &.as(String)if !title || title.empty?- error_message = "Title cannot be empty."- return templated "error"+ return error_template(400, "Title cannot be empty.")endprivacy = PlaylistPrivacy.parse?(env.params.body["privacy"]?.try &.as(String) || "")if !privacy- error_message = "Invalid privacy setting."- return templated "error"+ return error_template(400, "Invalid privacy setting.")endif PG_DB.query_one("SELECT count(*) FROM playlists WHERE author = $1", user.email, as: Int64) >= 100- error_message = "User cannot have more than 100 playlists."- return templated "error"+ return error_template(400, "User cannot have more than 100 playlists.")endplaylist = create_playlist(PG_DB, title, privacy, user)@@ -142,9 +137,7 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoutebeginvalidate_request(token, sid, env.request, HMAC_KEY, PG_DB, locale)rescue ex- error_message = ex.message- env.response.status_code = 400- return templated "error"+ return error_template(400, ex)endplaylist = PG_DB.query_one?("SELECT * FROM playlists WHERE id = $1", plid, as: InvidiousPlaylist)@@ -217,9 +210,7 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoutebeginvalidate_request(token, sid, env.request, HMAC_KEY, PG_DB, locale)rescue ex- error_message = ex.message- env.response.status_code = 400- return templated "error"+ return error_template(400, ex)endplaylist = PG_DB.query_one?("SELECT * FROM playlists WHERE id = $1", plid, as: InvidiousPlaylist)@@ -306,9 +297,7 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRouteif redirectreturn env.redirect refererelse- error_message = {"error" => "No such user"}.to_json- env.response.status_code = 403- return error_message+ return error_json(403, "No such user")endend@@ -320,13 +309,9 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoutevalidate_request(token, sid, env.request, HMAC_KEY, PG_DB, locale)rescue exif redirect- error_message = ex.message- env.response.status_code = 400- return templated "error"+ return error_template(400, ex)else- error_message = {"error" => ex.message}.to_json- env.response.status_code = 400- return error_message+ return error_json(400, ex)endend@@ -353,13 +338,9 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRouteraise "Invalid user" if playlist.author != user.emailrescue exif redirect- error_message = ex.message- env.response.status_code = 400- return templated "error"+ return error_template(400, ex)else- error_message = {"error" => ex.message}.to_json- env.response.status_code = 400- return error_message+ return error_json(400, ex)endend@@ -374,13 +355,10 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoute# TODO: Playlist stubwhen "action_add_video"if playlist.index.size >= 500- env.response.status_code = 400if redirect- error_message = "Playlist cannot have more than 500 videos"- return templated "error"+ return error_template(400, "Playlist cannot have more than 500 videos")else- error_message = {"error" => "Playlist cannot have more than 500 videos"}.to_json- return error_message+ return error_json(400, "Playlist cannot have more than 500 videos")endend@@ -389,13 +367,10 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoutebeginvideo = get_video(video_id, PG_DB)rescue ex- env.response.status_code = 500if redirect- error_message = ex.message- return templated "error"+ return error_template(500, ex)else- error_message = {"error" => ex.message}.to_json- return error_message+ return error_json(500, ex)endend@@ -423,9 +398,7 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoutewhen "action_move_video_before"# TODO: Playlist stubelse- error_message = {"error" => "Unsupported action #{action}"}.to_json- env.response.status_code = 400- return error_message+ return error_json(400, "Unsupported action #{action}")endif redirect@@ -457,15 +430,11 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoutebeginplaylist = get_playlist(PG_DB, plid, locale)rescue ex- error_message = ex.message- env.response.status_code = 500- return templated "error"+ return error_template(500, ex)endif playlist.privacy == PlaylistPrivacy::Private && playlist.author != user.try &.email- error_message = "This playlist is private."- env.response.status_code = 403- return templated "error"+ return error_template(403, "This playlist is private.")endbegin@@ -495,9 +464,7 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoutebeginmix = fetch_mix(rdid, continuation, locale: locale)rescue ex- error_message = ex.message- env.response.status_code = 500- return templated "error"+ return error_template(500, ex)endtemplated "mix"diff --git a/src/invidious/routes/watch.cr b/src/invidious/routes/watch.crindex 4eee7793..7018d0a4 100644--- a/src/invidious/routes/watch.cr+++ b/src/invidious/routes/watch.cr@@ -12,9 +12,7 @@ class Invidious::Routes::Watch < Invidious::Routes::BaseRouteid = env.params.query["v"]if env.params.query["v"].empty?- error_message = "Invalid parameters."- env.response.status_code = 400- return templated "error"+ return error_template(400, "Invalid parameters.")endif id.size > 11@@ -56,10 +54,8 @@ class Invidious::Routes::Watch < Invidious::Routes::BaseRouterescue ex : VideoRedirectreturn env.redirect env.request.resource.gsub(id, ex.video_id)rescue ex- error_message = ex.message- env.response.status_code = 500logger.puts("#{id} : #{ex.message}")- return templated "error"+ return error_template(500, ex)endif preferences.annotations_subscribed &&