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

Handling uncaughtException in create-amplify command #2364

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
5 changes: 5 additions & 0 deletions .changeset/happy-students-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'create-amplify': patch
---

add process handler to gracefully exit when Ctrl + C. See Issue #825.
3 changes: 2 additions & 1 deletion packages/cli-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@inquirer/prompts": "^3.0.0",
"execa": "^9.5.1",
"kleur": "^4.1.5",
"zod": "^3.22.2"
"zod": "^3.22.2",
"yargs": "^17.7.2"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
generateCommandFailureHandler,
} from './error_handler.js';
import { Argv } from 'yargs';
import { LogLevel, printer } from '@aws-amplify/cli-core';
import { LogLevel } from './printer/printer.js';
import { printer } from './printer.js';
import assert from 'node:assert';
import { AmplifyUserError, UsageDataEmitter } from '@aws-amplify/platform-core';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { LogLevel, format, printer } from '@aws-amplify/cli-core';
import { LogLevel } from './printer/printer.js';
import { format } from './format/format.js';
import { printer } from './printer.js';

import { Argv } from 'yargs';
import { AmplifyError, UsageDataEmitter } from '@aws-amplify/platform-core';
import { extractSubCommands } from './extract_sub_commands.js';
Expand All @@ -17,7 +20,7 @@ type HandleErrorProps = {
* Attaches process listeners to handle unhandled exceptions and rejections
*/
export const attachUnhandledExceptionListeners = (
usageDataEmitter: UsageDataEmitter
usageDataEmitter?: UsageDataEmitter
): void => {
if (hasAttachUnhandledExceptionListenersBeenCalled) {
return;
Expand Down Expand Up @@ -54,7 +57,7 @@ export const attachUnhandledExceptionListeners = (
* This prevents our top-level error handler from being invoked after the yargs error handler has already been invoked
*/
export const generateCommandFailureHandler = (
parser: Argv,
parser?: Argv,
usageDataEmitter?: UsageDataEmitter
): ((message: string, error: Error) => Promise<void>) => {
/**
Expand All @@ -63,19 +66,30 @@ export const generateCommandFailureHandler = (
* @param error error thrown by yargs handler
*/
const handleCommandFailure = async (message: string, error?: Error) => {
const printHelp = () => {
printer.printNewLine();
parser.showHelp();
printer.printNewLine();
};
await handleErrorSafe({
command: extractSubCommands(parser),
printMessagePreamble: printHelp,
error,
message,
usageDataEmitter,
});
parser.exit(1, error || new Error(message));
// create-amplify command
if (!parser) {
await handleErrorSafe({
error,
message,
});
}

// for ampx commands
if (parser) {
const printHelp = () => {
printer.printNewLine();
parser.showHelp();
printer.printNewLine();
};
await handleErrorSafe({
command: extractSubCommands(parser),
printMessagePreamble: printHelp,
error,
message,
usageDataEmitter,
});
parser.exit(1, error || new Error(message));
}
};
return handleCommandFailure;
};
Expand Down
2 changes: 2 additions & 0 deletions packages/cli-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ export * from './printer/printer.js';
export * from './printer.js';
export { ColorName, colorNames, format, Format } from './format/format.js';
export * from './package-manager-controller/package_manager_controller_factory.js';
export * from './error_handler.js';
export * from './extract_sub_commands.js';
13 changes: 7 additions & 6 deletions packages/cli/src/ampx.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
#!/usr/bin/env node
import { createMainParser } from './main_parser_factory.js';
import {
attachUnhandledExceptionListeners,
generateCommandFailureHandler,
} from './error_handler.js';
import { extractSubCommands } from './extract_sub_commands.js';
import {
AmplifyFault,
PackageJsonReader,
Expand All @@ -13,7 +8,13 @@ import {
import { fileURLToPath } from 'node:url';
import { verifyCommandName } from './verify_command_name.js';
import { hideBin } from 'yargs/helpers';
import { PackageManagerControllerFactory, format } from '@aws-amplify/cli-core';
import {
PackageManagerControllerFactory,
attachUnhandledExceptionListeners,
extractSubCommands,
format,
generateCommandFailureHandler,
} from '@aws-amplify/cli-core';

const packageJson = new PackageJsonReader().read(
fileURLToPath(new URL('../package.json', import.meta.url))
Expand Down
6 changes: 4 additions & 2 deletions packages/cli/src/test-utils/command_runner.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Argv } from 'yargs';
import { AsyncLocalStorage } from 'node:async_hooks';
import { UsageDataEmitter } from '@aws-amplify/platform-core';
import { generateCommandFailureHandler } from '../error_handler.js';
import { extractSubCommands } from '../extract_sub_commands.js';
import {
extractSubCommands,
generateCommandFailureHandler,
} from '@aws-amplify/cli-core';

class OutputInterceptor {
private output = '';
Expand Down
2 changes: 1 addition & 1 deletion packages/client-config/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ export type CustomClientConfig = {
export const DEFAULT_CLIENT_CONFIG_VERSION: ClientConfigVersion;

// @public
export const generateClientConfig: <T extends "1" | "1.1" | "1.2" | "1.3" | "0">(backendIdentifier: DeployedBackendIdentifier, version: T, awsClientProvider?: AWSClientProvider<{
export const generateClientConfig: <T extends "1.3" | "1.2" | "1.1" | "1" | "0">(backendIdentifier: DeployedBackendIdentifier, version: T, awsClientProvider?: AWSClientProvider<{
getS3Client: S3Client;
getAmplifyClient: AmplifyClient;
getCloudFormationClient: CloudFormationClient;
Expand Down
12 changes: 8 additions & 4 deletions packages/create-amplify/src/create_amplify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@
*/

import {
LogLevel,
PackageManagerControllerFactory,
attachUnhandledExceptionListeners,
format,
printer,
generateCommandFailureHandler,
} from '@aws-amplify/cli-core';
import { ProjectRootValidator } from './project_root_validator.js';
import { AmplifyProjectCreator } from './amplify_project_creator.js';
import { getProjectRoot } from './get_project_root.js';
import { GitIgnoreInitializer } from './gitignore_initializer.js';
import { InitialProjectFileGenerator } from './initial_project_file_generator.js';

attachUnhandledExceptionListeners();
const errorHandler = generateCommandFailureHandler();

const projectRoot = await getProjectRoot();

const packageManagerControllerFactory = new PackageManagerControllerFactory(
Expand All @@ -39,6 +42,7 @@ const amplifyProjectCreator = new AmplifyProjectCreator(
try {
await amplifyProjectCreator.create();
} catch (err) {
printer.log(format.error(err), LogLevel.ERROR);
process.exitCode = 1;
if (err instanceof Error) {
await errorHandler(format.error(err), err);
}
}