Skip to content

Add overload of Catch that returns an IPromise or IPromise<PromisedT>… #109

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
62 changes: 61 additions & 1 deletion Tests/PromiseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ 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(onRejected: e => throw new Exception("This shouldn't happen"));

promise.Resolve(5);
}
Expand Down Expand Up @@ -1472,5 +1472,65 @@ public void rejected_reject_callback_is_caught_by_chained_catch()

Assert.Equal(expectedException, actualException);
}

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

promise.Catch(err => Promise<int>.Resolved(expectedValue))
.Then(result => { actualValue = result; });

promise.Reject(new Exception());

Assert.Equal(expectedValue, actualValue);
}

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

promise.Catch(err => Promise<int>.Rejected(new Exception()))
.Catch(err => actualValue = expectedValue);

promise.Reject(new Exception());

Assert.Equal(expectedValue, actualValue);
}

[Fact]
public void rejected_catch_returning_resolved_promise_state_is_adopted_nongeneric()
{
var promise = new Promise();
int expectedValue = 1;
int actualValue = 0;

promise.Catch(err => Promise.Resolved())
.Then(() => { actualValue = expectedValue; });

promise.Reject(new Exception());

Assert.Equal(expectedValue, actualValue);
}

[Fact]
public void rejected_catch_returning_rejected_promise_state_is_adopted_nongeneric()
{
var promise = new Promise();
int expectedValue = 1;
int actualValue = 0;

promise.Catch(err => Promise.Rejected(new Exception()))
.Catch(err => actualValue = expectedValue);

promise.Reject(new Exception());

Assert.Equal(expectedValue, actualValue);
}

Choose a reason for hiding this comment

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

You should add some tests to make sure the value passed into onResolved and onRejected are the same as the promise returned in the first catch.

}
}
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 and adopt status of returned promise..
/// </summary>
IPromise<PromisedT> Catch(Func<Exception, IPromise<PromisedT>> rejectionHandler);

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

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

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

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

Choose a reason for hiding this comment

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

resultPromise should be rejected with reject, not 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).
/// </summary>
Expand Down
31 changes: 31 additions & 0 deletions src/Promise_NonGeneric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public interface IPromise
/// </summary>
IPromise Catch(Action<Exception> onRejected);

/// <summary>
/// Handle errors for the promise and adopt status of returned promise..
/// </summary>
IPromise Catch(Func<Exception, IPromise> onRejected);

/// <summary>
/// Add a resolved callback that chains a value promise (optionally converting to a different value type).
/// </summary>
Expand Down Expand Up @@ -689,6 +694,32 @@ public IPromise Catch(Action<Exception> onRejected)
return resultPromise;
}

public IPromise Catch(Func<Exception, IPromise> onRejected) {
var resultPromise = new Promise();
resultPromise.WithName(Name);

Action resolveHandler = () => resultPromise.Resolve();

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

Choose a reason for hiding this comment

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

resultPromise should be rejected with reject, not 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).
/// </summary>
Expand Down