From 117d31e6336b04fb3a60449ab2d33abdb1aeee09 Mon Sep 17 00:00:00 2001 From: evazion Date: Sat, 12 Feb 2022 15:14:08 -0600 Subject: [PATCH] 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. --- app/logical/danbooru/http.rb | 16 ++++++++-------- test/unit/danbooru_http_test.rb | 4 ++++ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/app/logical/danbooru/http.rb b/app/logical/danbooru/http.rb index 0bd59c650..2508abe86 100644 --- a/app/logical/danbooru/http.rb +++ b/app/logical/danbooru/http.rb @@ -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 diff --git a/test/unit/danbooru_http_test.rb b/test/unit/danbooru_http_test.rb index 61c2ed1d1..29e7fb509 100644 --- a/test/unit/danbooru_http_test.rb +++ b/test/unit/danbooru_http_test.rb @@ -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