faraday 2.13.3 → 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 +4 -4
- data/lib/faraday/error.rb +36 -15
- data/lib/faraday/version.rb +1 -1
- data/spec/faraday/error_spec.rb +82 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 12dedb476a26ae00e5f902e4e10f5327a790290d2d7647711aac09e8661af959
|
4
|
+
data.tar.gz: f43c7bb1127b0a57bb7aa0727a096e0b66cfeead93445631dacf151c354efbdb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
82
|
+
case exc
|
83
|
+
when Exception
|
83
84
|
[exc, exc.message, response]
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
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.
|
data/lib/faraday/version.rb
CHANGED
data/spec/faraday/error_spec.rb
CHANGED
@@ -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.
|
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.
|
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'
|