Add special error page for InitialInnerTubeParseException

This commit is contained in:
syeopite 2021-11-14 08:37:50 -08:00
parent 072977537f
commit a715becca3
No known key found for this signature in database
GPG key ID: 6FA616E5A5294A82
3 changed files with 65 additions and 4 deletions

View file

@ -15,8 +15,11 @@ def github_details_backtrace(summary : String, content : String)
end
def generic_error_template_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, exception : Exception)
# Custom error routing
if exception.is_a?(InfoException)
return generic_error_template_helper(env, locale, status_code, exception.message || "")
elsif exception.is_a? InitialInnerTubeParseException
return exception.error_template_helper(env, locale)
end
env.response.content_type = "text/html"

View file

@ -14,10 +14,49 @@ end
# the error page for debugging/research purposes.
#
class InitialInnerTubeParseException < Exception
# temporally place holder
def self.new(parse_exception, **kwargs)
def initialize(@endpoint : String, @client_config : String, @data : String, @status_code : Int32, @mime_type : String, @cause : Exception)
end
def self.new(parse_exception : Exception,
endpoint : String,
client_config : String,
data : String,
status_code : Int32,
mime_type : String)
instance = InitialInnerTubeParseException.allocate
instance.initialize(error_message, parse_exception)
instance.initialize(endpoint, client_config, data, status_code, mime_type, cause: parse_exception)
return instance
end
private def render_innertube_metadata_section(locale)
contents = %(\n\n<details>)
contents += %(\n<summary>InnerTube request metadata</summary>)
contents += %(\n<p>\n)
contents += %(\n \n```\n)
contents += %(Endpoint: `#{@endpoint}`\n)
contents += %(\nClient config: ```json\n#{@client_config}\n```\n)
contents += %(\nData: ```json\n#{@data}\n```\n)
contents += %(\nStatus code: `#{@status_code}`\n)
contents += %(MIME type: `#{@mime_type}`)
contents += %(\n```)
contents += %(\n</p>)
contents += %(\n</details>)
return HTML.escape(contents)
end
def error_template_helper(env, locale)
env.response.content_type = "text/html"
env.response.status_code = 500
# HTML rendering.
exception = @cause.not_nil!
backtrace = github_details_backtrace("Backtrace", @cause.not_nil!.inspect_with_backtrace)
backtrace += render_innertube_metadata_section(locale)
error_message = rendered "error_pages/generic"
return templated "error_pages/generic_wrapper"
end
end

View file

@ -135,6 +135,15 @@ module YoutubeAPI
proxy_region: @proxy_region,
}.to_s
end
# Converts to hash for displaying on error pages
def to_h
return {
client_type: self.name,
region: @region,
proxy_region: @proxy_region,
}
end
end
# Default client config, used if nothing is passed
@ -435,7 +444,17 @@ module YoutubeAPI
end
# Convert result to Hash
initial_data = JSON.parse(response.body).as_h
begin
initial_data = JSON.parse(response.body).as_h
rescue ex
raise InitialInnerTubeParseException.new(ex,
endpoint: endpoint.to_s,
client_config: client_config.to_h.to_pretty_json,
data: data.to_pretty_json,
status_code: response.status_code,
mime_type: "#{response.mime_type.try &.media_type}"
)
end
# Error handling
if initial_data.has_key?("error")