Commit 1f4ce96d authored by Nick Thomas's avatar Nick Thomas

Merge branch 'wc-catch-cmderr-branch' into 'master'

Catch Git::CommandError in Branches::CreateService

See merge request gitlab-org/gitlab!64727
parents d2ebc02b 727a0716
...@@ -9,12 +9,16 @@ module Branches ...@@ -9,12 +9,16 @@ module Branches
return result if result[:status] == :error return result if result[:status] == :error
begin
new_branch = repository.add_branch(current_user, branch_name, ref) new_branch = repository.add_branch(current_user, branch_name, ref)
rescue Gitlab::Git::CommandError => e
return error("Failed to create branch '#{branch_name}': #{e}")
end
if new_branch if new_branch
success(new_branch) success(new_branch)
else else
error("Invalid reference name: #{ref}") error("Failed to create branch '#{branch_name}': invalid reference name '#{ref}'")
end end
rescue Gitlab::Git::PreReceiveError => e rescue Gitlab::Git::PreReceiveError => e
Gitlab::ErrorTracking.track_exception(e, pre_receive_message: e.raw_message, branch_name: branch_name, ref: ref) Gitlab::ErrorTracking.track_exception(e, pre_receive_message: e.raw_message, branch_name: branch_name, ref: ref)
......
...@@ -727,10 +727,11 @@ RSpec.describe API::Branches do ...@@ -727,10 +727,11 @@ RSpec.describe API::Branches do
end end
it 'returns 400 if ref name is invalid' do it 'returns 400 if ref name is invalid' do
error_message = 'Failed to create branch \'new_design3\': invalid reference name \'foo\''
post api(route, user), params: { branch: 'new_design3', ref: 'foo' } post api(route, user), params: { branch: 'new_design3', ref: 'foo' }
expect(response).to have_gitlab_http_status(:bad_request) expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response['message']).to eq('Invalid reference name: foo') expect(json_response['message']).to eq(error_message)
end end
end end
......
...@@ -35,11 +35,12 @@ RSpec.describe 'Creation of a new branch' do ...@@ -35,11 +35,12 @@ RSpec.describe 'Creation of a new branch' do
end end
context 'when ref is not correct' do context 'when ref is not correct' do
err_msg = 'Failed to create branch \'another_branch\': invalid reference name \'unknown\''
let(:new_branch) { 'another_branch' } let(:new_branch) { 'another_branch' }
let(:ref) { 'unknown' } let(:ref) { 'unknown' }
it_behaves_like 'a mutation that returns errors in the response', it_behaves_like 'a mutation that returns errors in the response',
errors: ['Invalid reference name: unknown'] errors: [err_msg]
end end
end end
end end
...@@ -38,10 +38,23 @@ RSpec.describe Branches::CreateService do ...@@ -38,10 +38,23 @@ RSpec.describe Branches::CreateService do
end end
it 'returns an error with a reference name' do it 'returns an error with a reference name' do
err_msg = 'Failed to create branch \'new-feature\': invalid reference name \'unknown\''
result = service.execute('new-feature', 'unknown') result = service.execute('new-feature', 'unknown')
expect(result[:status]).to eq(:error) expect(result[:status]).to eq(:error)
expect(result[:message]).to eq('Invalid reference name: unknown') expect(result[:message]).to eq(err_msg)
end
end
context 'when an ambiguous branch name is provided' do
it 'returns an error that branch could not be created' do
err_msg = 'Failed to create branch \'feature\': 13:reference is ambiguous.'
service.execute('feature/widget', 'master')
result = service.execute('feature', 'master')
expect(result[:status]).to eq(:error)
expect(result[:message]).to eq(err_msg)
end end
end end
......
...@@ -87,7 +87,7 @@ RSpec.describe Commits::CommitPatchService do ...@@ -87,7 +87,7 @@ RSpec.describe Commits::CommitPatchService do
context 'when specifying a non existent start branch' do context 'when specifying a non existent start branch' do
let(:start_branch) { 'does-not-exist' } let(:start_branch) { 'does-not-exist' }
it_behaves_like 'an error response', 'Invalid reference name' it_behaves_like 'an error response', 'Failed to create branch'
end end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment