Commit fd8bb846 authored by Mayra Cabrera's avatar Mayra Cabrera

Merge branch 'exit-early-when-jira-request-fails' into 'master'

Return error when Jira REST request fails

See merge request gitlab-org/gitlab!33688
parents 57f33ff7 fd5f0c69
......@@ -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,12 +41,13 @@ 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
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)
......@@ -59,6 +60,18 @@ describe Resolvers::Projects::JiraProjectsResolver do
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
it 'raises failure error' do
expect { resolve_jira_projects }.to raise_error('Jira request error: Some failure.')
end
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