Skip to content

Commit f8bfc83

Browse files
authored
Merge pull request #59 from tabkram/fix/trace-handler
fix: improve @trace execution to correctly auto bind the class context to the handler
2 parents cc42b66 + 8e9a86e commit f8bfc83

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

src/execution/execute.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,19 @@ export function execute<INPUT extends unknown[], OUTPUT, RESPONSE = OUTPUT, ERRO
6262
)
6363
.catch((error) => (errorCallback ? (errorCallback.bind(this) as typeof errorCallback)(error, true) : error));
6464
} else {
65+
let result;
6566
try {
66-
const result = blockFunction.bind(this)(...inputs, ...extraInputs);
67-
if (result instanceof Promise) {
68-
return result
69-
.then((output: OUTPUT) =>
70-
successCallback ? (successCallback.bind(this) as typeof successCallback)(output, true) : (output as unknown as RESPONSE)
71-
)
72-
.catch((error) => (errorCallback ? (errorCallback.bind(this) as typeof errorCallback)(error, true) : error));
73-
}
74-
return successCallback ? (successCallback.bind(this) as typeof successCallback)(result, false) : (result as unknown as RESPONSE);
67+
result = blockFunction.bind(this)(...inputs, ...extraInputs);
7568
} catch (error) {
7669
return errorCallback ? (errorCallback.bind(this) as typeof errorCallback)(error, false) : error;
7770
}
71+
if (result instanceof Promise) {
72+
return result
73+
.then((output: OUTPUT) =>
74+
successCallback ? (successCallback.bind(this) as typeof successCallback)(output, true) : (output as unknown as RESPONSE)
75+
)
76+
.catch((error) => (errorCallback ? (errorCallback.bind(this) as typeof errorCallback)(error, true) : error));
77+
}
78+
return successCallback ? (successCallback.bind(this) as typeof successCallback)(result, false) : (result as unknown as RESPONSE);
7879
}
7980
}

src/trace/traceDecorator.spec.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ describe('trace decorator', () => {
2222

2323
describe('Tracing synchronous functions', () => {
2424
const traceHandlerMock = jest.fn();
25+
const traceHandlerMockThrows = jest.fn();
2526

2627
class SyncClass {
2728
greetingFrom = ['hello from class'];
@@ -35,11 +36,21 @@ describe('trace decorator', () => {
3536
this.greetingFrom.push('hello from method');
3637
return 'Hello World';
3738
}
39+
40+
@trace((...args) => {
41+
traceHandlerMockThrows(...args);
42+
throw new Error('hello but I throw!');
43+
})
44+
helloWorldHandlerThrows(): string {
45+
this.greetingFrom.push('hello from method');
46+
return 'Hello World';
47+
}
3848
}
3949

4050
it('should trace a synchronous function and verify context', () => {
4151
const instance = new SyncClass();
4252
expect(instance.helloWorld()).toBe('Hello World');
53+
expect(traceHandlerMock).toHaveBeenCalledTimes(1);
4354
expect(traceHandlerMock).toHaveBeenCalledWith(
4455
expect.objectContaining({
4556
metadata: expect.objectContaining({
@@ -54,6 +65,25 @@ describe('trace decorator', () => {
5465
})
5566
);
5667
});
68+
69+
it('should trace a synchronous function and verify helloWorldHandlerThrows', () => {
70+
const instance = new SyncClass();
71+
expect(() => instance.helloWorldHandlerThrows()).toThrowError('hello but I throw!');
72+
expect(traceHandlerMockThrows).toHaveBeenCalledTimes(1);
73+
expect(traceHandlerMockThrows).toHaveBeenCalledWith(
74+
expect.objectContaining({
75+
metadata: expect.objectContaining({
76+
class: 'SyncClass',
77+
method: 'helloWorldHandlerThrows',
78+
name: 'helloWorldHandlerThrows',
79+
parameters: [],
80+
isAsync: false,
81+
isBound: false
82+
}),
83+
...successfulExecutionTraceExpectation
84+
})
85+
);
86+
});
5787
});
5888

5989
describe('Asynchronous functions', () => {
@@ -142,8 +172,8 @@ describe('trace decorator', () => {
142172
url: string,
143173
traceContext: Record<string, unknown> = {}
144174
): Promise<{
145-
data: string;
146-
}> {
175+
data: string;
176+
}> {
147177
return this.fetchDataFunction(url, traceContext);
148178
}
149179

0 commit comments

Comments
 (0)