From 26e2e86fe71eca9030dd9379c3ca2bff7582ecc4 Mon Sep 17 00:00:00 2001 From: Mattia Giuffrida Date: Thu, 15 Jun 2023 16:58:35 +0100 Subject: [PATCH] Fix implementation of Faraday::Error helpers. Fix #1509 --- lib/faraday/error.rb | 12 +++++++++--- spec/faraday/error_spec.rb | 37 +++++++++++++++++++++++++++++++------ 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/lib/faraday/error.rb b/lib/faraday/error.rb index 2f0c76ce0..32354b3eb 100644 --- a/lib/faraday/error.rb +++ b/lib/faraday/error.rb @@ -29,15 +29,21 @@ def inspect end def response_status - @response[:status] if @response + return unless @response + + @response.is_a?(Faraday::Response) ? @response.status : @response[:status] end def response_headers - @response[:headers] if @response + return unless @response + + @response.is_a?(Faraday::Response) ? @response.headers : @response[:headers] end def response_body - @response[:body] if @response + return unless @response + + @response.is_a?(Faraday::Response) ? @response.body : @response[:body] end protected diff --git a/spec/faraday/error_spec.rb b/spec/faraday/error_spec.rb index bea9f0c34..fd30e2d34 100644 --- a/spec/faraday/error_spec.rb +++ b/spec/faraday/error_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -RSpec.describe Faraday::ClientError do +RSpec.describe Faraday::Error do describe '.initialize' do subject { described_class.new(exception, response) } let(:response) { nil } @@ -12,8 +12,10 @@ it { expect(subject.response).to be_nil } it { expect(subject.message).to eq(exception.message) } it { expect(subject.backtrace).to eq(exception.backtrace) } - it { expect(subject.inspect).to eq('#>') } + it { expect(subject.inspect).to eq('#>') } it { expect(subject.response_status).to be_nil } + it { expect(subject.response_headers).to be_nil } + it { expect(subject.response_body).to be_nil } end context 'with response hash' do @@ -22,8 +24,10 @@ it { expect(subject.wrapped_exception).to be_nil } it { expect(subject.response).to eq(exception) } it { expect(subject.message).to eq('the server responded with status 400') } - it { expect(subject.inspect).to eq('#400}>') } + it { expect(subject.inspect).to eq('#400}>') } it { expect(subject.response_status).to eq(400) } + it { expect(subject.response_headers).to be_nil } + it { expect(subject.response_body).to be_nil } end context 'with string' do @@ -32,8 +36,10 @@ it { expect(subject.wrapped_exception).to be_nil } it { expect(subject.response).to be_nil } it { expect(subject.message).to eq('custom message') } - it { expect(subject.inspect).to eq('#>') } + it { expect(subject.inspect).to eq('#>') } it { expect(subject.response_status).to be_nil } + it { expect(subject.response_headers).to be_nil } + it { expect(subject.response_body).to be_nil } end context 'with anything else #to_s' do @@ -42,8 +48,10 @@ it { expect(subject.wrapped_exception).to be_nil } it { expect(subject.response).to be_nil } it { expect(subject.message).to eq('["error1", "error2"]') } - it { expect(subject.inspect).to eq('#>') } + it { expect(subject.inspect).to eq('#>') } it { expect(subject.response_status).to be_nil } + it { expect(subject.response_headers).to be_nil } + it { expect(subject.response_body).to be_nil } end context 'with exception string and response hash' do @@ -53,8 +61,25 @@ it { expect(subject.wrapped_exception).to be_nil } it { expect(subject.response).to eq(response) } it { expect(subject.message).to eq('custom message') } - it { expect(subject.inspect).to eq('#400}>') } + it { expect(subject.inspect).to eq('#400}>') } it { expect(subject.response_status).to eq(400) } + it { expect(subject.response_headers).to be_nil } + it { expect(subject.response_body).to be_nil } + end + + context 'with exception and response object' do + let(:exception) { RuntimeError.new('test') } + let(:body) { { test: 'test' } } + let(:headers) { { 'Content-Type' => 'application/json' } } + let(:response) { Faraday::Response.new(status: 400, response_headers: headers, response_body: body) } + + it { expect(subject.wrapped_exception).to eq(exception) } + it { expect(subject.response).to eq(response) } + it { expect(subject.message).to eq(exception.message) } + it { expect(subject.backtrace).to eq(exception.backtrace) } + it { expect(subject.response_status).to eq(400) } + it { expect(subject.response_headers).to eq(headers) } + it { expect(subject.response_body).to eq(body) } end end end