Commit 6d928726 authored by Douwe Maan's avatar Douwe Maan

Better error handling.

parent 76d96363
module Projects module Projects
class UpdateMirrorService < BaseService class UpdateMirrorService < BaseService
class FetchError < StandardError; end class Error < StandardError; end
class UpdateError < Error; end
def execute def execute
return false unless project.mirror? return false unless project.mirror?
begin unless current_user.can?(:push_code_to_protected_branches, project)
update_tags do return error("The mirror user is not allowed to push code to all branches on this project.")
project.fetch_mirror end
end
rescue Gitlab::Shell::Error => e update_tags do
raise FetchError, e.message project.fetch_mirror
end end
update_branches update_branches
true success
rescue Gitlab::Shell::Error, UpdateError => e
error(e.message)
end end
private private
...@@ -29,13 +32,20 @@ module Projects ...@@ -29,13 +32,20 @@ module Projects
local_branch = local_branches[name] local_branch = local_branches[name]
if local_branch.nil? if local_branch.nil?
CreateBranchService.new(project, current_user).execute(name, upstream_branch.target) result = CreateBranchService.new(project, current_user).execute(name, upstream_branch.target)
if result[:status] == :error
raise UpdateError, result[:message]
end
elsif local_branch.target == upstream_branch.target elsif local_branch.target == upstream_branch.target
# Already up to date # Already up to date
elsif repository.diverged_from_upstream?(name) elsif repository.diverged_from_upstream?(name)
# Cannot be updated # Cannot be updated
else else
repository.ff_merge(current_user, upstream_branch.target, name) begin
repository.ff_merge(current_user, upstream_branch.target, name)
rescue Repository::PreReceiveError, Repository::CommitError => e
raise UpdateError, e.message
end
end end
end end
end end
......
...@@ -11,10 +11,9 @@ class RepositoryUpdateMirrorWorker ...@@ -11,10 +11,9 @@ class RepositoryUpdateMirrorWorker
# TODO: Use actual user # TODO: Use actual user
@current_user = User.last @current_user = User.last
begin result = Projects::UpdateMirrorService.new(@project, @current_user).execute
Projects::UpdateMirrorService.new(@project, @current_user).execute if result[:status] == :error
rescue Projects::UpdateMirrorService::FetchError => e project.update(import_error: result[:message])
project.update(import_error: e.message)
project.import_fail project.import_fail
return return
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