Class: Dry::Monads::Try::Error

Inherits:
Try
  • Object
show all
Includes:
RightBiased::Left
Defined in:
lib/dry/monads/try.rb,
lib/dry/monads/maybe.rb,
lib/dry/monads/result.rb

Overview

Represents a result of a failed execution.

Instance Method Summary collapse

Methods included from RightBiased::Left

#and, #apply, #bind, #deconstruct, #deconstruct_keys, #discard, #flatten, #fmap, #or_fmap, #tee, trace_caller, #value!, #value_or, #|

Constructor Details

#initialize(exception) ⇒ Error

Returns a new instance of Error.



178
179
180
181
182
# File 'lib/dry/monads/try.rb', line 178

def initialize(exception)
  super()

  @exception = @value = exception
end

Instance Method Details

#===(other) ⇒ Boolean



208
# File 'lib/dry/monads/try.rb', line 208

def ===(other) = Error === other && exception === other.exception

#or(*args) ⇒ Object

If a block is given passes internal value to it and returns the result, otherwise simply returns the first argument.

Examples:

Try(ZeroDivisionError) { 1 / 0 }.or { "zero!" } # => "zero!"


198
199
200
201
202
203
204
# File 'lib/dry/monads/try.rb', line 198

def or(*args)
  if block_given?
    yield(exception, *args)
  else
    args[0]
  end
end

#recover(*errors) ⇒ Try::Value

Acts in a similar way to rescue. It checks if Dry::Monads::Try#exception is one of errors and yields the block if so.



216
217
218
219
220
221
222
223
224
225
226
227
228
# 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

#to_maybeMaybe::None



395
# File 'lib/dry/monads/maybe.rb', line 395

def to_maybe = Maybe::None.new(RightBiased::Left.trace_caller)

#to_resultResult::Failure



414
415
416
# File 'lib/dry/monads/result.rb', line 414

def to_result
  Result::Failure.new(exception, RightBiased::Left.trace_caller)
end

#to_sString Also known as: inspect



185
# File 'lib/dry/monads/try.rb', line 185

def to_s = "Try::Error(#{exception.class}: #{exception.message})"