diff options
author | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2019-04-24 07:52:59 +0000 |
---|---|---|
committer | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2019-04-24 07:52:59 +0000 |
commit | 87880cb841847fee43f777e8e69afc383e6b25ff (patch) | |
tree | 1c50506e5788cc3cecff2766548e40fc93049626 /app/services | |
parent | c45bc6d094863caf425c10fa6ff334629b549689 (diff) | |
parent | b77d811c824ceb09669bf57205f490c565beb795 (diff) | |
download | gitlab-ce-87880cb841847fee43f777e8e69afc383e6b25ff.tar.gz |
Merge branch '60730-service-response' into 'master'
Introduce ServiceResponse to wrap around response
Closes #60730
See merge request gitlab-org/gitlab-ce!27516
Diffstat (limited to 'app/services')
-rw-r--r-- | app/services/delete_branch_service.rb | 24 | ||||
-rw-r--r-- | app/services/service_response.rb | 31 |
2 files changed, 42 insertions, 13 deletions
diff --git a/app/services/delete_branch_service.rb b/app/services/delete_branch_service.rb index 4c3ac19f754..fd41ce54486 100644 --- a/app/services/delete_branch_service.rb +++ b/app/services/delete_branch_service.rb @@ -6,27 +6,25 @@ class DeleteBranchService < BaseService branch = repository.find_branch(branch_name) unless current_user.can?(:push_code, project) - return error('You dont have push access to repo', 405) + return ServiceResponse.error( + message: 'You dont have push access to repo', + http_status: 405) end unless branch - return error('No such branch', 404) + return ServiceResponse.error( + message: 'No such branch', + http_status: 404) end if repository.rm_branch(current_user, branch_name) - success('Branch was deleted') + ServiceResponse.success(message: 'Branch was deleted') else - error('Failed to remove branch') + ServiceResponse.error( + message: 'Failed to remove branch', + http_status: 400) end rescue Gitlab::Git::PreReceiveError => ex - error(ex.message) - end - - def error(message, return_code = 400) - super(message).merge(return_code: return_code) - end - - def success(message) - super().merge(message: message) + ServiceResponse.error(message: ex.message, http_status: 400) end end diff --git a/app/services/service_response.rb b/app/services/service_response.rb new file mode 100644 index 00000000000..1de30e68d87 --- /dev/null +++ b/app/services/service_response.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +class ServiceResponse + def self.success(message: nil) + new(status: :success, message: message) + end + + def self.error(message:, http_status: nil) + new(status: :error, message: message, http_status: http_status) + end + + attr_reader :status, :message, :http_status + + def initialize(status:, message: nil, http_status: nil) + self.status = status + self.message = message + self.http_status = http_status + end + + def success? + status == :success + end + + def error? + status == :error + end + + private + + attr_writer :status, :message, :http_status +end |