Commit 3a722ff5 authored by Stan Hu's avatar Stan Hu

Show a more helpful error for import status

Importing a project from GitHub for a project namespace that already exists
would show an unhelpful error, "An error occurred while importing project."
We now add the base message from Projects::CreateService when this fails.

Closes #47365
parent af07c490
...@@ -67,7 +67,15 @@ class ImporterStatus { ...@@ -67,7 +67,15 @@ class ImporterStatus {
false, false,
)); ));
}) })
.catch(() => flash(__('An error occurred while importing project'))); .catch((error) => {
let details = error;
if (error.response && error.response.data && error.response.data.errors) {
details = error.response.data.errors;
}
flash(__(`An error occurred while importing project: ${details}`));
});
} }
autoUpdate() { autoUpdate() {
......
...@@ -25,4 +25,13 @@ class Import::BaseController < ApplicationController ...@@ -25,4 +25,13 @@ class Import::BaseController < ApplicationController
current_user.namespace current_user.namespace
end end
def project_save_error(project)
# Projects::CreateService will set base message if unable to save
if project.errors[:base].present?
project.errors[:base].last
else
project.errors.full_messages.join(', ')
end
end
end end
...@@ -55,7 +55,7 @@ class Import::BitbucketController < Import::BaseController ...@@ -55,7 +55,7 @@ class Import::BitbucketController < Import::BaseController
if project.persisted? if project.persisted?
render json: ProjectSerializer.new.represent(project) render json: ProjectSerializer.new.represent(project)
else else
render json: { errors: project.errors.full_messages }, status: :unprocessable_entity render json: { errors: project_save_error(project) }, status: :unprocessable_entity
end end
else else
render json: { errors: 'This namespace has already been taken! Please choose another one.' }, status: :unprocessable_entity render json: { errors: 'This namespace has already been taken! Please choose another one.' }, status: :unprocessable_entity
......
...@@ -66,7 +66,7 @@ class Import::FogbugzController < Import::BaseController ...@@ -66,7 +66,7 @@ class Import::FogbugzController < Import::BaseController
if project.persisted? if project.persisted?
render json: ProjectSerializer.new.represent(project) render json: ProjectSerializer.new.represent(project)
else else
render json: { errors: project.errors.full_messages }, status: :unprocessable_entity render json: { errors: project_save_error(project) }, status: :unprocessable_entity
end end
end end
......
...@@ -48,7 +48,7 @@ class Import::GithubController < Import::BaseController ...@@ -48,7 +48,7 @@ class Import::GithubController < Import::BaseController
if project.persisted? if project.persisted?
render json: ProjectSerializer.new.represent(project) render json: ProjectSerializer.new.represent(project)
else else
render json: { errors: project.errors.full_messages }, status: :unprocessable_entity render json: { errors: project_save_error(project) }, status: :unprocessable_entity
end end
else else
render json: { errors: 'This namespace has already been taken! Please choose another one.' }, status: :unprocessable_entity render json: { errors: 'This namespace has already been taken! Please choose another one.' }, status: :unprocessable_entity
......
...@@ -32,7 +32,7 @@ class Import::GitlabController < Import::BaseController ...@@ -32,7 +32,7 @@ class Import::GitlabController < Import::BaseController
if project.persisted? if project.persisted?
render json: ProjectSerializer.new.represent(project) render json: ProjectSerializer.new.represent(project)
else else
render json: { errors: project.errors.full_messages }, status: :unprocessable_entity render json: { errors: project_save_error(project) }, status: :unprocessable_entity
end end
else else
render json: { errors: 'This namespace has already been taken! Please choose another one.' }, status: :unprocessable_entity render json: { errors: 'This namespace has already been taken! Please choose another one.' }, status: :unprocessable_entity
......
...@@ -92,7 +92,7 @@ class Import::GoogleCodeController < Import::BaseController ...@@ -92,7 +92,7 @@ class Import::GoogleCodeController < Import::BaseController
if project.persisted? if project.persisted?
render json: ProjectSerializer.new.represent(project) render json: ProjectSerializer.new.represent(project)
else else
render json: { errors: project.errors.full_messages }, status: :unprocessable_entity render json: { errors: project_save_error(project) }, status: :unprocessable_entity
end end
end end
......
---
title: Show a more helpful error for import status
merge_request:
author:
type: other
...@@ -50,6 +50,24 @@ describe('Importer Status', () => { ...@@ -50,6 +50,24 @@ describe('Importer Status', () => {
}) })
.catch(done.fail); .catch(done.fail);
}); });
it('shows error message after failed POST request', (done) => {
appendSetFixtures('<div class="flash-container"></div>');
mock.onPost(importUrl).reply(422, {
errors: 'You forgot your lunch',
});
instance.addToImport({
currentTarget: document.querySelector('.js-add-to-import'),
})
.then(() => {
const flashMessage = document.querySelector('.flash-text');
expect(flashMessage.textContent.trim()).toEqual('An error occurred while importing project: You forgot your lunch');
done();
})
.catch(done.fail);
});
}); });
describe('autoUpdate', () => { describe('autoUpdate', () => {
......
...@@ -118,14 +118,34 @@ shared_examples 'a GitHub-ish import controller: POST create' do ...@@ -118,14 +118,34 @@ shared_examples 'a GitHub-ish import controller: POST create' do
expect(response).to have_gitlab_http_status(200) expect(response).to have_gitlab_http_status(200)
end end
it 'returns 422 response when the project could not be imported' do it 'returns 422 response with the base error when the project could not be imported' do
project = build(:project)
error_message = 'This is an error'
project.errors.add(:base, error_message)
allow(Gitlab::LegacyGithubImport::ProjectCreator)
.to receive(:new).with(provider_repo, provider_repo.name, user.namespace, user, access_params, type: provider)
.and_return(double(execute: project))
post :create, format: :json
expect(response).to have_gitlab_http_status(422)
expect(json_response['errors']).to eq(error_message)
end
it 'returns 422 response with a combined error when the project could not be imported' do
project = build(:project)
project.errors.add(:name, 'is invalid')
project.errors.add(:path, 'is old')
allow(Gitlab::LegacyGithubImport::ProjectCreator) allow(Gitlab::LegacyGithubImport::ProjectCreator)
.to receive(:new).with(provider_repo, provider_repo.name, user.namespace, user, access_params, type: provider) .to receive(:new).with(provider_repo, provider_repo.name, user.namespace, user, access_params, type: provider)
.and_return(double(execute: build(:project))) .and_return(double(execute: project))
post :create, format: :json post :create, format: :json
expect(response).to have_gitlab_http_status(422) expect(response).to have_gitlab_http_status(422)
expect(json_response['errors']).to eq('Name is invalid, Path is old')
end end
context "when the repository owner is the provider user" do context "when the repository owner is the provider user" do
......
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