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