summaryrefslogtreecommitdiff
path: root/src/mongo/util/future.h
diff options
context:
space:
mode:
authorJonathan Ma <jonathan.ma@mongodb.com>2019-01-25 14:37:32 -0500
committerJonathan Ma <jonathan.ma@mongodb.com>2019-01-25 17:11:53 -0500
commit6d1458c964e600a07b183d0d5331eff0bcab9493 (patch)
tree3f56f6b40ba565328a56d92366f83e97e41bd7e4 /src/mongo/util/future.h
parentffe35fbcb46d344fef138fe82dfbedc070205ab8 (diff)
downloadmongo-6d1458c964e600a07b183d0d5331eff0bcab9493.tar.gz
SERVER-38368 Allow handling ErrorCategory types for Futures error handling
Diffstat (limited to 'src/mongo/util/future.h')
-rw-r--r--src/mongo/util/future.h23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/mongo/util/future.h b/src/mongo/util/future.h
index 3033bec5687..c32731c1986 100644
--- a/src/mongo/util/future.h
+++ b/src/mongo/util/future.h
@@ -1097,6 +1097,29 @@ public:
}
/**
+ * Similar to the first two onErrors, but only calls the callback if the category matches
+ * the template parameter. Otherwise lets the error propagate unchanged.
+ */
+ template <ErrorCategory category, typename Func>
+ Future<T> onErrorCategory(Func&& func) && noexcept {
+ using Result = RawNormalizedCallResult<Func, Status>;
+ static_assert(
+ std::is_same<Result, T>::value || std::is_same<Result, Future<T>>::value ||
+ (std::is_same<T, FakeVoid>::value && std::is_same<Result, Future<void>>::value),
+ "func passed to Future<T>::onErrorCategory must return T, StatusWith<T>, or Future<T>");
+
+ if (_immediate || (isReady() && _shared->status.isOK()))
+ return std::move(*this);
+
+ return std::move(*this).onError([func =
+ std::forward<Func>(func)](Status && status) mutable {
+ if (!ErrorCodes::isA<category>(status.code()))
+ uassertStatusOK(status);
+ return throwingCall(func, std::move(status));
+ });
+ }
+
+ /**
* TODO do we need a version of then/onError like onCompletion() that handles both success and
* Failure, but doesn't end the chain like getAsync()? Right now we don't, and we can add one if
* we do.