Exemplo n.º 1
0
        /// <summary>
        /// Catches any errors in the source future and handles them with the specified handler.
        /// If <paramref name="handler"/> throws an error the result will complete with that error.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source">The source.</param>
        /// <param name="handler">The handler.</param>
        /// <returns>A result that will catch errors from <paramref name="source"/> with <paramref name="handler"/>.</returns>
        public static Future <T> Catch <T>(this IFuture <T> source, Func <Exception, T> handler)
        {
            var result = new Future <T>();

            source.OnComplete(() =>
            {
                if (source.Status == FutureStatus.Failure)
                {
                    try
                    {
                        result.SetResult(handler(source.Error));
                    }
                    catch (Exception ex)
                    {
                        result.SetError(ex);
                    }
                    return;
                }
                source.CopyTo(result);
            });

            return(result);
        }