Comments (8)
@serradura , thank you very much for working with me on this! This definitely means that this lib is in-scope for us.
from u-case.
@ekampp you can return a failure in this case. e.g:
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'u-service', '~> 0.12.0'
end
class Divide < Micro::Service::Base
attributes :a, :b
def call!
return Failure('numbers must be greater than 0') if a < 0 || b < 0
Success(a / b)
rescue => e
Failure(e.message)
end
end
#---------------------------------#
puts "\n-- Success scenario --\n\n"
#---------------------------------#
result = Divide.call(a: 4, b: 2)
puts result.value if result.success?
#----------------------------------#
puts "\n-- Failure scenarios --\n\n"
#----------------------------------#
result = Divide.call(a: 4, b: 0)
puts result.value if result.failure?
puts ''
result = Divide.call(a: -4, b: 2)
puts result.value if result.failure?
# :: example of the output: ::
# -- Success scenario --
#
# 2
#
# -- Failure scenarios --
#
# divided by 0
#
# numbers must be greater than 0
from u-case.
I saw that example as well. However, this behavior breaks regular, expected exception handling. This is very unexpected and is probably a blocking issue for us.
I would love to work with you on a change if you're open to this.
from u-case.
@ekampp I created a new example to help the understanding of how to handle exceptions/the program flow in case of different kinds of failures.
https://github.com/serradura/u-service/tree/master/examples/calculator
Gif showing the usage of this CLI (rake tasks).
Examples:
- Handle exceptions outside of the service objects
- Using different kinds of failures to handle the program flow
Question: Some of these examples help you on how to use the gem in your application?
from u-case.
I understand how you expect exceptions should be handled.
This, however, moves exception handling outside the actual service object. This conflicts with separation-of-concern and -responsibility.
The outside caller of the service object would have to know every error that the service object could cause. This also prevents the service object from reporting exceptions internally. Right?
from u-case.
@ekampp what do you think about a macro to enable an auto failure if happens an exception inside of a service object?
from u-case.
@ekampp could you share an example of your use case?
from u-case.
I'm not sure I'm clear on what you mean by this comment.
A bit more complex example could be something like this:
def call!
# This could fail if the ID is invalid
user = User.find id # id is the call parameter
# This could fail with a different error, which means the calling class should now know about two errors.
user.complex_calculation
# This could fail with a third type of error, which means that use calling class should now know about three, different errors.
user.heavy_calculations
end
In this case, the calling class should know about three, different error states, which is relevant to the user object. So the calling class should know about errors from an object that's twice removed from the caller.
I think it would be cleaner to implement if one could add a rescue block inside the service to handle errors and report them to an exception handling service if needed.
from u-case.
Related Issues (20)
- (BREAKING CHANGE) Allow a success result have access to its use case
- Create a configuration object to disable/enable the different gems features
- Rename branch master to main HOT 1
- Remove Micro::Case#call (Breaking Change)
- Normalize the name of all the private methods
- Add more methods to Micro::Case::Result quack like a Hash HOT 1
- Improve Micro::Case internal steps [NEW FEATURE]
- MyCase.call(hash) { |on| on.success {} } HOT 2
- [pt-BR] Aprimorar documentação HOT 1
- Adicionar documentação e exemplo sobre uso do Micro::Case #apply
- Add Micro::Case::Result#to_sym
- Micro::Case::Result#on - Enable pattern matching of result type and data for Ruby >= 2.2.0 HOT 1
- Allow calling a case inside of another one
- Add new Micro::Case::Config (enable_attributes_accept = true)
- Improve the inspect output
- Class/Case with an inner flow has a strange behavior when it receives an array of cases
- Micro::Case#transaction
- QUESTION: Navigating a flow HOT 2
- Micro::Case#then(:method_name) HOT 1
- DRAFT: Plans for the next major version (v5)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from u-case.