Skip to content

Commit 583e3ae

Browse files
fix(parameters): preserve original stack trace on transform failures … (#3982)
Co-authored-by: Andrea Amorosi <[email protected]>
1 parent 3c38308 commit 583e3ae

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

packages/parameters/src/base/BaseProvider.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,19 @@ abstract class BaseProvider implements BaseProviderInterface {
167167
entryKey
168168
);
169169
} catch (error) {
170-
if (configs.throwOnTransformError)
171-
throw new TransformParameterError(
170+
if (configs.throwOnTransformError) {
171+
if (error instanceof TransformParameterError) {
172+
throw error;
173+
}
174+
175+
// Otherwise wrap—but preserve the original stack
176+
const wrapped = new TransformParameterError(
172177
configs.transform,
173178
(error as Error).message
174179
);
180+
wrapped.stack = (error as Error).stack;
181+
throw wrapped;
182+
}
175183
}
176184
}
177185
}

packages/parameters/tests/unit/BaseProvider.test.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ vi.mock('@aws-lambda-powertools/commons', async (importOriginal) => ({
2424
class TestProvider extends BaseProvider {
2525
public constructor() {
2626
super({
27-
proto: class {
27+
awsSdkV3ClientPrototype: class {
2828
#name = 'TestProvider';
2929

3030
public hello(): string {
@@ -389,7 +389,7 @@ describe('Class: BaseProvider', () => {
389389
});
390390
});
391391

392-
it('throws when called with a binary transform and throwOnTransformError equal to TRUE, and at least ONE the values is NOT a valid string representation of a binary', async () => {
392+
it('throws a TransformParameterError when trying to transform an invalid binary and throwOnTransformError is enabled', async () => {
393393
// Prepare
394394
const mockData = { A: 'qw' };
395395
const provider = new TestProvider();
@@ -404,6 +404,22 @@ describe('Class: BaseProvider', () => {
404404
).rejects.toThrowError(TransformParameterError);
405405
});
406406

407+
it('throws a TransformParameterError when a runtime error occurs during the transformation and throwOnTransformError is enabled', async () => {
408+
// Prepare
409+
const mockData = { A: 'foo' };
410+
const provider = new TestProvider();
411+
vi.spyOn(provider, '_getMultiple').mockResolvedValue(mockData);
412+
413+
// Act & Assess
414+
await expect(
415+
provider.getMultiple('my-path', {
416+
// @ts-ignore - we want to test an unexpected runtime error
417+
transform: 1,
418+
throwOnTransformError: true,
419+
})
420+
).rejects.toThrowError(TransformParameterError);
421+
});
422+
407423
it('returns an object with the transformed values when auto transform is used and the key of the parameter ends with `.binary`', async () => {
408424
// Prepare
409425
const mockData = { 'A.binary': toBase64(encoder.encode('my-value')) };

0 commit comments

Comments
 (0)