Sometimes it makes sense to treat errors as part of an application’s “valid” flow of events. Meaning, expected errors should not necessarily make your code go crazy when random errors are raised.
With that in mind, I created a way to implement some kind of “Result Object” by rescuing StandardError instances and handling these errors gracefully.
Gracefully is a wrapper for a result object that will either be a success value or an error object, depending on the block that was passed and invoked.
In the example above, if order with id 999 does not exist, result.success? method will return false. Otherwise, true.
If result.success? is true, result.value will contain the successful value. Otherwise, result.error will hold the error object itself.
Whenever Gracefully.handle is called, it will return a Gracefully object. If you want to get the raw value, you can use result.value.
By using this, I can be resilient in handling errors. This will help me recover from failures in the process flow.