Skip to content

fix(parameters): preserve original stack trace on transform failures … #3982

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 10 additions & 2 deletions packages/parameters/src/base/BaseProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,19 @@ abstract class BaseProvider implements BaseProviderInterface {
entryKey
);
} catch (error) {
if (configs.throwOnTransformError)
throw new TransformParameterError(
if (configs.throwOnTransformError) {
if (error instanceof TransformParameterError) {
throw error;
}

// Otherwise wrap—but preserve the original stack
const wrapped = new TransformParameterError(
configs.transform,
(error as Error).message
);
wrapped.stack = (error as Error).stack;
throw wrapped;
}
}
}
}
Expand Down
20 changes: 18 additions & 2 deletions packages/parameters/tests/unit/BaseProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ vi.mock('@aws-lambda-powertools/commons', async (importOriginal) => ({
class TestProvider extends BaseProvider {
public constructor() {
super({
proto: class {
awsSdkV3ClientPrototype: class {
#name = 'TestProvider';

public hello(): string {
Expand Down Expand Up @@ -389,7 +389,7 @@ describe('Class: BaseProvider', () => {
});
});

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 () => {
it('throws a TransformParameterError when trying to transform an invalid binary and throwOnTransformError is enabled', async () => {
// Prepare
const mockData = { A: 'qw' };
const provider = new TestProvider();
Expand All @@ -404,6 +404,22 @@ describe('Class: BaseProvider', () => {
).rejects.toThrowError(TransformParameterError);
});

it('throws a TransformParameterError when a runtime error occurs during the transformation and throwOnTransformError is enabled', async () => {
// Prepare
const mockData = { A: 'foo' };
const provider = new TestProvider();
vi.spyOn(provider, '_getMultiple').mockResolvedValue(mockData);

// Act & Assess
await expect(
provider.getMultiple('my-path', {
// @ts-ignore - we want to test an unexpected runtime error
transform: 1,
throwOnTransformError: true,
})
).rejects.toThrowError(TransformParameterError);
});

it('returns an object with the transformed values when auto transform is used and the key of the parameter ends with `.binary`', async () => {
// Prepare
const mockData = { 'A.binary': toBase64(encoder.encode('my-value')) };
Expand Down