Skip to content

Commit 14ccc2f

Browse files
committed
chore: remove the captureError option
1 parent 6eedbbe commit 14ccc2f

File tree

4 files changed

+4
-129
lines changed

4 files changed

+4
-129
lines changed

Diff for: docs/core/tracer.md

-34
Original file line numberDiff line numberDiff line change
@@ -454,40 +454,6 @@ Use **`POWERTOOLS_TRACER_CAPTURE_ERROR=false`** environment variable to instruct
454454

455455
1. You might **return sensitive** information from exceptions, stack traces you might not control
456456

457-
### Disabling exception capture for targeted methods and handlers
458-
459-
Use the `captureError: false` option in both `tracer.captureLambdaHandler()` and `tracer.captureMethod()` decorators to instruct Tracer **not** to serialize exceptions as metadata.
460-
461-
=== "method.ts"
462-
463-
```typescript hl_lines="5"
464-
import { Tracer } from '@aws-lambda-powertools/tracer';
465-
466-
const tracer = new Tracer({ serviceName: 'serverlessAirline' });
467-
class MyThing {
468-
@tracer.captureMethod({ captureError: false })
469-
myMethod(): string {
470-
/* ... */
471-
return 'foo bar';
472-
}
473-
}
474-
```
475-
476-
=== "handler.ts"
477-
478-
```typescript hl_lines="6"
479-
import { Tracer } from '@aws-lambda-powertools/tracer';
480-
import { LambdaInterface } from '@aws-lambda-powertools/commons';
481-
482-
const tracer = new Tracer({ serviceName: 'serverlessAirline' });
483-
class MyHandler implements LambdaInterface {
484-
@tracer.captureLambdaHandler({ captureError: false })
485-
async handler(_event: any, _context: any): Promise<void> {
486-
/* ... */
487-
}
488-
}
489-
```
490-
491457
### Escape hatch mechanism
492458

493459
You can use `tracer.provider` attribute to access all methods provided by the [AWS X-Ray SDK](https://docs.aws.amazon.com/xray-sdk-for-nodejs/latest/reference/AWSXRay.html).

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

+2-15
Original file line numberDiff line numberDiff line change
@@ -370,13 +370,7 @@ class Tracer extends Utility implements TracerInterface {
370370
}
371371

372372
} catch (error) {
373-
if (options?.captureError ?? true) {
374-
tracerRef.addErrorAsMetadata(error as Error);
375-
} else {
376-
// Though we aren't adding the error as metadata, we should still
377-
// mark the segment as having an error.
378-
tracerRef.getSegment().addErrorFlag();
379-
}
373+
tracerRef.addErrorAsMetadata(error as Error);
380374
throw error;
381375
} finally {
382376
subsegment?.close();
@@ -449,14 +443,7 @@ class Tracer extends Utility implements TracerInterface {
449443
}
450444

451445
} catch (error) {
452-
if (options?.captureError ?? true) {
453-
tracerRef.addErrorAsMetadata(error as Error);
454-
} else {
455-
// Though we aren't adding the error as metadata, we should still
456-
// mark the segment as having an error.
457-
tracerRef.getSegment().addErrorFlag();
458-
}
459-
446+
tracerRef.addErrorAsMetadata(error as Error);
460447
throw error;
461448
} finally {
462449
subsegment?.close();

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type TracerOptions = {
3535
* const tracer = new Tracer();
3636
*
3737
* class MyThing {
38-
* @tracer.captureMethod({ captureResponse: false, captureError: false })
38+
* @tracer.captureMethod({ captureResponse: false })
3939
* myMethod(): string {
4040
* return 'foo bar';
4141
* }
@@ -44,7 +44,6 @@ type TracerOptions = {
4444
*/
4545
type TracerCaptureMethodOptions = {
4646
captureResponse?: boolean
47-
captureError?: boolean
4847
};
4948

5049
/**
@@ -56,14 +55,13 @@ type TracerCaptureMethodOptions = {
5655
* const tracer = new Tracer();
5756
*
5857
* class MyThing implements LambdaInterface {
59-
* @tracer.captureLambdaHandler({ captureResponse: false, captureError: false })
58+
* @tracer.captureLambdaHandler({ captureResponse: false })
6059
* async handler(_event: any, _context: any): Promise<void> {}
6160
* }
6261
* ```
6362
*/
6463
type TracerCaptureLambdaHandlerOptions = {
6564
captureResponse?: boolean
66-
captureError?: boolean
6765
};
6866

6967
type HandlerMethodDecorator = (

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

-76
Original file line numberDiff line numberDiff line change
@@ -757,41 +757,6 @@ describe('Class: Tracer', () => {
757757

758758
});
759759

760-
test('when used as decorator while captureError is set to false, it does not capture the exceptions', async () => {
761-
762-
// Prepare
763-
const tracer: Tracer = new Tracer();
764-
const newSubsegment: Segment | Subsegment | undefined = new Subsegment('## index.handler');
765-
jest.spyOn(tracer.provider, 'getSegment')
766-
.mockImplementation(() => newSubsegment);
767-
setContextMissingStrategy(() => null);
768-
const captureAsyncFuncSpy = jest.spyOn(tracer.provider, 'captureAsyncFunc');
769-
const addErrorSpy = jest.spyOn(newSubsegment, 'addError');
770-
const addErrorFlagSpy = jest.spyOn(newSubsegment, 'addErrorFlag');
771-
class Lambda implements LambdaInterface {
772-
773-
@tracer.captureLambdaHandler({ captureError: false })
774-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
775-
// @ts-ignore
776-
public handler<TEvent, TResult>(_event: TEvent, _context: Context, _callback: Callback<TResult>): void | Promise<TResult> {
777-
throw new Error('Exception thrown!');
778-
}
779-
780-
}
781-
782-
// Act & Assess
783-
await expect(new Lambda().handler({}, context, () => console.log('Lambda invoked!'))).rejects.toThrowError(Error);
784-
expect(captureAsyncFuncSpy).toHaveBeenCalledTimes(1);
785-
expect(newSubsegment).toEqual(expect.objectContaining({
786-
name: '## index.handler',
787-
}));
788-
expect('cause' in newSubsegment).toBe(false);
789-
expect(addErrorFlagSpy).toHaveBeenCalledTimes(1);
790-
expect(addErrorSpy).toHaveBeenCalledTimes(0);
791-
expect.assertions(6);
792-
793-
});
794-
795760
test('when used as decorator and with standard config, it captures the exception correctly', async () => {
796761

797762
// Prepare
@@ -1115,47 +1080,6 @@ describe('Class: Tracer', () => {
11151080

11161081
});
11171082

1118-
test('when used as decorator and with captureError set to false, it does not capture the exception', async () => {
1119-
1120-
// Prepare
1121-
const tracer: Tracer = new Tracer();
1122-
const newSubsegment: Segment | Subsegment | undefined = new Subsegment('### dummyMethod');
1123-
jest.spyOn(tracer.provider, 'getSegment')
1124-
.mockImplementation(() => newSubsegment);
1125-
setContextMissingStrategy(() => null);
1126-
const captureAsyncFuncSpy = createCaptureAsyncFuncMock(tracer.provider);
1127-
const addErrorSpy = jest.spyOn(newSubsegment, 'addError');
1128-
const addErrorFlagSpy = jest.spyOn(newSubsegment, 'addErrorFlag');
1129-
class Lambda implements LambdaInterface {
1130-
1131-
@tracer.captureMethod({ captureError: false })
1132-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
1133-
// @ts-ignore
1134-
public async dummyMethod(_some: string): Promise<string> {
1135-
throw new Error('Exception thrown!');
1136-
}
1137-
1138-
public async handler<TEvent, TResult>(_event: TEvent, _context: Context, _callback: Callback<TResult>): Promise<TResult> {
1139-
const result = await this.dummyMethod('foo bar');
1140-
1141-
return new Promise((resolve, _reject) => resolve(result as unknown as TResult));
1142-
}
1143-
1144-
}
1145-
1146-
// Act / Assess
1147-
await expect(new Lambda().handler({}, context, () => console.log('Lambda invoked!'))).rejects.toThrowError(Error);
1148-
expect(captureAsyncFuncSpy).toHaveBeenCalledTimes(1);
1149-
expect(newSubsegment).toEqual(expect.objectContaining({
1150-
name: '### dummyMethod',
1151-
}));
1152-
expect('cause' in newSubsegment).toBe(false);
1153-
expect(addErrorFlagSpy).toHaveBeenCalledTimes(1);
1154-
expect(addErrorSpy).toHaveBeenCalledTimes(0);
1155-
expect.assertions(6);
1156-
1157-
});
1158-
11591083
test('when used as decorator and when calling other methods/props in the class they are called in the orginal scope', async () => {
11601084

11611085
// Prepare

0 commit comments

Comments
 (0)