Skip to content

Commit bd1dc92

Browse files
committed
Update rescue logic
1 parent 218e3a4 commit bd1dc92

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

lib/cypress_on_rails/vcr/insert_eject_middleware.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def handle_insert(req)
3434
vcr.insert_cassette(cassette_name, options)
3535
[201, { 'Content-Type' => 'application/json' }, [{ 'message': 'OK' }.to_json]]
3636
rescue LoadError, ArgumentError => e
37-
[501, { 'Content-Type' => 'application/json' }, [{ 'message': e.message }.to_json]]
37+
[500, { 'Content-Type' => 'application/json' }, [{ 'message': e.message }.to_json]]
3838
end
3939

4040
def parse_request_body(req)
@@ -57,7 +57,7 @@ def handle_eject
5757
do_first_call
5858
[201, { 'Content-Type' => 'application/json' }, [{ 'message': 'OK' }.to_json]]
5959
rescue LoadError, ArgumentError => e
60-
[501, { 'Content-Type' => 'application/json' }, [{ 'message': e.message }.to_json]]
60+
[500, { 'Content-Type' => 'application/json' }, [{ 'message': e.message }.to_json]]
6161
end
6262

6363
def do_first_call

spec/cypress_on_rails/vcr/insert_eject_middleware_spec.rb

+46
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,30 @@ def rack_input(json_value)
7777
expect(vcr).to have_received(:insert_cassette).with('cas1', persist_with: :file_system)
7878
end
7979
end
80+
81+
context 'when an error occurs' do
82+
it 'returns a 500 error with the error message' do
83+
env['rack.input'] = rack_input(['cas1'])
84+
allow(vcr).to receive(:insert_cassette).and_raise(ArgumentError.new('Invalid cassette name'))
85+
86+
expect(response).to eq([
87+
500,
88+
{ 'Content-Type' => 'application/json' },
89+
['{"message":"Invalid cassette name"}']
90+
])
91+
end
92+
93+
it 'returns a 500 error when LoadError occurs' do
94+
env['rack.input'] = rack_input(['cas1'])
95+
allow(vcr).to receive(:insert_cassette).and_raise(LoadError.new('Cannot load VCR'))
96+
97+
expect(response).to eq([
98+
500,
99+
{ 'Content-Type' => 'application/json' },
100+
['{"message":"Cannot load VCR"}']
101+
])
102+
end
103+
end
80104
end
81105

82106
describe '/__e2e__/vcr/eject' do
@@ -93,6 +117,28 @@ def rack_input(json_value)
93117
expect(vcr).to have_received(:eject_cassette)
94118
end
95119
end
120+
121+
context 'when an error occurs' do
122+
it 'returns a 500 error with the error message' do
123+
allow(vcr).to receive(:eject_cassette).and_raise(ArgumentError.new('No cassette to eject'))
124+
125+
expect(response).to eq([
126+
500,
127+
{ 'Content-Type' => 'application/json' },
128+
['{"message":"No cassette to eject"}']
129+
])
130+
end
131+
132+
it 'returns a 500 error when LoadError occurs' do
133+
allow(vcr).to receive(:eject_cassette).and_raise(LoadError.new('Cannot load VCR'))
134+
135+
expect(response).to eq([
136+
500,
137+
{ 'Content-Type' => 'application/json' },
138+
['{"message":"Cannot load VCR"}']
139+
])
140+
end
141+
end
96142
end
97143

98144
describe '"Other paths"' do

0 commit comments

Comments
 (0)