faraday 2.13.2 → 2.13.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ba5077c825db9905310c665e101ad938f6e169dc9a887cbd03314434a7b37405
4
- data.tar.gz: dc66440fa21267c217c7f5ee12d490eb039bcd6eed2b1efd727963c4d92eee16
3
+ metadata.gz: 12dedb476a26ae00e5f902e4e10f5327a790290d2d7647711aac09e8661af959
4
+ data.tar.gz: f43c7bb1127b0a57bb7aa0727a096e0b66cfeead93445631dacf151c354efbdb
5
5
  SHA512:
6
- metadata.gz: f55feaf0423e9169ad01017c18479dbac065fd1bce8403a82d841fac49e2845f420b0bb1bae5b3bf1b1dba5dbc03796f10997ea8ec7c15148b8ed1f5770cd81b
7
- data.tar.gz: 875b2d50d365f7771e825949a483b433e7c87a969bb721c6024cb575832ddf02013c9252be5c2285008c4d4177ce9d085caaeb4940b014db5b505ed6bd25c226
6
+ metadata.gz: b3cb808ea984cc306ad71fc502bf79541d5e79fb9ca066784e0674dc71a2dc22ac20b6968065cceaa3fe5ccb909124aeef9513a37bf83bbcb4a63e116dbd6afe
7
+ data.tar.gz: 1540d3fe594b1d0a025caede980b99419de50d4ea148b53bf5cf7a3082307982c0917fd739621805252f8016e427ffdbfba59da5a679b529380adf0100cf10ef
data/lib/faraday/error.rb CHANGED
@@ -79,26 +79,47 @@ module Faraday
79
79
 
80
80
  # Pulls out potential parent exception and response hash.
81
81
  def exc_msg_and_response(exc, response = nil)
82
- if exc.is_a?(Exception)
82
+ case exc
83
+ when Exception
83
84
  [exc, exc.message, response]
84
- elsif exc.is_a?(Hash)
85
- http_status = exc.fetch(:status)
86
-
87
- request = exc.fetch(:request, nil)
88
-
89
- if request.nil?
90
- exception_message = "the server responded with status #{http_status} - method and url are not available " \
91
- 'due to include_request: false on Faraday::Response::RaiseError middleware'
92
- else
93
- exception_message = "the server responded with status #{http_status} for #{request.fetch(:method).upcase} " \
94
- "#{request.fetch(:url)}"
95
- end
96
-
97
- [nil, exception_message, exc]
85
+ when Hash
86
+ [nil, build_error_message_from_hash(exc), exc]
87
+ when Faraday::Env
88
+ [nil, build_error_message_from_env(exc), exc]
98
89
  else
99
90
  [nil, exc.to_s, response]
100
91
  end
101
92
  end
93
+
94
+ private
95
+
96
+ def build_error_message_from_hash(hash)
97
+ # Be defensive with external Hash objects - they might be missing keys
98
+ status = hash.fetch(:status, nil)
99
+ request = hash.fetch(:request, nil)
100
+
101
+ return fallback_error_message(status) if request.nil?
102
+
103
+ method = request.fetch(:method, nil)
104
+ url = request.fetch(:url, nil)
105
+ build_status_error_message(status, method, url)
106
+ end
107
+
108
+ def build_error_message_from_env(env)
109
+ # Faraday::Env is internal - we can make reasonable assumptions about its structure
110
+ build_status_error_message(env.status, env.method, env.url)
111
+ end
112
+
113
+ def build_status_error_message(status, method, url)
114
+ method_str = method ? method.to_s.upcase : ''
115
+ url_str = url ? url.to_s : ''
116
+ "the server responded with status #{status} for #{method_str} #{url_str}"
117
+ end
118
+
119
+ def fallback_error_message(status)
120
+ "the server responded with status #{status} - method and url are not available " \
121
+ 'due to include_request: false on Faraday::Response::RaiseError middleware'
122
+ end
102
123
  end
103
124
 
104
125
  # Faraday client error class. Represents 4xx status responses.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Faraday
4
- VERSION = '2.13.2'
4
+ VERSION = '2.13.4'
5
5
  end
@@ -89,5 +89,87 @@ RSpec.describe Faraday::Error do
89
89
  it { expect(subject.response_headers).to eq(headers) }
90
90
  it { expect(subject.response_body).to eq(body) }
91
91
  end
92
+
93
+ context 'with hash missing status key' do
94
+ let(:exception) { { body: 'error body' } }
95
+
96
+ it { expect(subject.wrapped_exception).to be_nil }
97
+ it { expect(subject.response).to eq(exception) }
98
+ it { expect(subject.message).to eq('the server responded with status - method and url are not available due to include_request: false on Faraday::Response::RaiseError middleware') }
99
+ end
100
+
101
+ context 'with hash with status but missing request data' do
102
+ let(:exception) { { status: 404, body: 'not found' } } # missing request key
103
+
104
+ it { expect(subject.wrapped_exception).to be_nil }
105
+ it { expect(subject.response).to eq(exception) }
106
+ it { expect(subject.message).to eq('the server responded with status 404 - method and url are not available due to include_request: false on Faraday::Response::RaiseError middleware') }
107
+ end
108
+
109
+ context 'with hash with status and request but missing method in request' do
110
+ let(:exception) { { status: 404, body: 'not found', request: { url: 'http://example.com/test' } } } # missing method
111
+
112
+ it { expect(subject.wrapped_exception).to be_nil }
113
+ it { expect(subject.response).to eq(exception) }
114
+ it { expect(subject.message).to eq('the server responded with status 404 for http://example.com/test') }
115
+ end
116
+
117
+ context 'with hash with status and request but missing url in request' do
118
+ let(:exception) { { status: 404, body: 'not found', request: { method: :get } } } # missing url
119
+
120
+ it { expect(subject.wrapped_exception).to be_nil }
121
+ it { expect(subject.response).to eq(exception) }
122
+ it { expect(subject.message).to eq('the server responded with status 404 for GET ') }
123
+ end
124
+
125
+ context 'with properly formed Faraday::Env' do
126
+ # This represents the normal case - a well-formed Faraday::Env object
127
+ # with all the standard properties populated as they would be during
128
+ # a typical HTTP request/response cycle
129
+ let(:exception) { Faraday::Env.new }
130
+
131
+ before do
132
+ exception.status = 500
133
+ exception.method = :post
134
+ exception.url = URI('https://api.example.com/users')
135
+ exception.request = Faraday::RequestOptions.new
136
+ exception.response_headers = { 'content-type' => 'application/json' }
137
+ exception.response_body = '{"error": "Internal server error"}'
138
+ exception.request_headers = { 'authorization' => 'Bearer token123' }
139
+ exception.request_body = '{"name": "John"}'
140
+ end
141
+
142
+ it { expect(subject.wrapped_exception).to be_nil }
143
+ it { expect(subject.response).to eq(exception) }
144
+ it { expect(subject.message).to eq('the server responded with status 500 for POST https://api.example.com/users') }
145
+ end
146
+
147
+ context 'with Faraday::Env missing status key' do
148
+ let(:exception) { Faraday::Env.new }
149
+
150
+ before do
151
+ exception[:body] = 'error body'
152
+ # Intentionally not setting status
153
+ end
154
+
155
+ it { expect(subject.wrapped_exception).to be_nil }
156
+ it { expect(subject.response).to eq(exception) }
157
+ it { expect(subject.message).to eq('the server responded with status for ') }
158
+ end
159
+
160
+ context 'with Faraday::Env with direct method and url properties' do
161
+ let(:exception) { Faraday::Env.new }
162
+
163
+ before do
164
+ exception.status = 404
165
+ exception.method = :get
166
+ exception.url = URI('http://example.com/test')
167
+ exception[:body] = 'not found'
168
+ end
169
+
170
+ it { expect(subject.wrapped_exception).to be_nil }
171
+ it { expect(subject.response).to eq(exception) }
172
+ it { expect(subject.message).to eq('the server responded with status 404 for GET http://example.com/test') }
173
+ end
92
174
  end
93
175
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.13.2
4
+ version: 2.13.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - "@technoweenie"
@@ -144,7 +144,7 @@ licenses:
144
144
  - MIT
145
145
  metadata:
146
146
  homepage_uri: https://lostisland.github.io/faraday
147
- changelog_uri: https://github.com/lostisland/faraday/releases/tag/v2.13.2
147
+ changelog_uri: https://github.com/lostisland/faraday/releases/tag/v2.13.4
148
148
  source_code_uri: https://github.com/lostisland/faraday
149
149
  bug_tracker_uri: https://github.com/lostisland/faraday/issues
150
150
  rubygems_mfa_required: 'true'
@@ -163,7 +163,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
163
163
  - !ruby/object:Gem::Version
164
164
  version: '0'
165
165
  requirements: []
166
- rubygems_version: 3.6.7
166
+ rubygems_version: 3.6.9
167
167
  specification_version: 4
168
168
  summary: HTTP/REST API client library.
169
169
  test_files: []