Skip to content

Commit fb85238

Browse files
authored
fix(tracer): capture method throws errors correctly (#1016)
* wip * wip * fix: captureMethod now throws * chore: removed leftover comment
1 parent 1cbb4db commit fb85238

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

Diff for: packages/tracer/src/Tracer.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -421,17 +421,18 @@ class Tracer extends Utility implements TracerInterface {
421421
return originalMethod.apply(target, [...args]);
422422
}
423423

424-
return this.provider.captureAsyncFunc(`### ${originalMethod.name}`, async subsegment => {
424+
return this.provider.captureAsyncFunc(`### ${originalMethod.name}`, async subsegment => {
425425
let result;
426426
try {
427427
result = await originalMethod.apply(this, [...args]);
428428
this.addResponseAsMetadata(result, originalMethod.name);
429429
} catch (error) {
430-
this.addErrorAsMetadata(error as Error);
431-
// TODO: should this error be thrown?? If thrown we get a ERR_UNHANDLED_REJECTION. If not aren't we are basically catching a Customer error?
432-
// throw error;
430+
this.addErrorAsMetadata(error as Error);
431+
432+
throw error;
433433
} finally {
434434
subsegment?.close();
435+
subsegment?.flush();
435436
}
436437

437438
return result;

Diff for: packages/tracer/tests/unit/Tracer.test.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ interface LambdaInterface {
1717
type CaptureAsyncFuncMock = jest.SpyInstance<unknown, [name: string, fcn: (subsegment?: Subsegment) => unknown, parent?: Segment | Subsegment]>;
1818
const createCaptureAsyncFuncMock = function(provider: ProviderServiceInterface): CaptureAsyncFuncMock {
1919
return jest.spyOn(provider, 'captureAsyncFunc')
20-
.mockImplementation((methodName, callBackFn) => {
20+
.mockImplementation(async (methodName, callBackFn) => {
2121
const subsegment = new Subsegment(`### ${methodName}`);
2222
jest.spyOn(subsegment, 'flush').mockImplementation(() => null);
23-
callBackFn(subsegment);
23+
await callBackFn(subsegment);
2424
});
2525
};
2626

@@ -865,7 +865,6 @@ describe('Class: Tracer', () => {
865865
const captureAsyncFuncSpy = jest.spyOn(tracer.provider, 'captureAsyncFunc');
866866
class Lambda implements LambdaInterface {
867867

868-
// TODO: revisit return type & make it more specific
869868
@tracer.captureMethod()
870869
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
871870
// @ts-ignore
@@ -895,6 +894,7 @@ describe('Class: Tracer', () => {
895894
// Prepare
896895
const tracer: Tracer = new Tracer();
897896
const newSubsegment: Segment | Subsegment | undefined = new Subsegment('### dummyMethod');
897+
jest.spyOn(newSubsegment, 'flush').mockImplementation(() => null);
898898
jest.spyOn(tracer.provider, 'getSegment')
899899
.mockImplementation(() => newSubsegment);
900900
setContextMissingStrategy(() => null);
@@ -960,17 +960,16 @@ describe('Class: Tracer', () => {
960960

961961
}
962962

963-
// Act
964-
await new Lambda().handler(event, context, () => console.log('Lambda invoked!'));
965-
966-
// Assess
963+
// Act / Assess
964+
await expect(new Lambda().handler({}, context, () => console.log('Lambda invoked!'))).rejects.toThrowError(Error);
967965
expect(captureAsyncFuncSpy).toHaveBeenCalledTimes(1);
968966
expect(newSubsegment).toEqual(expect.objectContaining({
969967
name: '### dummyMethod',
970968
}));
971969
expect('cause' in newSubsegment).toBe(true);
972970
expect(addErrorSpy).toHaveBeenCalledTimes(1);
973971
expect(addErrorSpy).toHaveBeenCalledWith(new Error('Exception thrown!'), false);
972+
expect.assertions(6);
974973

975974
});
976975

0 commit comments

Comments
 (0)