simplify ErrorResponse by fetching options from the request, like Response

This commit is contained in:
HoneyryderChuck 2024-03-22 18:45:46 +00:00
parent 8b2ee0b466
commit 0b671fa2f9
11 changed files with 17 additions and 16 deletions

View File

@ -47,7 +47,7 @@ module WebMock
end
def build_error_response(request, exception)
HTTPX::ErrorResponse.new(request, exception, request.options)
HTTPX::ErrorResponse.new(request, exception)
end
end

View File

@ -506,7 +506,7 @@ module HTTPX
when MisdirectedRequestError
emit(:misdirected, request)
else
response = ErrorResponse.new(request, ex, @options)
response = ErrorResponse.new(request, ex)
request.response = response
request.emit(:response, response)
end
@ -659,7 +659,7 @@ module HTTPX
def handle_error(error)
parser.handle_error(error) if @parser && parser.respond_to?(:handle_error)
while (request = @pending.shift)
response = ErrorResponse.new(request, error, request.options)
response = ErrorResponse.new(request, error)
request.response = response
request.emit(:response, response)
end

View File

@ -146,7 +146,7 @@ module HTTPX
response << chunk
rescue StandardError => e
error_response = ErrorResponse.new(request, e, request.options)
error_response = ErrorResponse.new(request, e)
request.response = error_response
dispatch
end

View File

@ -309,7 +309,7 @@ module HTTPX
if error
ex = Error.new(stream.id, error)
ex.set_backtrace(caller)
response = ErrorResponse.new(request, ex, request.options)
response = ErrorResponse.new(request, ex)
request.response = response
emit(:response, request, response)
else

View File

@ -79,8 +79,9 @@ module HTTPX
# the Location field. The Location field gives the URI of the proxy.
redirect_options = options.merge(headers: redirect_request.headers,
proxy: { uri: redirect_uri },
body: request_body,
max_redirects: max_redirects - 1)
redirect_params[:body] = request_body
redirect_uri = redirect_request.uri
options = redirect_options
else
@ -113,7 +114,7 @@ module HTTPX
redirect_uri.scheme == "http"
error = InsecureRedirectError.new(redirect_uri.to_s)
error.set_backtrace(caller)
return ErrorResponse.new(request, error, options)
return ErrorResponse.new(request, error)
end
retry_request = build_request(redirect_method, redirect_uri, redirect_params, options)

View File

@ -100,7 +100,7 @@ module HTTPX
error = ServerSideRequestForgeryError.new("#{request.uri} URI scheme not allowed")
error.set_backtrace(caller)
response = ErrorResponse.new(request, error, request.options)
response = ErrorResponse.new(request, error)
request.emit(:response, response)
response
end

View File

@ -247,11 +247,11 @@ module HTTPX
# the IP address of the peer server.
def_delegator :@request, :peer_address
def initialize(request, error, options)
def initialize(request, error)
@request = request
@response = request.response if request.response.is_a?(Response)
@error = error
@options = options
@options = request.options
log_exception(@error)
end

View File

@ -132,7 +132,7 @@ module HTTPX
end
return unless error.is_a?(Error)
request.emit(:response, ErrorResponse.new(request, error, options))
request.emit(:response, ErrorResponse.new(request, error))
end
# sets the callbacks on the +connection+ required to process certain specific

View File

@ -91,7 +91,7 @@ module HTTPX
private
def initialize: (Request, Exception, options) -> untyped
def initialize: (Request, Exception) -> untyped
end
type response = Response | ErrorResponse

View File

@ -40,7 +40,7 @@ class ErrorResponseTest < Minitest::Test
def test_error_response_body_write
response = Response.new(request_mock, 200, "1.1", {})
request_mock.response = response
r = ErrorResponse.new(request_mock, RuntimeError.new("wow"), {})
r = ErrorResponse.new(request_mock, RuntimeError.new("wow"))
assert response.body.empty?, "body should be empty after init"
r << "data"
assert response.body == "data", "body should have been updated"
@ -53,6 +53,6 @@ class ErrorResponseTest < Minitest::Test
end
def make_error_response(*args)
ErrorResponse.new(request_mock, *args, request_mock.options)
ErrorResponse.new(request_mock, *args)
end
end

View File

@ -22,7 +22,7 @@ module ResponsePatternMatchTests
Response.new(request, 202, "2.0", { "x-success" => "true" }),
Response.new(request, 400, "2.0", { "x-client-error" => "true" }),
Response.new(request, 500, "2.0", { "x-server-error" => "true" }),
ErrorResponse.new(request, StandardError.new("match error"), Options.new),
ErrorResponse.new(request, StandardError.new("match error")),
]
responses.each do |response|
@ -52,7 +52,7 @@ module ResponsePatternMatchTests
Response.new(request, 202, "2.0", { "x-success" => "true" }),
Response.new(request, 400, "2.0", { "x-client-error" => "true" }),
Response.new(request, 500, "2.0", { "x-server-error" => "true" }),
ErrorResponse.new(request, StandardError.new("match error"), Options.new),
ErrorResponse.new(request, StandardError.new("match error")),
]
responses.each do |response|