Fix undefined method readpartial' for \"\":String` error.

This exception was thrown by app/logical/pixiv_ajax_client.rb:406 when a
Pixiv API call failed with a network error. In this case we tried to log
the response body, but this failed because we returned a faked HTTP
response with an empty string for the body, which the http.rb library
didn't like because it was expecting an IO-like object for the body.
This commit is contained in:
evazion
2022-02-12 15:14:08 -06:00
parent b5b5cc77bd
commit 117d31e633
2 changed files with 12 additions and 8 deletions

View File

@@ -174,17 +174,17 @@ module Danbooru
def request(method, url, **options)
http.send(method, url, **options)
rescue OpenSSL::SSL::SSLError
fake_response(590, "")
fake_response(590)
rescue ValidatingSocket::ProhibitedIpError
fake_response(591, "")
fake_response(591)
rescue HTTP::Redirector::TooManyRedirectsError
fake_response(596, "")
fake_response(596)
rescue HTTP::TimeoutError
fake_response(597, "")
fake_response(597)
rescue HTTP::ConnectionError
fake_response(598, "")
fake_response(598)
rescue HTTP::Error
fake_response(599, "")
fake_response(599)
end
# Perform a HTTP request for the given URL, raising an error on 4xx or 5xx
@@ -205,8 +205,8 @@ module Danbooru
end
end
def fake_response(status, body)
::HTTP::Response.new(status: status, version: "1.1", body: ::HTTP::Response::Body.new(body))
def fake_response(status)
::HTTP::Response.new(status: status, version: "1.1", body: "")
end
end
end

View File

@@ -22,11 +22,13 @@ class DanbooruHttpTest < ActiveSupport::TestCase
should "fail if redirected too many times" do
response = Danbooru::Http.get(httpbin_url("absolute-redirect/10"))
assert_equal(596, response.status)
assert_equal("", response.body.to_s)
end
should "fail if the request takes too long to connect" do
response = Danbooru::Http.timeout(1).get(httpbin_url("delay/5"))
assert_equal(597, response.status)
assert_equal("", response.body.to_s)
end
should "fail if the request takes too long to download" do
@@ -39,11 +41,13 @@ class DanbooruHttpTest < ActiveSupport::TestCase
should "return a 5xx error if the domain can't be resolved" do
response = Danbooru::Http.get("http://doesnotexist.donmai.us")
assert_equal(598, response.status)
assert_equal("", response.body.to_s)
end
should "return a 5xx error if the SSL certificate is expired" do
response = Danbooru::Http.get("https://expired.badssl.com")
assert_equal(590, response.status)
assert_equal("", response.body.to_s)
end
should "automatically decompress gzipped responses" do