Skip to content

Commit

Permalink
gracefully handle HTTP errors in create and fork
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Feb 14, 2011
1 parent 166aacc commit 55a27ba
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/hub/commands.rb
Expand Up @@ -258,6 +258,10 @@ def fork(args)
args.after { puts "new remote: #{github_user}" }
end
end
rescue Net::HTTPExceptions
response = $!.response
warn "error creating fork: #{response.message} (HTTP #{response.code})"
exit 1
end

# $ hub create
Expand Down Expand Up @@ -302,6 +306,10 @@ def create(args)

args.after { puts "#{action}: #{github_user}/#{repo_name}" }
end
rescue Net::HTTPExceptions
response = $!.response
warn "error creating repository: #{response.message} (HTTP #{response.code})"
exit 1
end

# $ hub push origin,staging cool-feature
Expand Down Expand Up @@ -661,7 +669,8 @@ def repo_exists?(user)
# Returns nothing.
def fork_repo
url = API_FORK % [repo_owner, repo_name]
Net::HTTP.post_form(URI(url), 'login' => github_user, 'token' => github_token)
response = Net::HTTP.post_form(URI(url), 'login' => github_user, 'token' => github_token)
response.error! unless Net::HTTPSuccess === response
end

# Creates a new repo using the GitHub API.
Expand All @@ -674,7 +683,8 @@ def create_repo(options = {})
params['description'] = options[:description] if options[:description]
params['homepage'] = options[:homepage] if options[:homepage]

Net::HTTP.post_form(URI(url), params)
response = Net::HTTP.post_form(URI(url), params)
response.error! unless Net::HTTPSuccess === response
end

end
Expand Down
19 changes: 19 additions & 0 deletions test/hub_test.rb
Expand Up @@ -397,6 +397,16 @@ def test_create
assert_equal expected, hub("create") { ENV['GIT'] = 'echo' }
end

def test_create_failed
stub_no_remotes
stub_nonexisting_fork('tpw')
stub_request(:post, "github.com/api/v2/yaml/repos/create").
to_return(:status => [401, "Your token is fail"])

expected = "error creating repository: Your token is fail (HTTP 401)\n"
assert_equal expected, hub("create") { ENV['GIT'] = 'echo' }
end

def test_create_with_env_authentication
stub_no_remotes
stub_nonexisting_fork('mojombo')
Expand Down Expand Up @@ -484,6 +494,15 @@ def test_fork
assert_equal expected, hub("fork") { ENV['GIT'] = 'echo' }
end

def test_fork_failed
stub_nonexisting_fork('tpw')
stub_request(:post, "github.com/api/v2/yaml/repos/fork/defunkt/hub").
to_return(:status => [500, "Your fork is fail"])

expected = "error creating fork: Your fork is fail (HTTP 500)\n"
assert_equal expected, hub("fork") { ENV['GIT'] = 'echo' }
end

def test_fork_no_remote
stub_nonexisting_fork('tpw')
stub_request(:post, "github.com/api/v2/yaml/repos/fork/defunkt/hub")
Expand Down

0 comments on commit 55a27ba

Please sign in to comment.