Commit ebe9cdfc authored by Vasilii Iakliushin's avatar Vasilii Iakliushin

Graceful degradation for refs endpoint

**Problem**

We have an unhandled exception when Gitaly is not available.

**Solution**

Return 503 error with an error message.

Changelog: added
parent 39da3930
...@@ -305,12 +305,7 @@ class ProjectsController < Projects::ApplicationController ...@@ -305,12 +305,7 @@ class ProjectsController < Projects::ApplicationController
end end
if find_tags && @repository.tag_count.nonzero? if find_tags && @repository.tag_count.nonzero?
tags = begin tags = TagsFinder.new(@repository, refs_params).execute
TagsFinder.new(@repository, refs_params).execute
rescue Gitlab::Git::CommandError
[]
end
options['Tags'] = tags.take(100).map(&:name) options['Tags'] = tags.take(100).map(&:name)
end end
...@@ -321,6 +316,8 @@ class ProjectsController < Projects::ApplicationController ...@@ -321,6 +316,8 @@ class ProjectsController < Projects::ApplicationController
end end
render json: options.to_json render json: options.to_json
rescue Gitlab::Git::CommandError
render json: { error: _('Unable to load refs') }, status: :service_unavailable
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
......
...@@ -39636,6 +39636,9 @@ msgstr "" ...@@ -39636,6 +39636,9 @@ msgstr ""
msgid "Unable to load file contents. Try again later." msgid "Unable to load file contents. Try again later."
msgstr "" msgstr ""
msgid "Unable to load refs"
msgstr ""
msgid "Unable to load the diff" msgid "Unable to load the diff"
msgstr "" msgstr ""
......
...@@ -1159,16 +1159,15 @@ RSpec.describe ProjectsController do ...@@ -1159,16 +1159,15 @@ RSpec.describe ProjectsController do
context 'when gitaly is unavailable' do context 'when gitaly is unavailable' do
before do before do
expect_next_instance_of(TagsFinder) do |finder| expect_next_instance_of(TagsFinder) do |finder|
allow(finder).to receive(:execute).and_raise(Gitlab::Git::CommandError) allow(finder).to receive(:execute).and_raise(Gitlab::Git::CommandError, 'something went wrong')
end end
end end
it 'gets an empty list of tags' do it 'responds with 503 error' do
get :refs, params: { namespace_id: project.namespace, id: project, ref: "123456" } get :refs, params: { namespace_id: project.namespace, id: project, ref: "123456" }
expect(json_response["Branches"]).to include("master") expect(response).to have_gitlab_http_status(:service_unavailable)
expect(json_response["Tags"]).to eq([]) expect(json_response['error']).to eq 'Unable to load refs'
expect(json_response["Commits"]).to include("123456")
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