diff options
author | Achilleas Pipinellis <axil@gitlab.com> | 2019-06-24 17:24:26 +0000 |
---|---|---|
committer | Achilleas Pipinellis <axil@gitlab.com> | 2019-06-24 17:24:26 +0000 |
commit | 1f2387c4ffd710eb9ce0b006e59a76fcae2af437 (patch) | |
tree | e57c60f4fcc3c8c071f1a91bddfa656c943afcb1 | |
parent | 7f6de7fe29c2731975166b1a864c5a5cf3141132 (diff) | |
parent | b32f7d489136e3172967f1031d5ca58aacd26cab (diff) | |
download | gitlab-ce-1f2387c4ffd710eb9ce0b006e59a76fcae2af437.tar.gz |
Merge branch '62356-service-response-docs' into 'master'
Add doc for ServiceResponse
Closes #62356
See merge request gitlab-org/gitlab-ce!29992
-rw-r--r-- | doc/development/reusing_abstractions.md | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/doc/development/reusing_abstractions.md b/doc/development/reusing_abstractions.md index 01cedf734fb..59da02ed6fd 100644 --- a/doc/development/reusing_abstractions.md +++ b/doc/development/reusing_abstractions.md @@ -127,6 +127,42 @@ Everything in `lib/api`. Everything that resides in `app/services`. +#### ServiceResponse + +Service classes usually have an `execute` method, which can return a +`ServiceResponse`. You can use `ServiceResponse.success` and +`ServiceResponse.error` to return a response in `execute` method. + +In a successful case: + +``` ruby +response = ServiceResponse.success(message: 'Branch was deleted') + +response.success? # => true +response.error? # => false +response.status # => :success +response.message # => 'Branch was deleted' +``` + +In a failed case: + +``` ruby +response = ServiceResponse.error(message: 'Unsupported operation') + +response.success? # => false +response.error? # => true +response.status # => :error +response.message # => 'Unsupported operation' +``` + +An additional payload can also be attached: + +``` ruby +response = ServiceResponse.success(payload: { issue: issue }) + +response.payload[:issue] # => issue +``` + ### Finders Everything in `app/finders`, typically used for retrieving data from a database. |