Commit 7977a20b authored by Grzegorz Bizon's avatar Grzegorz Bizon

Extend error message in case of HTTP errors in `include`

parent d9780bc0
...@@ -17,9 +17,7 @@ module Gitlab ...@@ -17,9 +17,7 @@ module Gitlab
@opts = opts @opts = opts
@errors = [] @errors = []
validate_location! validate!
validate_content!
validate_hash!
end end
def invalid_extension? def invalid_extension?
...@@ -46,6 +44,12 @@ module Gitlab ...@@ -46,6 +44,12 @@ module Gitlab
protected protected
def validate!
validate_location!
validate_content! if errors.none?
validate_hash! if errors.none?
end
def validate_location! def validate_location!
if invalid_extension? if invalid_extension?
errors.push("Included file `#{location}` does not have YAML extension!") errors.push("Included file `#{location}` does not have YAML extension!")
...@@ -53,13 +57,13 @@ module Gitlab ...@@ -53,13 +57,13 @@ module Gitlab
end end
def validate_content! def validate_content!
if errors.none? && content.blank? if content.blank?
errors.push("Included file `#{location}` is empty or does not exist!") errors.push("Included file `#{location}` is empty or does not exist!")
end end
end end
def validate_hash! def validate_hash!
if errors.none? && to_hash.blank? if to_hash.blank?
errors.push("Included file `#{location}` does not have valid YAML syntax!") errors.push("Included file `#{location}` does not have valid YAML syntax!")
end end
end end
......
...@@ -24,8 +24,6 @@ module Gitlab ...@@ -24,8 +24,6 @@ module Gitlab
private private
def validate_content! def validate_content!
return if errors.any?
if content.nil? if content.nil?
errors.push("Local file `#{location}` does not exist!") errors.push("Local file `#{location}` does not exist!")
elsif content.blank? elsif content.blank?
......
...@@ -23,19 +23,25 @@ module Gitlab ...@@ -23,19 +23,25 @@ module Gitlab
end end
def fetch_remote_content def fetch_remote_content
Gitlab::HTTP.get(location) begin
rescue SocketError response = Gitlab::HTTP.get(location)
errors.push("Remote file `#{location}` could not be fetched because of a socket error!") rescue SocketError
nil errors.push("Remote file `#{location}` could not be fetched because of a socket error!")
rescue Timeout::Error rescue Timeout::Error
errors.push("Remote file `#{location}` could not be fetched because of a timeout error!") errors.push("Remote file `#{location}` could not be fetched because of a timeout error!")
nil rescue Gitlab::HTTP::Error
rescue Gitlab::HTTP::Error errors.push("Remote file `#{location}` could not be fetched because of HTTP error!")
errors.push("Remote file `#{location}` could not be fetched because of a HTTP error!") rescue Gitlab::HTTP::BlockedUrlError
nil errors.push("Remote file `#{location}` could not be fetched because the URL is blocked!")
rescue Gitlab::HTTP::BlockedUrlError end
errors.push("Remote file `#{location}` could not be fetched because the URL is blocked!")
nil if response&.code.to_i >= 400
errors.push <<~ERROR
Remote file `#{location}` could not be fetched because of HTTP code `#{response.code}` error!
ERROR
end
response.to_s if errors.none?
end end
end end
end end
......
...@@ -105,10 +105,44 @@ describe Gitlab::Ci::Config::External::File::Remote do ...@@ -105,10 +105,44 @@ describe Gitlab::Ci::Config::External::File::Remote do
end end
describe "#error_message" do describe "#error_message" do
let(:location) { 'not-valid://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' } subject { remote_file.error_message }
it 'should return an error message' do context 'when remote file location is not valid' do
expect(remote_file.error_message).to eq("Remote file `#{location}` does not have a valid address!") let(:location) { 'not-valid://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml' }
it 'returns an error message describing invalid address' do
expect(subject).to match /does not have a valid address!/
end
end
context 'when timeout error has been raised' do
before do
WebMock.stub_request(:get, location).to_timeout
end
it 'should returns error message about a timeout' do
expect(subject).to match /could not be fetched because of a timeout error!/
end
end
context 'when HTTP error has been raised' do
before do
WebMock.stub_request(:get, location).to_raise(Gitlab::HTTP::Error)
end
it 'should returns error message about a HTTP error' do
expect(subject).to match /could not be fetched because of HTTP error!/
end
end
context 'when response has 404 status' do
before do
WebMock.stub_request(:get, location).to_return(body: remote_file_content, status: 404)
end
it 'should returns error message about a timeout' do
expect(subject).to match /could not be fetched because of HTTP code `404` error!/
end
end end
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