Skip to content

fix(tracer): capture method throws errors correctly #1016

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

Merged
merged 4 commits into from
Jul 13, 2022
Merged
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
9 changes: 5 additions & 4 deletions packages/tracer/src/Tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,17 +421,18 @@ class Tracer extends Utility implements TracerInterface {
return originalMethod.apply(target, [...args]);
}

return this.provider.captureAsyncFunc(`### ${originalMethod.name}`, async subsegment => {
return this.provider.captureAsyncFunc(`### ${originalMethod.name}`, async subsegment => {
let result;
try {
result = await originalMethod.apply(this, [...args]);
this.addResponseAsMetadata(result, originalMethod.name);
} catch (error) {
this.addErrorAsMetadata(error as Error);
// 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?
// throw error;
this.addErrorAsMetadata(error as Error);

throw error;
} finally {
subsegment?.close();
subsegment?.flush();
}

return result;
Expand Down
13 changes: 6 additions & 7 deletions packages/tracer/tests/unit/Tracer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ interface LambdaInterface {
type CaptureAsyncFuncMock = jest.SpyInstance<unknown, [name: string, fcn: (subsegment?: Subsegment) => unknown, parent?: Segment | Subsegment]>;
const createCaptureAsyncFuncMock = function(provider: ProviderServiceInterface): CaptureAsyncFuncMock {
return jest.spyOn(provider, 'captureAsyncFunc')
.mockImplementation((methodName, callBackFn) => {
.mockImplementation(async (methodName, callBackFn) => {
const subsegment = new Subsegment(`### ${methodName}`);
jest.spyOn(subsegment, 'flush').mockImplementation(() => null);
callBackFn(subsegment);
await callBackFn(subsegment);
});
};

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

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

}

// Act
await new Lambda().handler(event, context, () => console.log('Lambda invoked!'));

// Assess
// Act / Assess
await expect(new Lambda().handler({}, context, () => console.log('Lambda invoked!'))).rejects.toThrowError(Error);
expect(captureAsyncFuncSpy).toHaveBeenCalledTimes(1);
expect(newSubsegment).toEqual(expect.objectContaining({
name: '### dummyMethod',
}));
expect('cause' in newSubsegment).toBe(true);
expect(addErrorSpy).toHaveBeenCalledTimes(1);
expect(addErrorSpy).toHaveBeenCalledWith(new Error('Exception thrown!'), false);
expect.assertions(6);

});

Expand Down