When using .to_result
inside a class that has defined Failure
constrains, should the result respect those constrains.
Eg:
require "dry/monads/do"
require "dry/monads/result"
require "dry/monads/try"
require 'dry-types'
module Types
include Dry.Types()
end
class DoSomething
class Errors
class BananaError; end
class InternalError; end
end
include Dry::Monads[:result, :try]
include Dry::Monads::Do.for(:call)
Error = Types.Instance(Errors)
include Dry::Monads::Result(Error)
def call()
Try {
raise StandardError.new('BOOOM')
}.to_result
rescue Dry::Monads::InvalidFailureTypeError => e
puts "it didn't got rescued"
# expected to catch here as the return is not one of the declared errors
# Failure(#<StandardError: BOOOM>)
end
end
puts DoSomething.new.call
# Failure(#<StandardError: BOOOM>)
require "dry/monads/do"
require "dry/monads/result"
require "dry/monads/try"
require 'dry-types'
module Types
include Dry.Types()
end
class DoSomethingElse
class Errors
class BananaError; end
class InternalError; end
end
include Dry::Monads[:result, :try]
include Dry::Monads::Do.for(:call)
Error = Types.Instance(Errors)
include Dry::Monads::Result(Error)
def call()
Failure(StandardError.new('BOOOM'))
rescue Dry::Monads::InvalidFailureTypeError => e
puts 'it got rescued'
end
end
DoSomethingElse.new.call
# 'it got rescued'
Seems like the execution of .to_result
is done in another scope, so the constrain is not applied. is it the expected behaviour? is there a way around it?