README.md in sorbet-result-0.3.0 vs README.md in sorbet-result-0.3.1

- old
+ new

@@ -74,14 +74,15 @@ Finally, there are a few methods you can use on both `Typed::Result` types. ```ruby result = call_api(1) -result.success? # => true if success, false if failure -result.failure? # => true if failure, false if success +result.success? # => true on success, false on failure +result.failure? # => true on failure, false on success result.payload # => nil on failure, payload type on failure result.error # => nil on success, error type on failure +result.payload_or("fallback") # => returns payload on success, given value on failure # You can combine all the above to write flow-sensitive type-checked code if result.success? T.assert_type!(result.payload, Float) else @@ -110,9 +111,22 @@ else # Something went wrong, res.error could be either from retrieve_user or send_notification res.error # => T.any(RetrieveUserError, SendNotificationError) end ``` + +You can also use the `#on_error` chain to take an action only on failure, such as logging or capturing error information in an error monitoring service. + +```ruby +# In this example, retrieve_user and send_notification both return a Typed::Result +# retrieve_user: Typed::Result[User, RetrieveUserError +# send_notification: Typed::Result[T::Boolean, SendNotificationError] +res = retrieve_user(user_id) + .and_then { |user| send_notification(user.email) } # this block will only run if retrieve_user returns a Typed::Success + .on_error { |error| puts "Encountered this error: #{error}"} +``` + +If the above chain does not fail, the `puts` statement is never run. If the chain does yield a `Failure`, the `puts` block is executed and the `Failure` is ultimately returned. ## Why use Results? Let's say you're working on a method that reaches out to an API and fetches a resource. We hope to get a successful response and continue on in our program, but you can imagine several scenarios where we don't get that response: our authentication could fail, the server could return a 5XX response code, or the resource we were querying could have moved or not exist any more.