HomePhabricator

[Error] Add a handleExpected utility.

Description

[Error] Add a handleExpected utility.

handleExpected is similar to handleErrors, but takes an Expected<T> as its first
input value and a fallback functor as its second, followed by an arbitary list
of error handlers (equivalent to the handler list of handleErrors). If the first
input value is a success value then it is returned from handleErrors
unmodified. Otherwise the contained error(s) are passed to handleErrors, along
with the handlers. If handleErrors returns success (indicating that all errors
have been handled) then handleExpected runs the fallback functor and returns its
result. If handleErrors returns a failure value then the failure value is
returned and the fallback functor is never run.

This simplifies the process of re-trying operations that return Expected values.
Without this utility such retry logic is cumbersome as the internal Error must
be explicitly extracted from the Expected value, inspected to see if its
handleable and then consumed:

enum FooStrategy { Aggressive, Conservative };
Expected<Foo> tryFoo(FooStrategy S);

Expected<Foo> Result;
(void)!!Result; // "Check" Result so that it can be safely overwritten.
if (auto ValOrErr = tryFoo(Aggressive))

Result = std::move(ValOrErr);

else {

auto Err = ValOrErr.takeError();
if (Err.isA<HandleableError>()) {
  consumeError(std::move(Err));
  Result = tryFoo(Conservative);
} else
  return std::move(Err);

}

with handleExpected, this can be re-written as:

auto Result =

handleExpected(
  tryFoo(Aggressive),
  []() { return tryFoo(Conservative); },
  [](HandleableError&) { /* discard to handle */ });

Details

Committed
lhamesAug 27 2017, 8:36 PM
Parents
rL311869: [asan] Move __asan_handle_no_return to public header
Branches
Unknown
Tags
Unknown