Commit 85fd72fe authored by Robert Speicher's avatar Robert Speicher

Add error_code attribute to ChangeError

Also adds an additional `payload` keyword argument to the
`BaseService#error` method, which adds additional attributes to the
`result` Hash.
parent 7139d248
......@@ -50,16 +50,24 @@ class BaseService
private
def error(message, http_status = nil)
# Return a Hash with an `error` status
#
# message - Error message to include in the Hash
# http_status - Optional HTTP status code override (default: nil)
# pass_back - Additional attributes to be included in the resulting Hash
def error(message, http_status = nil, pass_back: {})
result = {
message: message,
status: :error
}
}.reverse_merge(pass_back)
result[:http_status] = http_status if http_status
result
end
# Return a Hash with a `success` status
#
# pass_back - Additional attributes to be included in the resulting Hash
def success(pass_back = {})
pass_back[:status] = :success
pass_back
......
......@@ -30,7 +30,8 @@ module Commits
error_msg = "Sorry, we cannot #{act} this #{type} automatically. " \
"This #{type} may already have been #{act}ed, or a more recent " \
"commit may have updated some of its content."
raise ChangeError, error_msg
raise ChangeError.new(error_msg, ex.error_code)
end
end
end
......@@ -3,7 +3,15 @@
module Commits
class CreateService < ::BaseService
ValidationError = Class.new(StandardError)
ChangeError = Class.new(StandardError)
class ChangeError < StandardError
attr_reader :error_code
def initialize(message, error_code = nil)
super(message)
@error_code = error_code
end
end
def initialize(*args)
super
......@@ -21,8 +29,9 @@ module Commits
new_commit = create_commit!
success(result: new_commit)
rescue ChangeError => ex
error(ex.message, pass_back: { error_code: ex.error_code })
rescue ValidationError,
ChangeError,
Gitlab::Git::Index::IndexError,
Gitlab::Git::CommitError,
Gitlab::Git::PreReceiveError,
......
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