Update service to handle unexpected exceptions
This will ensure that now and in the future, PushOptionsHandlerService will not cause the post_receive API endpoint from running other code if something causes an unknown exception.
This commit is contained in:
parent
3c40c98e26
commit
b5bcf80c9a
2 changed files with 29 additions and 0 deletions
|
@ -24,6 +24,9 @@ module MergeRequests
|
||||||
execute_for_branch(branch)
|
execute_for_branch(branch)
|
||||||
rescue Gitlab::Access::AccessDeniedError
|
rescue Gitlab::Access::AccessDeniedError
|
||||||
errors << 'User access was denied'
|
errors << 'User access was denied'
|
||||||
|
rescue StandardError => e
|
||||||
|
Gitlab::AppLogger.error(e)
|
||||||
|
errors << 'An unknown error occurred'
|
||||||
end
|
end
|
||||||
|
|
||||||
self
|
self
|
||||||
|
|
|
@ -334,6 +334,32 @@ describe MergeRequests::PushOptionsHandlerService do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'handling unexpected exceptions' do
|
||||||
|
let(:push_options) { { create: true } }
|
||||||
|
let(:changes) { new_branch_changes }
|
||||||
|
let(:exception) { StandardError.new('My standard error') }
|
||||||
|
|
||||||
|
def run_service_with_exception
|
||||||
|
allow_any_instance_of(
|
||||||
|
MergeRequests::BuildService
|
||||||
|
).to receive(:execute).and_raise(exception)
|
||||||
|
|
||||||
|
service.execute
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'records an error' do
|
||||||
|
run_service_with_exception
|
||||||
|
|
||||||
|
expect(service.errors).to eq(['An unknown error occurred'])
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'writes to Gitlab::AppLogger' do
|
||||||
|
expect(Gitlab::AppLogger).to receive(:error).with(exception)
|
||||||
|
|
||||||
|
run_service_with_exception
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe 'when target is not a valid branch name' do
|
describe 'when target is not a valid branch name' do
|
||||||
let(:push_options) { { create: true, target: 'my-branch' } }
|
let(:push_options) { { create: true, target: 'my-branch' } }
|
||||||
let(:changes) { new_branch_changes }
|
let(:changes) { new_branch_changes }
|
||||||
|
|
Loading…
Reference in a new issue