Commit 23c02982 authored by Rémy Coutable's avatar Rémy Coutable

Merge branch...

Merge branch '28769-raise-custom-exception-in-all-methods-of-mergeservice-that-want-to-inform-the-user' into 'master'

Use exceptions for MergeService error handling

Closes #28769

See merge request !9576
parents a69aa3da 90666937
...@@ -6,6 +6,8 @@ module MergeRequests ...@@ -6,6 +6,8 @@ module MergeRequests
# Executed when you do merge via GitLab UI # Executed when you do merge via GitLab UI
# #
class MergeService < MergeRequests::BaseService class MergeService < MergeRequests::BaseService
MergeError = Class.new(StandardError)
attr_reader :merge_request, :source attr_reader :merge_request, :source
def execute(merge_request) def execute(merge_request)
...@@ -27,6 +29,8 @@ module MergeRequests ...@@ -27,6 +29,8 @@ module MergeRequests
success success
end end
end end
rescue MergeError => e
log_merge_error(e.message, save_message_on_model: true)
end end
private private
...@@ -42,19 +46,13 @@ module MergeRequests ...@@ -42,19 +46,13 @@ module MergeRequests
commit_id = repository.merge(current_user, source, merge_request, options) commit_id = repository.merge(current_user, source, merge_request, options)
if commit_id raise MergeError, 'Conflicts detected during merge' unless commit_id
merge_request.update(merge_commit_sha: commit_id) merge_request.update(merge_commit_sha: commit_id)
else
log_merge_error('Conflicts detected during merge', save_message_on_model: true)
false
end
rescue GitHooksService::PreReceiveError => e rescue GitHooksService::PreReceiveError => e
log_merge_error(e.message, save_message_on_model: true) raise MergeError, e.message
false
rescue StandardError => e rescue StandardError => e
merge_request.update(merge_error: "Something went wrong during merge: #{e.message}") raise MergeError, "Something went wrong during merge: #{e.message}"
log_merge_error(e.message)
false
ensure ensure
merge_request.update(in_progress_merge_commit_sha: nil) merge_request.update(in_progress_merge_commit_sha: nil)
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