Skip to content

Commit

Permalink
Fixes NARKOZ#99.
Browse files Browse the repository at this point in the history
  • Loading branch information
asedge committed Jan 10, 2015
1 parent ae7b961 commit 1695f2e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
15 changes: 14 additions & 1 deletion lib/gitlab/request.rb
Expand Up @@ -106,8 +106,21 @@ def set_httparty_config(options)
end

def error_message(response)
"Server responded with code #{response.code}, message: #{response.parsed_response.message}. " \
"Server responded with code #{response.code}, message: " \
"#{handle_error(response.parsed_response.message)}. " \
"Request URI: #{response.request.base_uri}#{response.request.path}"
end

# Handle error response message in case of nested hashes
def handle_error(message)
if message.is_a? Gitlab::ObjectifiedHash
message.to_h.sort.map do |key, val|
"'#{key}' #{(val.is_a?(Hash) ? val.sort.map { |k,v| "(#{k}: #{v.join(' ')})"} : val).join(' ')}"
end.join(', ')
else
message
end
end

end
end
29 changes: 25 additions & 4 deletions spec/gitlab/request_spec.rb
Expand Up @@ -5,6 +5,12 @@
it { should respond_to :post }
it { should respond_to :put }
it { should respond_to :delete }
before do
@request = Gitlab::Request.new
@obj_h = Gitlab::ObjectifiedHash.new({user: ['not set'],
password: ['too short'],
embed_entity: { foo: ['bar'], sna: ['fu'] }})
end

describe ".default_options" do
it "should have default values" do
Expand All @@ -28,16 +34,15 @@
context "when endpoint is not set" do
it "should raise Error::MissingCredentials" do
expect {
Gitlab::Request.new.set_request_defaults(nil, 1234000)
@request.set_request_defaults(nil, 1234000)
}.to raise_error(Gitlab::Error::MissingCredentials, 'Please set an endpoint to API')
end
end

context "when endpoint is set" do
it "should set instance variable 'endpoint'" do
request = Gitlab::Request.new
request.set_request_defaults('http://rabbit-hole.example.org', 1234000)
expect(request.instance_variable_get(:@endpoint)).to eq("http://rabbit-hole.example.org")
@request.set_request_defaults('http://rabbit-hole.example.org', 1234000)
expect(@request.instance_variable_get(:@endpoint)).to eq("http://rabbit-hole.example.org")
end

it "should set default_params" do
Expand All @@ -46,4 +51,20 @@
end
end
end

describe "#handle_error" do
context "when passed an ObjectifiedHash" do
it "should return a joined string of error messages sorted by key" do
expect(@request.send(:handle_error, @obj_h)).to eq("'embed_entity' (foo: bar) (sna: fu), 'password' too short, 'user' not set")
end
end

context "when passed a String" do
it "should return the String untouched" do
error = 'this is an error string'
expect(@request.send(:handle_error, error)).to eq('this is an error string')
end
end
end

end

0 comments on commit 1695f2e

Please sign in to comment.