-
Notifications
You must be signed in to change notification settings - Fork 2
Fix issue with catching exceptions thrown by engage #50
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| coverage | ||
| node_modules | ||
| yarn-error.log | ||
| Future.js | ||
| Future.js | ||
| .envrc |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,14 +16,17 @@ describe("Future", () => { | |
| expect(resolveSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test("invokes reject when action throws exception", () => { | ||
| const actionSpy = jasmine.createSpy("futureAction").and.throwError("forced error"); | ||
| test("does not invoke reject when action throws exception", () => { | ||
| const actionSpy = jasmine.createSpy("futureAction").and.callFake(() => { | ||
| throw new Error("forced error"); | ||
| }); | ||
| const rejectSpy = jasmine.createSpy("rejectSpy"); | ||
| const resolveSpy = jasmine.createSpy("resolveSpy"); | ||
| new Future(actionSpy).engage(rejectSpy, resolveSpy); | ||
|
|
||
| expect(() => { | ||
| new Future(actionSpy).engage(rejectSpy, resolveSpy); | ||
| }).toThrowError("forced error"); | ||
| expect(actionSpy).toHaveBeenCalledWith(rejectSpy, resolveSpy); | ||
| expect(rejectSpy).toHaveBeenCalledWith(new Error("forced error")); | ||
| expect(rejectSpy).not.toHaveBeenCalled(); | ||
| expect(resolveSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
|
|
@@ -37,6 +40,69 @@ describe("Future", () => { | |
|
|
||
| expect(resolveSpy).toHaveBeenCalledWith("my value"); | ||
| }); | ||
| test("handleWith for current Future does not get run if errors above its scope throw", (done) => { | ||
| let mapCalledTimes = 0; | ||
| let handleWithCalledTimes = 0; | ||
| const action = Future.of(33) | ||
| .handleWith((e: Error) => { | ||
| // eslint-disable-next-line no-console | ||
| console.log(`failed an infallible future: ${e.message}`); | ||
| handleWithCalledTimes++; | ||
| return Future.of(-1); | ||
| }) | ||
| .map((r) => { | ||
| mapCalledTimes++; | ||
| return r; | ||
| }); | ||
|
|
||
| try { | ||
| action.engage( | ||
| (e) => { | ||
| throw e; | ||
| }, | ||
| (r) => { | ||
| throw new Error(`oh no, something went wrong after the future has run to completion on ${r}`); | ||
| } | ||
| ); | ||
| } catch (e) { | ||
| expect(handleWithCalledTimes).toBe(0); | ||
| expect(mapCalledTimes).toBe(1); | ||
| done(); | ||
| } | ||
| }); | ||
|
|
||
| test("does not get run if engages above this scope throw in the rejection", (done) => { | ||
| let mapCalledTimes = 0; | ||
| let handleWithCalledTimes = 0; | ||
| const expectedError = new Error("error message"); | ||
| let errorInHandleWith = null; | ||
| const action: Future<Error, number> = Future.reject(expectedError) | ||
| .handleWith((e): any => { | ||
| handleWithCalledTimes++; | ||
| errorInHandleWith = e; | ||
| return Future.of(-1); | ||
| }) | ||
| .map((r) => { | ||
| mapCalledTimes++; | ||
| return r; | ||
| }); | ||
|
|
||
| try { | ||
| action.engage( | ||
| (e) => { | ||
| throw e; | ||
| }, | ||
| (r) => { | ||
| throw new Error(`oh no, something went wrong after the future has run to completion on ${r}`); | ||
| } | ||
| ); | ||
| } catch (e) { | ||
| expect(handleWithCalledTimes).toBe(1); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I understand based on the description, where does this test show that it the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I changed the test to show that it's the error that came from the Future.reject |
||
| expect(errorInHandleWith).toBe(expectedError); | ||
| expect(mapCalledTimes).toBe(1); | ||
| done(); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| describe("toPromise", () => { | ||
|
|
@@ -909,5 +975,16 @@ describe("Future", () => { | |
|
|
||
| await expect(chainedPromiseFuture).rejects.toThrow("bad stuff"); | ||
| }); | ||
|
|
||
| test("catches exceptions if thrown in the map of the future", async () => { | ||
| const p = Promise.resolve(1); | ||
| const chainedPromiseFuture = p.then(() => | ||
| Future.of(1).map(() => { | ||
| throw new Error("bad stuff"); | ||
| }) | ||
| ); | ||
|
|
||
| await expect(chainedPromiseFuture).rejects.toThrow("bad stuff"); | ||
| }); | ||
| }); | ||
| }); | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| { | ||
| description = "FutureJS"; | ||
| inputs.flake-utils.url = "github:numtide/flake-utils"; | ||
|
|
||
| outputs = { self, nixpkgs, flake-utils }: | ||
| flake-utils.lib.eachDefaultSystem | ||
| (system: | ||
| let | ||
| pkgs = nixpkgs.legacyPackages.${system}; | ||
| in | ||
| { | ||
| devShell = pkgs.mkShell | ||
| { | ||
| buildInputs = | ||
| [ | ||
| pkgs.nodejs_20 | ||
| (pkgs.yarn.override { | ||
| nodejs = pkgs.nodejs_20; | ||
| }) | ||
| ]; | ||
| }; | ||
| }); | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.