Skip to content

Commit

Permalink
Made 409's throw exceptions
Browse files Browse the repository at this point in the history
-Added more detail to error messages

GITLAB-721 (GITLAB-116)
  • Loading branch information
Izaak Alpert committed Jun 18, 2013
1 parent 0829e3c commit b7f6c48
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 20 deletions.
3 changes: 3 additions & 0 deletions lib/gitlab/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class Forbidden < Error; end
# Raised when API endpoint returns the HTTP status code 404.
class NotFound < Error; end

# Raised when API endpoint returns the HTTP status code 409.
class Conflict < Error; end

# Raised when API endpoint returns the HTTP status code 500.
class InternalServerError < Error; end

Expand Down
34 changes: 23 additions & 11 deletions lib/gitlab/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ module Gitlab
# @private
class Request
include HTTParty
format :json
format :json
headers 'Accept' => 'application/json'
parser Proc.new {|body, _| parse(body)}
parser Proc.new { |body, _| parse(body) }

# Converts the response body to an ObjectifiedHash.
def self.parse(body)
Expand All @@ -15,7 +15,7 @@ def self.parse(body)
if body.is_a? Hash
ObjectifiedHash.new body
elsif body.is_a? Array
body.collect! {|e| ObjectifiedHash.new(e)}
body.collect! { |e| ObjectifiedHash.new(e) }
else
raise Error::Parsing.new "Couldn't parse a response body"
end
Expand Down Expand Up @@ -53,20 +53,32 @@ def delete(path)
# Checks the response code for common errors.
# Returns parsed response for successful requests.
def validate(response)
message = "Server responsed with code #{response.code}"
case response.code
when 400; raise Error::BadRequest.new message
when 401; raise Error::Unauthorized.new message
when 403; raise Error::Forbidden.new message
when 404; raise Error::NotFound.new message
when 500; raise Error::InternalServerError.new message
when 502; raise Error::BadGateway.new message
when 503; raise Error::ServiceUnavailable.new message
when 400;
raise Error::BadRequest.new(error_message(response))
when 401;
raise Error::Unauthorized.new(error_message(response))
when 403;
raise Error::Forbidden.new(error_message(response))
when 404;
raise Error::NotFound.new(error_message(response))
when 409;
raise Error::Conflict.new(error_message(response))
when 500;
raise Error::InternalServerError.new(error_message(response))
when 502;
raise Error::BadGateway.new(error_message(response))
when 503;
raise Error::ServiceUnavailable.new(error_message(response))
end

response.parsed_response
end

def error_message(response)
"Server responded with code #{response.code}, message: #{response.parsed_response.message}"
end

# Sets a base_uri and private_token parameter for requests.
# @raise [Error::MissingCredentials] if endpoint or private_token not set.
def set_request_defaults(endpoint, private_token)
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/error_already_exists.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"message": "409 Already exists"}
29 changes: 20 additions & 9 deletions spec/gitlab/client/user_teams_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,29 @@
end

describe ".add_user_team_member" do
before do
stub_post("/user_teams/3/members", "user_team_member")
@team_member = Gitlab.add_user_team_member(3, 1, 40)
end
context 'successful add' do
before do
stub_post("/user_teams/3/members", "user_team_member")
@team_member = Gitlab.add_user_team_member(3, 1, 40)
end

it "should get the correct resource" do
a_post("/user_teams/3/members").
with(:body => {:user_id => '1', :access_level => '40'}).should have_been_made
it "should get the correct resource" do
a_post("/user_teams/3/members").
with(:body => {:user_id => '1', :access_level => '40'}).should have_been_made
end

it "should return information about an added team member" do
@team_member.name.should == "John Smith"
end
end

it "should return information about an added team member" do
@team_member.name.should == "John Smith"
context 'unsuccessful add' do
it "should throw an exception if the team already exists" do
stub_request(:post, "#{Gitlab.endpoint}/user_teams/3/members").
with(:query => {:private_token => Gitlab.private_token}).
to_return({:body => load_fixture("error_already_exists"), :status => 409})
expect {Gitlab.add_user_team_member(3, 1, 40)}.to raise_error(Gitlab::Error::Conflict) {|e|e.message.should == "Server responded with code 409, message: 409 Already exists"}
end
end
end

Expand Down

0 comments on commit b7f6c48

Please sign in to comment.