Class: MonadicExceptions::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/result.rb

Overview

Result wrap a static method called ‘from_exception` that receive a Proc and rescue it’s exception raises returning either a Failure or a Success variant of the Result monad.

Class Method Summary collapse

Class Method Details

.from_exception(callback) ⇒ Failure({error: Symbol, where: String, orig_exception: Exception, message: String}), Success(data)

This static method act as a bridge between exception to Result, it supress any raises a method invokes and transform into a valid Failure return.

Parameters:

  • callback (Proc)

Returns:

  • (Failure({error: Symbol, where: String, orig_exception: Exception, message: String}), Success(data))


17
18
19
20
21
22
23
24
25
26
# File 'lib/result.rb', line 17

def self.from_exception(callback)
  result = callback.call

  Dry::Monads::Result::Success.new(result)
rescue => e
  exception_name = MonadicExceptions::ResultSanitizer.new.treat_exception_name(e.class.to_s)
  Dry::Monads::Result::Failure.new({ error: exception_name,
                                     where: callback.source_location.first, orig_exception: e,
                                     message: e.message })
end