Class: Alchemrest::Result
- Inherits:
-
Object
- Object
- Alchemrest::Result
- Defined in:
- lib/alchemrest/result.rb,
lib/alchemrest/result/halt.rb,
lib/alchemrest/result/try_helpers.rb
Defined Under Namespace
Modules: TryHelpers Classes: Error, Halt, Ok
Class Method Summary collapse
-
.[](*_types) ⇒ Object
Allow support for sorbet runtime type checking.
- .Error(error) ⇒ Object
- .for ⇒ Object
- .Ok(value) ⇒ Object
Instance Method Summary collapse
-
#initialize ⇒ Result
constructor
A new instance of Result.
- #transform ⇒ Object
- #unwrap_or_raise! ⇒ Object
-
#unwrap_or_rescue ⇒ Object
The pattern of raising, rescuing, re-raising, rescuing again, and then handling looks a little weird, but it’s necessary to ensure our final result is an Alchemrest::ResultResecued error where ‘error.cause` contains the original exception.
Constructor Details
#initialize ⇒ Result
Returns a new instance of Result.
82 83 84 |
# File 'lib/alchemrest/result.rb', line 82 def initialize(*) raise 'Cannot create an instance of Alchemrest::Result. Use Alchemrest::Result::Ok or Alchemrest::Result::Error instead.' end |
Class Method Details
.[](*_types) ⇒ Object
Allow support for sorbet runtime type checking
6 7 8 |
# File 'lib/alchemrest/result.rb', line 6 def self.[](*_types) self end |
.Error(error) ⇒ Object
65 66 67 |
# File 'lib/alchemrest/result.rb', line 65 def self.Error(error) Error.new(error) end |
.for ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/alchemrest/result.rb', line 69 def self.for block_return_value = yield Alchemrest::Result::TryHelpers case block_return_value in Alchemrest::Result => result result else Alchemrest::Result::Ok.new(block_return_value) end rescue Alchemrest::Result::Halt => e e.error end |
Instance Method Details
#transform ⇒ Object
86 87 88 89 90 91 92 93 94 |
# File 'lib/alchemrest/result.rb', line 86 def transform raise ArgumentError, 'no block given' unless block_given? if ok? self.class::Ok(yield value) else self end end |
#unwrap_or_raise! ⇒ Object
114 115 116 117 118 |
# File 'lib/alchemrest/result.rb', line 114 def unwrap_or_raise! raise error unless ok? value end |
#unwrap_or_rescue ⇒ Object
The pattern of raising, rescuing, re-raising, rescuing again, and then handling looks a little weird, but it’s necessary to ensure our final result is an Alchemrest::ResultResecued error where ‘error.cause` contains the original exception. This way we have information on both the stack trace where the api error originated, and the stack trace of where we tried to rescue it.
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/alchemrest/result.rb', line 101 def unwrap_or_rescue raise ArgumentError, 'no error handler given' unless block_given? unwrap_or_raise! rescue *Alchemrest.rescuable_exceptions => e begin raise Alchemrest::ResultRescued, "Alchemrest rescued an unexpected result of type #{error.class}", cause: e rescue Alchemrest::ResultRescued => rescued_error Alchemrest.handle_rescued_result(rescued_error) yield end end |