mirror of
https://github.com/HoneyryderChuck/httpx.git
synced 2025-08-10 00:01:27 -04:00
added Response#raise_for_status, which raises an exception if the http code is an error code; it also duck-types the error response, which will raise its exception
This commit is contained in:
parent
0b4c6fc8c3
commit
67eb755a3b
@ -60,6 +60,11 @@ module HTTPX
|
||||
"#<Response:#{object_id} @status=#{@status} @headers=#{@headers}>"
|
||||
end
|
||||
|
||||
def raise_for_status
|
||||
return if @status < 400
|
||||
raise HTTPError, @status
|
||||
end
|
||||
|
||||
class Body
|
||||
def initialize(response, threshold_size:, window_size: 1 << 14)
|
||||
@response = response
|
||||
@ -224,6 +229,10 @@ module HTTPX
|
||||
@error.message
|
||||
end
|
||||
|
||||
def raise_for_status
|
||||
raise @error
|
||||
end
|
||||
|
||||
def retryable?
|
||||
@retries.positive?
|
||||
end
|
||||
|
@ -22,6 +22,17 @@ class ResponseTest < Minitest::Test
|
||||
assert resource.body == "data", "body should have been updated"
|
||||
end
|
||||
|
||||
def test_raise_for_status
|
||||
r1 = Response.new(request, 200, "2.0", {})
|
||||
r1.raise_for_status
|
||||
r2 = Response.new(request, 302, "2.0", {})
|
||||
r2.raise_for_status
|
||||
r3 = Response.new(request, 404, "2.0", {})
|
||||
assert_raises(HTTPX::HTTPError) { r3.raise_for_status }
|
||||
r4 = Response.new(request, 500, "2.0", {})
|
||||
assert_raises(HTTPX::HTTPError) { r4.raise_for_status }
|
||||
end
|
||||
|
||||
def test_response_body_to_s
|
||||
opts = { threshold_size: 1024 }
|
||||
body1 = Response::Body.new(Response.new(request, 200, "2.0", {}), opts)
|
||||
|
Loading…
x
Reference in New Issue
Block a user