From 6b1e69165bcd4c254927b17a44ba47cc0f53010d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fernando=20Mart=C3=ADnez?= Date: Wed, 15 Jul 2020 13:46:28 -0300 Subject: [PATCH] Make error polymorphic with Response https://github.com/cyx/requests/issues/9 --- lib/requests.rb | 5 ++++- tests/proxy_test.rb | 2 +- tests/requests_test.rb | 4 ++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/requests.rb b/lib/requests.rb index 74a697d..4ce1711 100644 --- a/lib/requests.rb +++ b/lib/requests.rb @@ -5,12 +5,15 @@ module Requests class Error < StandardError - attr_reader :response + attr_reader :response, :status, :headers, :body def initialize(response) super(response.message) @response = response + @status = Integer(response.code) + @headers = response.to_hash + @body = response.body end end diff --git a/tests/proxy_test.rb b/tests/proxy_test.rb index 0acb364..19551a2 100644 --- a/tests/proxy_test.rb +++ b/tests/proxy_test.rb @@ -26,7 +26,7 @@ assert_equal ['application/json'], r.headers['content-type'] assert(r.json['args'] && r.json['args']['foo'] == 'bar') - assert_equal ["1.1 vegur, 1.1 0.0.0.0:#{port}"], r.headers['via'] + assert_equal ["1.1 0.0.0.0:#{port}"], r.headers['via'] proxy.shutdown end diff --git a/tests/requests_test.rb b/tests/requests_test.rb index a15a39b..85bab4a 100644 --- a/tests/requests_test.rb +++ b/tests/requests_test.rb @@ -55,6 +55,10 @@ Requests.post('http://httpbin.org/something') rescue Requests::Error => e assert_equal Net::HTTPNotFound, e.response.class + + assert_equal Hash, e.headers.class + assert_equal String, e.body.class + assert_equal 404, e.status end end