Skip to content
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

fix: remove error incorrectly swallowed (fixes #13030) #13041

Merged
merged 7 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
21 changes: 7 additions & 14 deletions packages/amplify-category-auth/src/commands/auth/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,13 @@ export const run = async (context: $TSContext): Promise<void> => {
(resourceKey) => meta.auth[resourceKey].service === AmplifySupportedService.COGNITO,
);

try {
const resource = await amplify.removeResource(context, category, resourceName);
if (resource?.service === AmplifySupportedService.COGNITOUSERPOOLGROUPS) {
// update cli input here
const cliState = new AuthInputState(context, authResourceName[0]);
const cliInputPayload = cliState.getCLIInputPayload();
cliInputPayload.cognitoConfig.userPoolGroupList = [];
await cliState.saveCLIInputPayload(cliInputPayload);
}
} catch (err) {
printer.info(err.stack);
printer.error('There was an error removing the auth resource');
void context.usageData.emitError(err);
process.exitCode = 1;
const resource = await amplify.removeResource(context, category, resourceName);
if (resource?.service === AmplifySupportedService.COGNITOUSERPOOLGROUPS) {
// update cli input here
const cliState = new AuthInputState(context, authResourceName[0]);
const cliInputPayload = cliState.getCLIInputPayload();
cliInputPayload.cognitoConfig.userPoolGroupList = [];
await cliState.saveCLIInputPayload(cliInputPayload);
}
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { stateManager, exitOnNextTick, ResourceDoesNotExistError } from '@aws-amplify/amplify-cli-core';
import { AmplifyError, stateManager, exitOnNextTick, ResourceDoesNotExistError } from '@aws-amplify/amplify-cli-core';
import { printer, prompter } from '@aws-amplify/amplify-prompts';
import * as inquirer from 'inquirer';
import * as path from 'path';
Expand Down Expand Up @@ -149,14 +149,21 @@ describe('remove-resource', () => {

it('print the deletion info when choose LambdaLayer', async () => {
prompterMock.pick.mockResolvedValueOnce('lambdaLayer1');
await expect(
removeResource(context as any, 'function', undefined, {

let error;
try {
await removeResource(context as any, 'function', undefined, {
serviceDeletionInfo: {
LambdaLayer: 'lambdaLayer deletion info message',
},
serviceSuffix: { Lambda: '(function)', LambdaLayer: '(layer)' },
}),
).rejects.toThrowError('An error occurred when removing the resources from the local directory');
});
} catch (e) {
error = e;
Fixed Show fixed Hide fixed
}
expect(error).toBeDefined();
expect(error instanceof AmplifyError).toBe(true);
expect(error.message).toBe('Resource cannot be removed because it has a dependency on another resource');

expect(prompterMock.pick).toBeCalledWith('Choose the resource you would want to remove', [
{
Expand Down Expand Up @@ -210,9 +217,15 @@ describe('remove-resource', () => {
});

it('throw an error when the dependent resources has a specified resource', async () => {
await expect(removeResource(context as any, 'function', 'lambdaLayer1')).rejects.toThrowError(
'An error occurred when removing the resources from the local directory',
);
let error;
try {
await removeResource(context as any, 'function', 'lambdaLayer1');
} catch (e) {
error = e;
Fixed Show fixed Hide fixed
}
expect(error).toBeDefined();
expect(error instanceof AmplifyError).toBe(true);
expect(error.message).toBe('Resource cannot be removed because it has a dependency on another resource');
});

it('print message to unlink the imported resource on confirm prompt when the specified service is imported resource', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
$TSContext,
AmplifyError,
AmplifyException,
AmplifyFault,
exitOnNextTick,
pathManager,
Expand Down Expand Up @@ -97,8 +98,11 @@ export async function removeResource(
}

try {
return await deleteResourceFiles(context, category, resourceName, resourceDir);
return deleteResourceFiles(context, category, resourceName, resourceDir);
jhockett marked this conversation as resolved.
Show resolved Hide resolved
} catch (err) {
if (err instanceof AmplifyException) {
throw err;
}
throw new AmplifyFault(
'ResourceRemoveFault',
{ message: 'An error occurred when removing the resources from the local directory' },
Expand Down