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

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

Overview

Represents a result of a failed execution.

Constant Summary

Constants inherited from Dry::Monads::Try

DEFAULT_EXCEPTIONS

Instance Attribute Summary

Attributes inherited from Dry::Monads::Try

#exception

Instance Method Summary collapse

Methods included from RightBiased::Left

#apply, #bind, #discard, #fmap, #or_fmap, #tee, trace_caller, #value!, #value_or

Methods inherited from Dry::Monads::Try

[], #error?, pure, run, #to_monad, #value?

Constructor Details

#initialize(exception) ⇒ Error

Returns a new instance of Error.

Parameters:

  • exception (Exception)


172
173
174
# File 'lib/dry/monads/try.rb', line 172

def initialize(exception)
  @exception = exception
end

Instance Method Details

#===(other) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


202
203
204
# File 'lib/dry/monads/try.rb', line 202

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

#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!"

Parameters:

  • args (Array<Object>)

    arguments that will be passed to a block if one was given, otherwise the first value will be returned

Returns:

  • (Object)


192
193
194
195
196
197
198
# File 'lib/dry/monads/try.rb', line 192

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

#to_maybeMaybe::None

Returns:



274
275
276
# File 'lib/dry/monads/maybe.rb', line 274

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

#to_resultResult::Failure

Returns:



343
344
345
# File 'lib/dry/monads/result.rb', line 343

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

#to_sString Also known as: inspect

Returns:

  • (String)


177
178
179
# File 'lib/dry/monads/try.rb', line 177

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