Handle Faraday errors

This commit is contained in:
Felix Auringer
2021-06-14 10:53:46 +02:00
committed by Sebastian Serth
parent b48b45de9f
commit 1d3f0d7ad8
4 changed files with 27 additions and 1 deletions

View File

@ -51,6 +51,9 @@ class ExecutionEnvironment < ApplicationRecord
Rails.logger.warn("Could not create execution environment in Poseidon, got response: #{response.as_json}") Rails.logger.warn("Could not create execution environment in Poseidon, got response: #{response.as_json}")
false false
rescue Faraday::Error => e
Rails.logger.warn("Could not create execution environment because of Faraday error: #{e.inspect}")
false
end end
def to_json(*_args) def to_json(*_args)

View File

@ -197,7 +197,7 @@ class Submission < ApplicationRecord
runner = Runner.for(user, exercise) runner = Runner.for(user, exercise)
copy_files_to runner copy_files_to runner
waiting_duration = Time.zone.now - request_time waiting_duration = Time.zone.now - request_time
yield(runner, waiting_duration) if block_given? yield(runner, waiting_duration)
end end
def command_for(template, file) def command_for(template, file)

View File

@ -100,6 +100,20 @@ describe Runner::Strategy::Poseidon do
end end
end end
# All requests handle a Faraday error the same way.
shared_examples 'Faraday error handling' do
context 'when Faraday throws an error' do
# The response status is not needed in this context but the describes block this context is embedded
# into expect this variable to be set in order to properly stub requests to the runner management.
let(:response_status) { -1 }
it 'raises an error' do
%i[post patch delete].each {|message| allow(Faraday).to receive(message).and_raise(Faraday::TimeoutError) }
expect { action.call }.to raise_error(Runner::Error::Unknown, /Faraday/)
end
end
end
describe '::request_from_management' do describe '::request_from_management' do
let(:action) { -> { described_class.request_from_management(execution_environment) } } let(:action) { -> { described_class.request_from_management(execution_environment) } }
let!(:request_runner_stub) do let!(:request_runner_stub) do
@ -158,6 +172,7 @@ describe Runner::Strategy::Poseidon do
include_examples 'InternalServerError (500) error handling' include_examples 'InternalServerError (500) error handling'
include_examples 'unknown response status error handling' include_examples 'unknown response status error handling'
include_examples 'Faraday error handling'
end end
describe '#execute_command' do describe '#execute_command' do
@ -213,6 +228,7 @@ describe Runner::Strategy::Poseidon do
include_examples 'NotFound (404) error handling' include_examples 'NotFound (404) error handling'
include_examples 'InternalServerError (500) error handling' include_examples 'InternalServerError (500) error handling'
include_examples 'unknown response status error handling' include_examples 'unknown response status error handling'
include_examples 'Faraday error handling'
end end
describe '#destroy_at_management' do describe '#destroy_at_management' do
@ -236,6 +252,7 @@ describe Runner::Strategy::Poseidon do
include_examples 'NotFound (404) error handling' include_examples 'NotFound (404) error handling'
include_examples 'InternalServerError (500) error handling' include_examples 'InternalServerError (500) error handling'
include_examples 'unknown response status error handling' include_examples 'unknown response status error handling'
include_examples 'Faraday error handling'
end end
describe '#copy_files' do describe '#copy_files' do
@ -268,6 +285,7 @@ describe Runner::Strategy::Poseidon do
include_examples 'NotFound (404) error handling' include_examples 'NotFound (404) error handling'
include_examples 'InternalServerError (500) error handling' include_examples 'InternalServerError (500) error handling'
include_examples 'unknown response status error handling' include_examples 'unknown response status error handling'
include_examples 'Faraday error handling'
end end
describe '#attach_to_execution' do describe '#attach_to_execution' do

View File

@ -227,5 +227,10 @@ describe ExecutionEnvironment do
[400, 500].each do |status| [400, 500].each do |status|
include_examples 'returns false when the api request failed', status include_examples 'returns false when the api request failed', status
end end
it 'returns false if Faraday raises an error' do
allow(Faraday).to receive(:put).and_raise(Faraday::TimeoutError)
expect(execution_environment.copy_to_poseidon).to be_falsey
end
end end
end end