From a98e9402e7a0d79a1b36c065f44b596091412c46 Mon Sep 17 00:00:00 2001 From: Mike Oliva Date: Tue, 12 Jun 2018 16:00:42 +0200 Subject: [PATCH 1/4] Done adding Catch with return of Promise --- Promise.cs | 27 +++++++++++++++++++++++++++ Promise_NonGeneric.cs | 20 ++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/Promise.cs b/Promise.cs index 5395223..5c45f1d 100644 --- a/Promise.cs +++ b/Promise.cs @@ -51,6 +51,8 @@ public interface IPromise /// IPromise Catch(Func onRejected); + IPromise Catch(Func> onRejected); + /// /// Add a resolved callback that chains a value promise (optionally converting to a different value type). /// @@ -580,6 +582,31 @@ public IPromise Catch(Func onRejected) return resultPromise; } + + public IPromise Catch(Func> onRejected) { + var resultPromise = new Promise(); + resultPromise.WithName(Name); + + Action rejectHandler = ex => + { + try + { + onRejected(ex) + .Then( + chainedValue => resultPromise.Resolve(chainedValue), + callbackEx => resultPromise.Reject(callbackEx) + ); + } + catch (Exception callbackEx) + { + resultPromise.Reject(callbackEx); + } + }; + + InvokeHandler(rejectHandler, this, rejectionException); + + return resultPromise; + } /// /// Add a resolved callback that chains a value promise (optionally converting to a different value type). diff --git a/Promise_NonGeneric.cs b/Promise_NonGeneric.cs index 6f5b2aa..7d947f5 100644 --- a/Promise_NonGeneric.cs +++ b/Promise_NonGeneric.cs @@ -46,6 +46,8 @@ public interface IPromise /// IPromise Catch(Action onRejected); + IPromise Catch(Func> onRejected); + /// /// Add a resolved callback that chains a value promise (optionally converting to a different value type). /// @@ -671,6 +673,24 @@ public IPromise Catch(Action onRejected) return resultPromise; } + + public IPromise Catch(Func> onRejected) { + var resultPromise = new Promise(); + resultPromise.WithName(Name); + + Action rejectHandler = ex => + { + onRejected(ex) + .Then( + chainedValue => resultPromise.Resolve(chainedValue), + callbackEx => resultPromise.Reject(callbackEx) + ); + }; + + InvokeRejectHandler(rejectHandler, this, rejectionException); + + return resultPromise; + } /// /// Add a resolved callback that chains a value promise (optionally converting to a different value type). From 16d9cfbc3825c8e082e460d9a44d3783f802eb71 Mon Sep 17 00:00:00 2001 From: Mike Oliva Date: Wed, 13 Jun 2018 17:39:07 +0200 Subject: [PATCH 2/4] Change new Catch Method --- Promise.cs | 31 +++++++++++++++++++------------ Promise_NonGeneric.cs | 22 ---------------------- 2 files changed, 19 insertions(+), 34 deletions(-) diff --git a/Promise.cs b/Promise.cs index 5c45f1d..71a59c6 100644 --- a/Promise.cs +++ b/Promise.cs @@ -51,7 +51,10 @@ public interface IPromise /// IPromise Catch(Func onRejected); - IPromise Catch(Func> onRejected); + /// + /// Handle errors for the promise. + /// + IPromise Catch(Func> onRejected); /// /// Add a resolved callback that chains a value promise (optionally converting to a different value type). @@ -583,27 +586,31 @@ public IPromise Catch(Func onRejected) return resultPromise; } - public IPromise Catch(Func> onRejected) { - var resultPromise = new Promise(); + /// + /// Handle errors for the promise. + /// + public IPromise Catch(Func> onRejected) { + var resultPromise = new Promise(); resultPromise.WithName(Name); + Action resolveHandler = v => resultPromise.Resolve(v); + Action rejectHandler = ex => { - try - { + try { onRejected(ex) - .Then( - chainedValue => resultPromise.Resolve(chainedValue), - callbackEx => resultPromise.Reject(callbackEx) - ); + .Progress(progress => resultPromise.ReportProgress(progress)) + .Then(resolve => resultPromise.Resolve(resolve)) + .Catch(reject => resultPromise.Reject(ex)); } - catch (Exception callbackEx) + catch (Exception cbEx) { - resultPromise.Reject(callbackEx); + resultPromise.Reject(cbEx); } }; - InvokeHandler(rejectHandler, this, rejectionException); + ActionHandlers(resultPromise, resolveHandler, rejectHandler); + ProgressHandlers(resultPromise, v => resultPromise.ReportProgress(v)); return resultPromise; } diff --git a/Promise_NonGeneric.cs b/Promise_NonGeneric.cs index 7d947f5..d5a19fe 100644 --- a/Promise_NonGeneric.cs +++ b/Promise_NonGeneric.cs @@ -46,8 +46,6 @@ public interface IPromise /// IPromise Catch(Action onRejected); - IPromise Catch(Func> onRejected); - /// /// Add a resolved callback that chains a value promise (optionally converting to a different value type). /// @@ -648,8 +646,6 @@ public IPromise WithName(string name) /// public IPromise Catch(Action onRejected) { -// Argument.NotNull(() => onRejected); - var resultPromise = new Promise(); resultPromise.WithName(Name); @@ -673,24 +669,6 @@ public IPromise Catch(Action onRejected) return resultPromise; } - - public IPromise Catch(Func> onRejected) { - var resultPromise = new Promise(); - resultPromise.WithName(Name); - - Action rejectHandler = ex => - { - onRejected(ex) - .Then( - chainedValue => resultPromise.Resolve(chainedValue), - callbackEx => resultPromise.Reject(callbackEx) - ); - }; - - InvokeRejectHandler(rejectHandler, this, rejectionException); - - return resultPromise; - } /// /// Add a resolved callback that chains a value promise (optionally converting to a different value type). From 04f7d762b3a8e7686b9b836c6ef7c07ebfa61d80 Mon Sep 17 00:00:00 2001 From: Mike Oliva Date: Wed, 13 Jun 2018 17:47:39 +0200 Subject: [PATCH 3/4] Add new tests --- Tests/PromiseTests.cs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/Tests/PromiseTests.cs b/Tests/PromiseTests.cs index 99c12a7..d77344e 100644 --- a/Tests/PromiseTests.cs +++ b/Tests/PromiseTests.cs @@ -1399,5 +1399,35 @@ public void rejected_reject_callback_is_caught_by_chained_catch() Assert.Equal(expectedException, actualException); } + + [Fact] + public void resolved_callback_is_caught_by_catch_returning_promise() { + var promise = new Promise(); + int excepectedValue = 1; + int actualValue = 0; + + promise.Catch(err => Promise.Resolved(-1)) + .Then(result => { actualValue = result; }) + .Catch(err => throw new Exception("Should not happend.")); + + promise.Resolve(1); + + Assert.Equal(excepectedValue, actualValue); + } + + [Fact] + public void rejected_callback_is_caught_by_catch_returning_promise() { + var promise = new Promise(); + int excepectedValue = -1; + int actualValue = 0; + + promise.Catch(err => Promise.Rejected(new Exception())) + .Then(result => throw new Exception("Should not happend.")) + .Catch(err => actualValue = -1); + + promise.Resolve(1); + + Assert.Equal(excepectedValue, actualValue); + } } } From cf07808077969e8920e0a37913a4c05353da25db Mon Sep 17 00:00:00 2001 From: Mike Oliva Date: Thu, 14 Jun 2018 09:46:25 +0200 Subject: [PATCH 4/4] Fix Promise Test --- Tests/PromiseTests.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Tests/PromiseTests.cs b/Tests/PromiseTests.cs index d77344e..3401970 100644 --- a/Tests/PromiseTests.cs +++ b/Tests/PromiseTests.cs @@ -202,7 +202,10 @@ public void error_handler_is_not_invoked_for_resolved_promised() { var promise = new Promise(); - promise.Catch(e => throw new Exception("This shouldn't happen")); + promise.Catch(e => { + throw new Exception("This shouldn't happen"); + return -1; + }); promise.Resolve(5); } @@ -734,7 +737,6 @@ public void can_chain_promise_and_convert_type_of_value() .Then(v => { Assert.Equal(chainedPromiseValue, v); - ++completed; });