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

@@ -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 playlist
else
raise "Playlist does not exist."
raise InfoException.new("Playlist does not exist.")
end
else
return 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 != 200
if 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.")
end
end
initial_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_info
title = 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_info
author_thumbnail = author_info["thumbnail"]["thumbnails"][0]["url"]?.try &.as_s || ""
author = author_info["title"]["runs"][0]["text"]?.try &.as_s || ""