Commit fd5f0c69 authored by Alexandru Croitor's avatar Alexandru Croitor

Return error when Jira REST request fails

When Jira REST request to fetch projects fails, exit early and
return the corresponding error.
parent ac9e467e
......@@ -16,7 +16,11 @@ module Resolvers
response, start_cursor, end_cursor = jira_projects(name: name, **compute_pagination_params(args))
end_cursor = nil if !!response.payload[:is_last]
response.success? ? Gitlab::Graphql::ExternallyPaginatedArray.new(start_cursor, end_cursor, *response.payload[:projects]) : nil
if response.success?
Gitlab::Graphql::ExternallyPaginatedArray.new(start_cursor, end_cursor, *response.payload[:projects])
else
raise Gitlab::Graphql::Errors::BaseError, response.message
end
end
def authorized_resource?(project)
......@@ -58,6 +62,9 @@ module Resolvers
args = { query: name, start_at: start_at, limit: limit }.compact
response = Jira::Requests::Projects.new(project.jira_service, args).execute
return [response, nil, nil] if response.error?
projects = response.payload[:projects]
start_cursor = start_at == 0 ? nil : Base64.encode64((start_at - 1).to_s)
end_cursor = Base64.encode64((start_at + projects.size - 1).to_s)
......
......@@ -35,7 +35,7 @@ module Jira
response = client.get(url)
build_service_response(response)
rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, Errno::ECONNREFUSED, URI::InvalidURIError, JIRA::HTTPError, OpenSSL::SSL::SSLError => error
error_message = error.message
error_message = "Jira request error: #{error.message}"
log_error("Error sending message", client_url: client.options[:site], error: error_message)
ServiceResponse.error(message: error_message)
end
......
......@@ -41,22 +41,35 @@ describe Resolvers::Projects::JiraProjectsResolver do
end
context 'when user is a maintainer' do
include_context 'jira projects request context'
before do
project.add_maintainer(user)
end
it 'returns jira projects' do
jira_projects = resolve_jira_projects
project_keys = jira_projects.map(&:key)
project_names = jira_projects.map(&:name)
project_ids = jira_projects.map(&:id)
context 'when Jira connection is valid' do
include_context 'jira projects request context'
it 'returns jira projects' do
jira_projects = resolve_jira_projects
project_keys = jira_projects.map(&:key)
project_names = jira_projects.map(&:name)
project_ids = jira_projects.map(&:id)
expect(jira_projects.size).to eq 2
expect(project_keys).to eq(%w(EX ABC))
expect(project_names).to eq(%w(Example Alphabetical))
expect(project_ids).to eq(%w(10000 10001))
end
end
context 'when Jira connection is not valid' do
before do
WebMock.stub_request(:get, 'https://jira.example.com/rest/api/2/project/search?maxResults=50&query=&startAt=0')
.to_raise(JIRA::HTTPError.new(double(message: 'Some failure.')))
end
expect(jira_projects.size).to eq 2
expect(project_keys).to eq(%w(EX ABC))
expect(project_names).to eq(%w(Example Alphabetical))
expect(project_ids).to eq(%w(10000 10001))
it 'raises failure error' do
expect { resolve_jira_projects }.to raise_error('Jira request error: Some failure.')
end
end
end
end
......
......@@ -54,7 +54,7 @@ describe Jira::Requests::Projects do
it 'returns an error response' do
expect(subject.error?).to be_truthy
expect(subject.message).to eq('Timeout::Error')
expect(subject.message).to eq('Jira request error: Timeout::Error')
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