0.0
No commit activity in last 3 years
No release in over 3 years
Provides a Result class for more elegant, exception-free error handling. Especially useful for processing input that could be invalid for many different reasons.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

Unexceptional

Provides a Result class for more elegant, exception-free error handling. Especially useful for processing input that could be invalid for many different reasons.

API Docs

http://www.rubydoc.info/github/jarrett/unexceptional/master/Unexceptional/Result

Examples

Result.try(
  ->    { Result.ok 2 },
  ->(i) { Result.ok 3 * i }
)
# => Result.ok(6)


Result.try(
  ->    { Result.ok 2 },
  ->(_) { Result.err :uh_oh },
  ->(i) { Result.ok 3 * i }
)
# => Result.err(:uh_oh)


Result.try(
  ->         { Result.ok [1, 2] },
  ->((a, b)) { Result.ok a + b }
)
# => Result.ok(3)


Result.map_while([1, 2]) do |i|
  Result.ok i * 2
end
# => Result.ok([1, 2])


Result.map_while([1, 2, 3]) do |i|
  if i == 2
    Result.err '2 is invalid'
  elsif i == 3
    raise 'This is never executed because Result.map_while aborts on the previous element.'
  else
    Result.ok i * 2
  end
end
# => Result.err('2 is invalid')


Result
  .err(:uh_oh)
  .and_then { 'This block never executes' }
# => Result.err(:uh_oh)


Result
  .ok('This value gets dropped')
  .and_then(Result.ok('This is the final value'))
# => Result.ok('This is the final value')


Result
  .ok(3)
  .and_then { |v| v * 2 }
# => Result.ok(6)


Result
  .ok('This value gets dropped')
  .and_then
# => Result.ok