Add backtraces to errors (#1498)

Error handling has been reworked to always go through the new `error_template`,
`error_json` and `error_atom` macros.
They all accept a status code followed by a string message or an exception
object. `error_json` accepts a hash with additional fields as third argument.

If the second argument is an exception a backtrace will be printed, if it is a
string only the string is printed. Since up till now only the exception message
was printed a new `InfoException` class was added for situations where no
backtrace is intended but a string cannot be used.

`error_template` with a string message automatically localizes the message.
Missing error translations have been collected in https://github.com/iv-org/invidious/issues/1497
`error_json` with a string message does not localize the message. This is the
same as previous behavior. If translations are desired for `error_json` they
can be added easily but those error messages have not been collected yet.

Uncaught exceptions previously only printed a generic message ("Looks like
you've found a bug in Invidious. [...]"). They still print that message
but now also include a backtrace.
This commit is contained in:
saltycrys
2020-11-30 10:59:21 +01:00
committed by GitHub
parent fe73eccb90
commit 3dac33ffba
11 changed files with 250 additions and 378 deletions

View File

@ -8,9 +8,7 @@ class Invidious::Routes::Embed::Index < Invidious::Routes::BaseRoute
offset = env.params.query["index"]?.try &.to_i? || 0
videos = 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)
end
url = "/embed/#{videos[0].id}?#{env.params.query}"

View File

@ -38,9 +38,7 @@ class Invidious::Routes::Embed::Show < Invidious::Routes::BaseRoute
offset = env.params.query["index"]?.try &.to_i? || 0
videos = 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)
end
url = "/embed/#{videos[0].id}"
@ -63,8 +61,7 @@ class Invidious::Routes::Embed::Show < Invidious::Routes::BaseRoute
env.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.")
end
url = "/embed/#{video_id}"
@ -100,9 +97,7 @@ class Invidious::Routes::Embed::Show < Invidious::Routes::BaseRoute
rescue ex : VideoRedirect
return 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)
end
if preferences.annotations_subscribed &&

View File

@ -56,26 +56,21 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoute
begin
validate_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)
end
title = 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.")
end
privacy = 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.")
end
if 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.")
end
playlist = create_playlist(PG_DB, title, privacy, user)
@ -142,9 +137,7 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoute
begin
validate_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)
end
playlist = PG_DB.query_one?("SELECT * FROM playlists WHERE id = $1", plid, as: InvidiousPlaylist)
@ -217,9 +210,7 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoute
begin
validate_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)
end
playlist = PG_DB.query_one?("SELECT * FROM playlists WHERE id = $1", plid, as: InvidiousPlaylist)
@ -306,9 +297,7 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoute
if redirect
return env.redirect referer
else
error_message = {"error" => "No such user"}.to_json
env.response.status_code = 403
return error_message
return error_json(403, "No such user")
end
end
@ -320,13 +309,9 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoute
validate_request(token, sid, env.request, HMAC_KEY, PG_DB, locale)
rescue ex
if 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)
end
end
@ -353,13 +338,9 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoute
raise "Invalid user" if playlist.author != user.email
rescue ex
if 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)
end
end
@ -374,13 +355,10 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoute
# TODO: Playlist stub
when "action_add_video"
if playlist.index.size >= 500
env.response.status_code = 400
if 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")
end
end
@ -389,13 +367,10 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoute
begin
video = get_video(video_id, PG_DB)
rescue ex
env.response.status_code = 500
if 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)
end
end
@ -423,9 +398,7 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoute
when "action_move_video_before"
# TODO: Playlist stub
else
error_message = {"error" => "Unsupported action #{action}"}.to_json
env.response.status_code = 400
return error_message
return error_json(400, "Unsupported action #{action}")
end
if redirect
@ -457,15 +430,11 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoute
begin
playlist = 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)
end
if 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.")
end
begin
@ -495,9 +464,7 @@ class Invidious::Routes::Playlists < Invidious::Routes::BaseRoute
begin
mix = 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)
end
templated "mix"

View File

@ -12,9 +12,7 @@ class Invidious::Routes::Watch < Invidious::Routes::BaseRoute
id = 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.")
end
if id.size > 11
@ -56,10 +54,8 @@ class Invidious::Routes::Watch < Invidious::Routes::BaseRoute
rescue ex : VideoRedirect
return env.redirect env.request.resource.gsub(id, ex.video_id)
rescue ex
error_message = ex.message
env.response.status_code = 500
logger.puts("#{id} : #{ex.message}")
return templated "error"
return error_template(500, ex)
end
if preferences.annotations_subscribed &&