Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/add on exception join point (#4)
* feat(advices): Add onException joinPoint The joinPoint will trigger after all "beforeMethod" advices, preventing the original method to trigger because is triggered by itself. For now, this takes into account that only one @onException will be used per method Closes #2 * build: ensure remove compiled folder * onException should have access to the captured exception * no need for a flag $$exception * Update IAdviceParamInjector.ts * save exception and group up for async afters * Update decorators.ts * add exception prop * added $$error advice container * pass $$error container value to CallStackIterator
- Loading branch information
1 parent
7f6865c
commit 36c55a9
Showing
8 changed files
with
115 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,5 +7,6 @@ export { | |
adviceParam, | ||
afterInstance, | ||
beforeInstance, | ||
beforeMethod | ||
beforeMethod, | ||
onException | ||
} from "./decorators" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,71 @@ | ||
import { AdvicePool, IMetadata, beforeMethod, adviceMetadata } from "../../src/kaop-ts" | ||
|
||
class MyAdvicePool extends AdvicePool { | ||
static onException (@adviceMetadata meta: IMetadata) { | ||
this.stop() | ||
try { | ||
meta.result = meta.rawMethod.apply(meta.scope, meta.args) | ||
} catch (err) { | ||
console.log(`There was an error in ${meta.propertyKey}(): -> ${err.message}`) | ||
import { AdvicePool, IMetadata, beforeMethod, adviceMetadata, adviceParam, onException } from "../../src/kaop-ts" | ||
|
||
describe("kaop-ts demo -> onException join point", () => { | ||
|
||
let exceptionSpy = jest.fn() | ||
let noopSpy = jest.fn() | ||
let methodSpy = jest.fn() | ||
|
||
let orderArr = [] | ||
let capturedException = null | ||
|
||
class MyAdvicePool extends AdvicePool { | ||
static handleException (@adviceMetadata meta: IMetadata, @adviceParam(0) order) { | ||
orderArr.push(order) | ||
capturedException = meta.exception | ||
exceptionSpy() | ||
} | ||
|
||
static noop (@adviceParam(0) order) { | ||
orderArr.push(order) | ||
noopSpy() | ||
} | ||
} | ||
} | ||
|
||
class ExceptionTest { | ||
class ExceptionTest { | ||
|
||
@onException(MyAdvicePool.handleException) | ||
static wrongMethod (callback: any) { | ||
callback() | ||
} | ||
|
||
@beforeMethod(MyAdvicePool.onException) | ||
// static wrongMethod (callback: number | Function) { | ||
static wrongMethod (callback: any) { | ||
callback() | ||
@beforeMethod(MyAdvicePool.noop, 0) | ||
@onException(MyAdvicePool.handleException, 1) | ||
@beforeMethod(MyAdvicePool.noop, 2) | ||
static orderTest (cb: any) { | ||
cb() | ||
} | ||
} | ||
} | ||
|
||
describe("kaop-ts demo -> exception join point", () => { | ||
it("advices are callback driven, advice stack will be executed when this.next is invoked", (done) => { | ||
beforeEach(() => { | ||
exceptionSpy.mockClear() | ||
methodSpy.mockClear() | ||
noopSpy.mockClear() | ||
orderArr = [] | ||
}) | ||
|
||
it("throws an exception and thus calls MyAdvicePool.handleException", () => { | ||
ExceptionTest.wrongMethod(2) | ||
ExceptionTest.wrongMethod(done) | ||
ExceptionTest.wrongMethod(methodSpy) | ||
|
||
expect(capturedException instanceof Error).toBe(true) | ||
expect(exceptionSpy).toHaveBeenCalledTimes(1) | ||
expect(methodSpy).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it("onException must be called after the last beforeMethod, regardless of the order", () => { | ||
ExceptionTest.orderTest(4) | ||
expect(orderArr).toEqual([0, 2, 1]) | ||
}) | ||
|
||
it("prevents the original function from triggering twice", () => { | ||
ExceptionTest.orderTest(() => { | ||
methodSpy() | ||
throw Error() | ||
}) | ||
|
||
expect(noopSpy).toHaveBeenCalledTimes(2) | ||
expect(exceptionSpy).toHaveBeenCalledTimes(1) | ||
expect(methodSpy).toHaveBeenCalledTimes(1) | ||
}) | ||
}) |