diff --git a/app/services/jira/requests/base.rb b/app/services/jira/requests/base.rb index 8d7b460bf69ad6d2150be51fd95c583fa8524cb1..5197e6d47c343c7b8263c795e64f15006bd5ee2f 100644 --- a/app/services/jira/requests/base.rb +++ b/app/services/jira/requests/base.rb @@ -113,9 +113,7 @@ def reportable_jira_ruby_error_message(error) when 'Forbidden' s_('JiraRequest|The credentials for accessing Jira are not allowed to access the data. Check your %{docs_link_start}Jira integration credentials%{docs_link_end} and try again.').html_safe % { docs_link_start: auth_docs_link_start, docs_link_end: '</a>'.html_safe } when 'Bad Request' - s_('JiraRequest|An error occurred while requesting data from Jira. Check your %{docs_link_start}Jira integration configuration%{docs_link_end} and try again.').html_safe % { docs_link_start: config_docs_link_start, docs_link_end: '</a>'.html_safe } - when /errorMessages/ - jira_ruby_json_error_message(error.message) + jira_ruby_json_error_message(error.response.body) || s_('JiraRequest|An error occurred while requesting data from Jira. Check your %{docs_link_start}Jira integration configuration%{docs_link_end} and try again.').html_safe % { docs_link_start: config_docs_link_start, docs_link_end: '</a>'.html_safe } end end @@ -127,7 +125,7 @@ def jira_ruby_json_error_message(error_message) messages = Rails::Html::FullSanitizer.new.sanitize(messages).presence return unless messages - s_('JiraRequest|An error occurred while requesting data from Jira: %{messages}. Check your %{docs_link_start}Jira integration configuration%{docs_link_end} and try again.').html_safe % { messages: messages, docs_link_start: config_docs_link_start, docs_link_end: '</a>'.html_safe } + s_('JiraRequest|An error occurred while requesting data from Jira: %{messages} Check your %{docs_link_start}Jira integration configuration%{docs_link_end} and try again.').html_safe % { messages: messages, docs_link_start: config_docs_link_start, docs_link_end: '</a>'.html_safe } rescue JSON::ParserError end end diff --git a/locale/gitlab.pot b/locale/gitlab.pot index 26f57a1e9d1f5daa678e716f83ea52812dc3ca15..80e78be2d0301d6d0e52888fa5637ea7529ec35c 100644 --- a/locale/gitlab.pot +++ b/locale/gitlab.pot @@ -28752,7 +28752,7 @@ msgstr "" msgid "JiraRequest|An error occurred while requesting data from Jira. Check your %{docs_link_start}Jira integration configuration%{docs_link_end} and try again." msgstr "" -msgid "JiraRequest|An error occurred while requesting data from Jira: %{messages}. Check your %{docs_link_start}Jira integration configuration%{docs_link_end} and try again." +msgid "JiraRequest|An error occurred while requesting data from Jira: %{messages} Check your %{docs_link_start}Jira integration configuration%{docs_link_end} and try again." msgstr "" msgid "JiraRequest|The Jira API URL for connecting to Jira is not valid. Check your Jira integration API URL and try again." diff --git a/spec/graphql/resolvers/projects/jira_projects_resolver_spec.rb b/spec/graphql/resolvers/projects/jira_projects_resolver_spec.rb index b2d5e0b74055f46397957997e3fdce235c9f772b..a01545e2b14fd124634f613db9806a421b5d43a8 100644 --- a/spec/graphql/resolvers/projects/jira_projects_resolver_spec.rb +++ b/spec/graphql/resolvers/projects/jira_projects_resolver_spec.rb @@ -86,7 +86,7 @@ context 'when Jira connection is not valid' do before do WebMock.stub_request(:get, 'https://jira.example.com/rest/api/2/project') - .to_raise(JIRA::HTTPError.new(double(message: '{"errorMessages":["Some failure"]}'))) + .to_raise(JIRA::HTTPError.new(double(message: 'Bad Request', body: '{"errorMessages":["Some failure."]}'))) end it 'generates a failure error' do diff --git a/spec/support/shared_examples/services/jira/requests/base_shared_examples.rb b/spec/support/shared_examples/services/jira/requests/base_shared_examples.rb index a51215f6c8951659f6c9e2bf62f5e59ce11df953..379a94dd4d799b8523fb1c6dd1ceaabb2b68c0e2 100644 --- a/spec/support/shared_examples/services/jira/requests/base_shared_examples.rb +++ b/spec/support/shared_examples/services/jira/requests/base_shared_examples.rb @@ -4,26 +4,26 @@ include AfterNextHelpers using RSpec::Parameterized::TableSyntax - where(:exception_class, :exception_message, :expected_message) do - Errno::ECONNRESET | '' | 'A connection error occurred' - Errno::ECONNREFUSED | '' | 'A connection error occurred' - Errno::ETIMEDOUT | '' | 'A timeout error occurred' - Timeout::Error | '' | 'A timeout error occurred' - URI::InvalidURIError | '' | 'The Jira API URL' - SocketError | '' | 'The Jira API URL' - Gitlab::HTTP::BlockedUrlError | '' | 'Unable to connect to the Jira URL. Please verify your' - OpenSSL::SSL::SSLError | 'foo' | 'An SSL error occurred while connecting to Jira: foo' - JIRA::HTTPError | 'Unauthorized' | 'The credentials for accessing Jira are not valid' - JIRA::HTTPError | 'Forbidden' | 'The credentials for accessing Jira are not allowed' - JIRA::HTTPError | 'Bad Request' | 'An error occurred while requesting data from Jira' - JIRA::HTTPError | 'Foo' | 'An error occurred while requesting data from Jira.' - JIRA::HTTPError | '{"errorMessages":["foo","bar"]}' | 'An error occurred while requesting data from Jira: foo and bar' - JIRA::HTTPError | '{"errorMessages":[""]}' | 'An error occurred while requesting data from Jira.' + where(:exception_class, :exception_message, :exception_body, :expected_message) do + Errno::ECONNRESET | '' | '' | 'A connection error occurred' + Errno::ECONNREFUSED | '' | '' | 'A connection error occurred' + Errno::ETIMEDOUT | '' | '' | 'A timeout error occurred' + Timeout::Error | '' | '' | 'A timeout error occurred' + URI::InvalidURIError | '' | '' | 'The Jira API URL' + SocketError | '' | '' | 'The Jira API URL' + Gitlab::HTTP::BlockedUrlError | '' | '' | 'Unable to connect to the Jira URL. Please verify your' + OpenSSL::SSL::SSLError | 'foo' | '' | 'An SSL error occurred while connecting to Jira: foo' + JIRA::HTTPError | 'Unauthorized' | '' | 'The credentials for accessing Jira are not valid' + JIRA::HTTPError | 'Forbidden' | '' | 'The credentials for accessing Jira are not allowed' + JIRA::HTTPError | 'Bad Request' | '' | 'An error occurred while requesting data from Jira' + JIRA::HTTPError | 'Bad Request' | 'Foo' | 'An error occurred while requesting data from Jira.' + JIRA::HTTPError | 'Bad Request' | '{"errorMessages":["foo","bar"]}' | 'An error occurred while requesting data from Jira: foo and bar' + JIRA::HTTPError | 'Bad Request' | '{"errorMessages":[""]}' | 'An error occurred while requesting data from Jira.' end with_them do it 'handles the error' do - stub_client_and_raise(exception_class, exception_message) + stub_client_and_raise(exception_class, exception_message, exception_body) expect(subject).to be_a(ServiceResponse) expect(subject).to be_error @@ -36,11 +36,11 @@ let(:docs_link_start) { '<a href="%{url}" target="_blank" rel="noopener noreferrer">'.html_safe % { url: config_docs_link_url } } before do - stub_client_and_raise(JIRA::HTTPError, error) + stub_client_and_raise(JIRA::HTTPError, 'Bad Request', body) end - context 'when JSON is malformed' do - let(:error) { '{"errorMessages":' } + context 'when JSON body is malformed' do + let(:body) { '{"errorMessages":' } it 'returns the default error message' do error_message = 'An error occurred while requesting data from Jira. Check your %{docs_link_start}Jira integration configuration</a> and try again.' % { docs_link_start: docs_link_start } @@ -49,10 +49,10 @@ end context 'when JSON contains tags' do - let(:error) { '{"errorMessages":["<script>alert(true)</script>foo"]}' } + let(:body) { '{"errorMessages":["<script>alert(true)</script>foo"]}' } it 'sanitizes it' do - error_message = 'An error occurred while requesting data from Jira: foo. Check your %{docs_link_start}Jira integration configuration</a> and try again.' % { docs_link_start: docs_link_start } + error_message = 'An error occurred while requesting data from Jira: foo Check your %{docs_link_start}Jira integration configuration</a> and try again.' % { docs_link_start: docs_link_start } expect(subject.message).to eq(error_message) end end @@ -76,9 +76,9 @@ expect(subject).to be_error end - def stub_client_and_raise(exception_class, message = '') + def stub_client_and_raise(exception_class, message = '', exception_body = nil) # `JIRA::HTTPError` classes take a response from the JIRA API, rather than a `String`. - message = double(body: message) if exception_class == JIRA::HTTPError + message = double(message: message, body: exception_body) if exception_class == JIRA::HTTPError allow_next(JIRA::Client).to receive(:get).and_raise(exception_class, message) end