Skip to content

Done adding Catch with return of Promise #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions Tests/PromiseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,10 @@ public void error_handler_is_not_invoked_for_resolved_promised()
{
var promise = new Promise<int>();

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);
}
Expand Down Expand Up @@ -759,7 +762,6 @@ public void can_chain_promise_and_convert_type_of_value()
.Then(v =>
{
Assert.Equal(chainedPromiseValue, v);

++completed;
});

Expand Down Expand Up @@ -1424,5 +1426,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>();
int excepectedValue = 1;
int actualValue = 0;

promise.Catch(err => Promise<int>.Resolved(-1))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the promise is resolved, this catch block is never executed.

.Then(result => { actualValue = result; })
.Catch(err => throw new Exception("Should not happend."));

promise.Resolve(1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would make more sense here to make the first promise reject and verify that actualValue was set to -1, to verify that the value from a promise returned inside a Catch is coming through.


Assert.Equal(excepectedValue, actualValue);
}

[Fact]
public void rejected_callback_is_caught_by_catch_returning_promise() {
var promise = new Promise<int>();
int excepectedValue = -1;
int actualValue = 0;

promise.Catch(err => Promise<int>.Rejected(new Exception()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since promise is resolved (promise.Resolve(1) further down), this Catch block is never executed.

.Then(result => throw new Exception("Should not happend."))
.Catch(err => actualValue = -1);

promise.Resolve(1);

Assert.Equal(excepectedValue, actualValue);
}
}
}
34 changes: 34 additions & 0 deletions src/Promise.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public interface IPromise<PromisedT>
/// </summary>
IPromise<PromisedT> Catch(Func<Exception, PromisedT> onRejected);

/// <summary>
/// Handle errors for the promise.
/// </summary>
IPromise<PromisedT> Catch(Func<Exception, IPromise<PromisedT>> onRejected);

/// <summary>
/// Add a resolved callback that chains a value promise (optionally converting to a different value type).
/// </summary>
Expand Down Expand Up @@ -580,6 +585,35 @@ public IPromise<PromisedT> Catch(Func<Exception, PromisedT> onRejected)

return resultPromise;
}

/// <summary>
/// Handle errors for the promise.
/// </summary>
public IPromise<PromisedT> Catch(Func<Exception, IPromise<PromisedT>> onRejected) {
var resultPromise = new Promise<PromisedT>();
resultPromise.WithName(Name);

Action<PromisedT> resolveHandler = v => resultPromise.Resolve(v);

Action<Exception> rejectHandler = ex =>
{
try {
onRejected(ex)
.Progress(progress => resultPromise.ReportProgress(progress))
.Then(resolve => resultPromise.Resolve(resolve))
.Catch(reject => resultPromise.Reject(ex));
}
catch (Exception cbEx)
{
resultPromise.Reject(cbEx);
}
};

ActionHandlers(resultPromise, resolveHandler, rejectHandler);
ProgressHandlers(resultPromise, v => resultPromise.ReportProgress(v));

return resultPromise;
}

/// <summary>
/// Add a resolved callback that chains a value promise (optionally converting to a different value type).
Expand Down
2 changes: 0 additions & 2 deletions src/Promise_NonGeneric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -646,8 +646,6 @@ public IPromise WithName(string name)
/// </summary>
public IPromise Catch(Action<Exception> onRejected)
{
// Argument.NotNull(() => onRejected);

var resultPromise = new Promise();
resultPromise.WithName(Name);

Expand Down