/
error_spec.rb
122 lines (103 loc) · 4.27 KB
/
error_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# frozen_string_literal: true
RSpec.describe Faraday::ClientError do
describe '.initialize' do
subject { described_class.new(exception, response) }
let(:response) { nil }
context 'with exception only' do
let(:exception) { RuntimeError.new('test') }
it { expect(subject.wrapped_exception).to eq(exception) }
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('#<Faraday::ClientError wrapped=#<RuntimeError: test>>') }
end
context 'with response hash' do
let(:exception) { { status: 400 } }
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('#<Faraday::ClientError response={:status=>400}>') }
end
context 'with string' do
let(:exception) { 'custom message' }
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('#<Faraday::ClientError #<Faraday::ClientError: custom message>>') }
end
context 'with anything else #to_s' do
let(:exception) { %w[error1 error2] }
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('#<Faraday::ClientError #<Faraday::ClientError: ["error1", "error2"]>>') }
end
context 'maintains backward-compatibility until 1.0' do
it 'does not raise an error for error-namespaced classes but prints an error message' do
error_message, error = with_warn_squelching { Faraday::Error::ClientError.new('foo') }
expect(error).to be_a Faraday::ClientError
expect(error_message).to match(
Regexp.new(
'NOTE: Faraday::Error::ClientError.new is deprecated; '\
'use Faraday::ClientError.new instead. It will be removed in or after version 1.0'
)
)
end
it 'does not raise an error for inherited error-namespaced classes but prints an error message' do
error_message, = with_warn_squelching { Class.new(Faraday::Error::ClientError) }
expect(error_message).to match(
Regexp.new(
'NOTE: Inheriting Faraday::Error::ClientError is deprecated; '\
'use Faraday::ClientError instead. It will be removed in or after version 1.0'
)
)
end
it 'allows backward-compatible class to be subclassed' do
expect {
with_warn_squelching { Class.new(Faraday::Error::ClientError) }
}.not_to raise_error
end
it 'allows rescuing of a current error with a deprecated error' do
begin
raise Faraday::ClientError, nil
rescue Faraday::Error::ClientError
rescue Faraday::Error => exc
fail "rescued #{exc.class.name} instead"
end
end
it 'allows rescuing of a current error with a current error' do
begin
raise Faraday::ClientError, nil
rescue Faraday::ClientError
rescue Faraday::Error => exc
fail "rescued #{exc.class.name} instead"
end
end
it 'allows rescuing of a deprecated error with a deprecated error' do
begin
raise Faraday::Error::ClientError, nil
rescue Faraday::Error::ClientError
rescue Faraday::Error => exc
fail "rescued #{exc.class.name} instead"
end
end
it 'allows rescuing of a deprecated error with a current error' do
begin
raise Faraday::Error::ClientError, nil
rescue Faraday::ClientError
rescue Faraday::Error => exc
fail "rescued #{exc.class.name} instead"
end
end
end
def with_warn_squelching
stderr_catcher = StringIO.new
original_stderr = $stderr
$stderr = stderr_catcher
result = yield if block_given?
[stderr_catcher.tap(&:rewind).string, result]
ensure
$stderr = original_stderr
end
end
end