class Dry::Monads::Try::Value::Error
Represents a result of a failed execution.
@api public
Public Class Methods
Source
# File lib/dry/monads/try.rb, line 178 def initialize(exception) super() @exception = @value = exception end
@param exception [Exception]
Calls superclass method
Public Instance Methods
Source
# File lib/dry/monads/try.rb, line 208 def ===(other) = Error === other && exception === other.exception # Acts in a similar way to `rescue`. It checks if # {exception} is one of {errors} and yields the block if so. # # @param errors [Class] List of Exception subclasses # # @return [Try::Value] def recover(*errors) if errors.empty? classes = DEFAULT_EXCEPTIONS else classes = errors end if classes.any? { _1 === exception } Value.new([exception.class], yield(exception)) else self end end end
@param other [Try] @return [Boolean]
Source
# File lib/dry/monads/try.rb, line 198 def or(*args) if block_given? yield(exception, *args) else args[0] end end
If a block is given passes internal value to it and returns the result, otherwise simply returns the first argument.
@example
Try(ZeroDivisionError) { 1 / 0 }.or { "zero!" } # => "zero!"
@param args [Array<Object>] arguments that will be passed to a block
if one was given, otherwise the first value will be returned
@return [Object]
Source
# File lib/dry/monads/try.rb, line 216 def recover(*errors) if errors.empty? classes = DEFAULT_EXCEPTIONS else classes = errors end if classes.any? { _1 === exception } Value.new([exception.class], yield(exception)) else self end end
Acts in a similar way to ‘rescue`. It checks if {exception} is one of {errors} and yields the block if so.
@param errors [Class] List
of Exception subclasses
@return [Try::Value]
Source
# File lib/dry/monads/try.rb, line 185 def to_s = "Try::Error(#{exception.class}: #{exception.message})" alias_method :inspect, :to_s # If a block is given passes internal value to it and returns the result, # otherwise simply returns the first argument. # # @example # Try(ZeroDivisionError) { 1 / 0 }.or { "zero!" } # => "zero!" # # @param args [Array<Object>] arguments that will be passed to a block # if one was given, otherwise the first # value will be returned # @return [Object] def or(*args) if block_given? yield(exception, *args) else args[0] end end # @param other [Try] # @return [Boolean] def ===(other) = Error === other && exception === other.exception # Acts in a similar way to `rescue`. It checks if # {exception} is one of {errors} and yields the block if so. # # @param errors [Class] List of Exception subclasses # # @return [Try::Value] def recover(*errors) if errors.empty? classes = DEFAULT_EXCEPTIONS else classes = errors end if classes.any? { _1 === exception } Value.new([exception.class], yield(exception)) else self end end end # A module that can be included for easier access to Try monads. # # @example # class Foo # include Dry::Monads::Try::Mixin # # attr_reader :average # # def initialize(total, count) # @average = Try(ZeroDivisionError) { total / count }.value # end # end # # Foo.new(10, 2).average # => 5 # Foo.new(10, 0).average # => nil module Mixin # @see Dry::Monads::Try Try = Try # @private module Constructors # A convenience wrapper for {Monads::Try.run}. # If no exceptions are provided it falls back to StandardError. # In general, relying on this behaviour is not recommended as it can lead to unnoticed # bugs and it is always better to explicitly specify a list of exceptions if possible. # # @param exceptions [Array<Exception>] # @return [Try] def Try(*exceptions, &f) catchable = exceptions.empty? ? DEFAULT_EXCEPTIONS : exceptions.flatten Try.run(catchable, f) end end include Constructors # Value constructor # # @overload Value(value) # @param value [Object] # @return [Try::Value] # # @overload Value(&block) # @param block [Proc] a block to be wrapped with Value # @return [Try::Value] # def Value(value = Undefined, exceptions = DEFAULT_EXCEPTIONS, &block) v = Undefined.default(value, block) raise ArgumentError, "No value given" if !value.nil? && v.nil? Try::Value.new(exceptions, v) end # Error constructor # # @overload Error(value) # @param error [Exception] # @return [Try::Error] # # @overload Error(&block) # @param block [Proc] a block to be wrapped with Error # @return [Try::Error] # def Error(error = Undefined, &block) v = Undefined.default(error, block) raise ArgumentError, "No value given" if v.nil? Try::Error.new(v) end end end
@return [String]