Skip to content

Commit 158d715

Browse files
committed
refactor(@angular/cli): improve error handling for package installation in ng add
This change enhances the error handling in the 'ng add' command's 'installPackageTask'. It now specifically catches 'PackageManagerError' exceptions and surfaces the standard output or standard error from the underlying package manager process. This provides users with more actionable information when a package installation fails.
1 parent 2cd4f2d commit 158d715

File tree

2 files changed

+42
-29
lines changed

2 files changed

+42
-29
lines changed

packages/angular/cli/src/commands/add/cli.ts

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
import {
2727
NgAddSaveDependency,
2828
PackageManager,
29+
PackageManagerError,
2930
PackageManifest,
3031
PackageMetadata,
3132
createPackageManager,
@@ -589,36 +590,47 @@ export default class AddCommandModule
589590
// Only show if installation will actually occur
590591
task.title = 'Installing package';
591592

592-
if (context.savePackage === false) {
593-
task.title += ' in temporary location';
594-
595-
// Temporary packages are located in a different directory
596-
// Hence we need to resolve them using the temp path
597-
const { workingDirectory } = await packageManager.acquireTempPackage(
598-
packageIdentifier.toString(),
599-
{
600-
registry,
601-
},
602-
);
603-
604-
const tempRequire = createRequire(workingDirectory + '/');
605-
assert(context.collectionName, 'Collection name should always be available');
606-
const resolvedCollectionPath = tempRequire.resolve(
607-
join(context.collectionName, 'package.json'),
608-
);
593+
try {
594+
if (context.savePackage === false) {
595+
task.title += ' in temporary location';
596+
597+
// Temporary packages are located in a different directory
598+
// Hence we need to resolve them using the temp path
599+
const { workingDirectory } = await packageManager.acquireTempPackage(
600+
packageIdentifier.toString(),
601+
{
602+
registry,
603+
},
604+
);
605+
606+
const tempRequire = createRequire(workingDirectory + '/');
607+
assert(context.collectionName, 'Collection name should always be available');
608+
const resolvedCollectionPath = tempRequire.resolve(
609+
join(context.collectionName, 'package.json'),
610+
);
611+
612+
context.collectionName = dirname(resolvedCollectionPath);
613+
} else {
614+
await packageManager.add(
615+
packageIdentifier.toString(),
616+
'none',
617+
savePackage !== 'dependencies',
618+
false,
619+
true,
620+
{
621+
registry,
622+
},
623+
);
624+
}
625+
} catch (e) {
626+
if (e instanceof PackageManagerError) {
627+
const output = e.stderr || e.stdout;
628+
if (output) {
629+
throw new CommandError(`Package installation failed: ${e.message}\nOutput: ${output}`);
630+
}
631+
}
609632

610-
context.collectionName = dirname(resolvedCollectionPath);
611-
} else {
612-
await packageManager.add(
613-
packageIdentifier.toString(),
614-
'none',
615-
savePackage !== 'dependencies',
616-
false,
617-
true,
618-
{
619-
registry,
620-
},
621-
);
633+
throw e;
622634
}
623635
}
624636

packages/angular/cli/src/package-managers/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
export { createPackageManager } from './factory';
1010
export type { PackageManagerName } from './package-manager-descriptor';
1111
export { PackageManager } from './package-manager';
12+
export { PackageManagerError } from './error';
1213
export type * from './package-metadata';
1314
export type { InstalledPackage } from './package-tree';

0 commit comments

Comments
 (0)