Skip to content

Commit dcaa3ae

Browse files
committed
Consolidate error_handler.ts
1 parent 3919c58 commit dcaa3ae

File tree

10 files changed

+53
-383
lines changed

10 files changed

+53
-383
lines changed

packages/cli-core/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"@inquirer/prompts": "^3.0.0",
2424
"execa": "^9.5.1",
2525
"kleur": "^4.1.5",
26-
"zod": "^3.22.2"
26+
"zod": "^3.22.2",
27+
"yargs": "^17.7.2"
2728
}
2829
}

packages/cli/src/error_handler.test.ts renamed to packages/cli-core/src/error_handler.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import {
44
generateCommandFailureHandler,
55
} from './error_handler.js';
66
import { Argv } from 'yargs';
7-
import { LogLevel, printer } from '@aws-amplify/cli-core';
7+
import { LogLevel } from './printer/printer.js';
8+
import { printer } from './printer.js';
89
import assert from 'node:assert';
910
import { AmplifyUserError, UsageDataEmitter } from '@aws-amplify/platform-core';
1011

packages/cli/src/error_handler.ts renamed to packages/cli-core/src/error_handler.ts

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { LogLevel, format, printer } from '@aws-amplify/cli-core';
1+
import { LogLevel } from './printer/printer.js';
2+
import { format } from './format/format.js';
3+
import { printer } from './printer.js';
4+
25
import { Argv } from 'yargs';
36
import { AmplifyError, UsageDataEmitter } from '@aws-amplify/platform-core';
47
import { extractSubCommands } from './extract_sub_commands.js';
@@ -17,7 +20,7 @@ type HandleErrorProps = {
1720
* Attaches process listeners to handle unhandled exceptions and rejections
1821
*/
1922
export const attachUnhandledExceptionListeners = (
20-
usageDataEmitter: UsageDataEmitter
23+
usageDataEmitter?: UsageDataEmitter
2124
): void => {
2225
if (hasAttachUnhandledExceptionListenersBeenCalled) {
2326
return;
@@ -54,7 +57,7 @@ export const attachUnhandledExceptionListeners = (
5457
* This prevents our top-level error handler from being invoked after the yargs error handler has already been invoked
5558
*/
5659
export const generateCommandFailureHandler = (
57-
parser: Argv,
60+
parser?: Argv,
5861
usageDataEmitter?: UsageDataEmitter
5962
): ((message: string, error: Error) => Promise<void>) => {
6063
/**
@@ -63,19 +66,30 @@ export const generateCommandFailureHandler = (
6366
* @param error error thrown by yargs handler
6467
*/
6568
const handleCommandFailure = async (message: string, error?: Error) => {
66-
const printHelp = () => {
67-
printer.printNewLine();
68-
parser.showHelp();
69-
printer.printNewLine();
70-
};
71-
await handleErrorSafe({
72-
command: extractSubCommands(parser),
73-
printMessagePreamble: printHelp,
74-
error,
75-
message,
76-
usageDataEmitter,
77-
});
78-
parser.exit(1, error || new Error(message));
69+
// create-amplify command
70+
if (!parser) {
71+
await handleErrorSafe({
72+
error,
73+
message,
74+
});
75+
}
76+
77+
// for ampx commands
78+
if (parser) {
79+
const printHelp = () => {
80+
printer.printNewLine();
81+
parser.showHelp();
82+
printer.printNewLine();
83+
};
84+
await handleErrorSafe({
85+
command: extractSubCommands(parser),
86+
printMessagePreamble: printHelp,
87+
error,
88+
message,
89+
usageDataEmitter,
90+
});
91+
parser.exit(1, error || new Error(message));
92+
}
7993
};
8094
return handleCommandFailure;
8195
};
File renamed without changes.

packages/cli-core/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ export * from './printer/printer.js';
33
export * from './printer.js';
44
export { ColorName, colorNames, format, Format } from './format/format.js';
55
export * from './package-manager-controller/package_manager_controller_factory.js';
6+
export * from './error_handler.js';
7+
export * from './extract_sub_commands.js';

packages/cli/src/ampx.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
#!/usr/bin/env node
22
import { createMainParser } from './main_parser_factory.js';
3-
import {
4-
attachUnhandledExceptionListeners,
5-
generateCommandFailureHandler,
6-
} from './error_handler.js';
7-
import { extractSubCommands } from './extract_sub_commands.js';
83
import {
94
AmplifyFault,
105
PackageJsonReader,
@@ -13,7 +8,13 @@ import {
138
import { fileURLToPath } from 'node:url';
149
import { verifyCommandName } from './verify_command_name.js';
1510
import { hideBin } from 'yargs/helpers';
16-
import { PackageManagerControllerFactory, format } from '@aws-amplify/cli-core';
11+
import {
12+
PackageManagerControllerFactory,
13+
attachUnhandledExceptionListeners,
14+
extractSubCommands,
15+
format,
16+
generateCommandFailureHandler,
17+
} from '@aws-amplify/cli-core';
1718

1819
const packageJson = new PackageJsonReader().read(
1920
fileURLToPath(new URL('../package.json', import.meta.url))

packages/cli/src/test-utils/command_runner.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { Argv } from 'yargs';
22
import { AsyncLocalStorage } from 'node:async_hooks';
33
import { UsageDataEmitter } from '@aws-amplify/platform-core';
4-
import { generateCommandFailureHandler } from '../error_handler.js';
5-
import { extractSubCommands } from '../extract_sub_commands.js';
4+
import {
5+
extractSubCommands,
6+
generateCommandFailureHandler,
7+
} from '@aws-amplify/cli-core';
68

79
class OutputInterceptor {
810
private output = '';

packages/create-amplify/src/create_amplify.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@
77
If customers have a cached version of the create-amplify package, they might execute that cached version even after we publish features and fixes to the package on npm.
88
*/
99

10-
import { PackageManagerControllerFactory, format } from '@aws-amplify/cli-core';
10+
import {
11+
PackageManagerControllerFactory,
12+
attachUnhandledExceptionListeners,
13+
format,
14+
generateCommandFailureHandler,
15+
} from '@aws-amplify/cli-core';
1116
import { ProjectRootValidator } from './project_root_validator.js';
1217
import { AmplifyProjectCreator } from './amplify_project_creator.js';
1318
import { getProjectRoot } from './get_project_root.js';
1419
import { GitIgnoreInitializer } from './gitignore_initializer.js';
1520
import { InitialProjectFileGenerator } from './initial_project_file_generator.js';
16-
import {
17-
attachUnhandledExceptionListeners,
18-
generateCommandFailureHandler,
19-
} from './error_handler.js';
2021

2122
attachUnhandledExceptionListeners();
2223
const errorHandler = generateCommandFailureHandler();

packages/create-amplify/src/error_handler.test.ts

Lines changed: 0 additions & 204 deletions
This file was deleted.

0 commit comments

Comments
 (0)