Iqdb::Download.find_similar: simplify error handling.

Raise errors immediately to avoid deeply nested if-else statements.
This commit is contained in:
evazion
2018-04-07 09:19:32 -05:00
parent d8142a6c21
commit b88a66dd30

View File

@@ -16,7 +16,8 @@ module Iqdb
end end
def self.find_similar(source) def self.find_similar(source)
if Danbooru.config.iqdbs_server raise NotImplementedError, "the IQDBs service isn't configured. Similarity searches are not available." unless enabled?
url, ref = get_referer(source) url, ref = get_referer(source)
params = { params = {
"key" => Danbooru.config.iqdbs_auth_key, "key" => Danbooru.config.iqdbs_auth_key,
@@ -27,7 +28,8 @@ module Iqdb
uri.query = URI.encode_www_form(params) uri.query = URI.encode_www_form(params)
resp = HTTParty.get(uri, Danbooru.config.httparty_options) resp = HTTParty.get(uri, Danbooru.config.httparty_options)
if resp.success? raise "HTTP error code: #{resp.code} #{resp.message}" unless resp.success?
json = JSON.parse(resp.body) json = JSON.parse(resp.body)
if json.is_a?(Array) if json.is_a?(Array)
post_ids = json.map { |match| match["post_id"] } post_ids = json.map { |match| match["post_id"] }
@@ -40,12 +42,6 @@ module Iqdb
else else
[] []
end end
else
raise "HTTP error code: #{resp.code} #{resp.message}"
end
else
raise NotImplementedError, "the IQDBs service isn't configured. Similarity searches are not available." unless enabled?
end
end end
end end
end end