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 ...@@ -16,7 +16,11 @@ module Resolvers
response, start_cursor, end_cursor = jira_projects(name: name, **compute_pagination_params(args)) response, start_cursor, end_cursor = jira_projects(name: name, **compute_pagination_params(args))
end_cursor = nil if !!response.payload[:is_last] 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 end
def authorized_resource?(project) def authorized_resource?(project)
...@@ -58,6 +62,9 @@ module Resolvers ...@@ -58,6 +62,9 @@ module Resolvers
args = { query: name, start_at: start_at, limit: limit }.compact args = { query: name, start_at: start_at, limit: limit }.compact
response = Jira::Requests::Projects.new(project.jira_service, args).execute response = Jira::Requests::Projects.new(project.jira_service, args).execute
return [response, nil, nil] if response.error?
projects = response.payload[:projects] projects = response.payload[:projects]
start_cursor = start_at == 0 ? nil : Base64.encode64((start_at - 1).to_s) start_cursor = start_at == 0 ? nil : Base64.encode64((start_at - 1).to_s)
end_cursor = Base64.encode64((start_at + projects.size - 1).to_s) end_cursor = Base64.encode64((start_at + projects.size - 1).to_s)
......
...@@ -35,7 +35,7 @@ module Jira ...@@ -35,7 +35,7 @@ module Jira
response = client.get(url) response = client.get(url)
build_service_response(response) build_service_response(response)
rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, Errno::ECONNREFUSED, URI::InvalidURIError, JIRA::HTTPError, OpenSSL::SSL::SSLError => error 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) log_error("Error sending message", client_url: client.options[:site], error: error_message)
ServiceResponse.error(message: error_message) ServiceResponse.error(message: error_message)
end end
......
...@@ -41,22 +41,35 @@ describe Resolvers::Projects::JiraProjectsResolver do ...@@ -41,22 +41,35 @@ describe Resolvers::Projects::JiraProjectsResolver do
end end
context 'when user is a maintainer' do context 'when user is a maintainer' do
include_context 'jira projects request context'
before do before do
project.add_maintainer(user) project.add_maintainer(user)
end end
it 'returns jira projects' do context 'when Jira connection is valid' do
jira_projects = resolve_jira_projects include_context 'jira projects request context'
project_keys = jira_projects.map(&:key)
project_names = jira_projects.map(&:name) it 'returns jira projects' do
project_ids = jira_projects.map(&:id) 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 it 'raises failure error' do
expect(project_keys).to eq(%w(EX ABC)) expect { resolve_jira_projects }.to raise_error('Jira request error: Some failure.')
expect(project_names).to eq(%w(Example Alphabetical)) end
expect(project_ids).to eq(%w(10000 10001))
end end
end end
end end
......
...@@ -54,7 +54,7 @@ describe Jira::Requests::Projects do ...@@ -54,7 +54,7 @@ describe Jira::Requests::Projects do
it 'returns an error response' do it 'returns an error response' do
expect(subject.error?).to be_truthy 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
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