Iqdb::Download.find_similar: raise on iqdb errors.

* Raise an error on iqdb errors instead of returning no matches.
* Include the iqdb api response in the error response.
This commit is contained in:
evazion
2018-04-07 11:42:06 -05:00
parent b88a66dd30
commit 8de20d2b36

View File

@@ -1,5 +1,7 @@
module Iqdb
class Download
class Error < StandardError; end
def self.enabled?
Danbooru.config.iqdbs_server.present? && Danbooru.config.iqdbs_auth_key.present?
end
@@ -31,17 +33,17 @@ module Iqdb
raise "HTTP error code: #{resp.code} #{resp.message}" unless resp.success?
json = JSON.parse(resp.body)
if json.is_a?(Array)
post_ids = json.map { |match| match["post_id"] }
posts = Post.find(post_ids)
raise "IQDB error: #{json["error"]}" unless json.is_a?(Array)
json.map do |match|
post = posts.find { |post| post.id == match["post_id"] }
match.with_indifferent_access.merge({ post: post })
end
else
[]
post_ids = json.map { |match| match["post_id"] }
posts = Post.find(post_ids)
json.map do |match|
post = posts.find { |post| post.id == match["post_id"] }
match.with_indifferent_access.merge({ post: post })
end
rescue => e
raise Error, { message: e.message, iqdb_response: json }
end
end
end