Repository navigation
fix(@angular/cli): propagate package manager errors when install fails - #34682
terencehonles wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request improves error reporting during package installation in the update command by formatting and displaying stdout and stderr when a PackageManagerError occurs. The review feedback recommends defensive programming to prevent potential TypeErrors if stdout or stderr are undefined, and suggests preserving diagnostic context for other types of errors by appending their messages.
| } catch (e) { | ||
| throw new CommandError('Unable to install packages'); | ||
| let message = 'Unable to install packages'; | ||
| if (e instanceof PackageManagerError) { | ||
| const stdout = e.stdout.trim(); | ||
| const stderr = e.stderr.trim(); | ||
| message += `: ${colors.bold(e.message)}${ | ||
| stdout ? `\n\n${colors.bold('[STDOUT]')}: ${stdout}` : '' | ||
| }${stderr ? `\n\n${colors.bold('[STDERR]')}: ${colors.red(stderr)}` : ''}`; | ||
| } | ||
|
|
||
| throw new CommandError(message); | ||
| } |
There was a problem hiding this comment.
-
Defensive Programming: If
e.stdoutore.stderris undefined or null at runtime (e.g., in certain error scenarios or test environments), calling.trim()directly on them will throw aTypeError(e.g.,Cannot read properties of undefined (reading 'trim')). This would crash the CLI and completely mask the original package manager error. Using optional chaining (?.) and nullish coalescing (??) prevents this secondary crash. -
Preserving Non-PackageManagerError Context: If
eis not an instance ofPackageManagerError(e.g., a network failure, file system issue, or other unexpected error), the current implementation discards the original error message and throws a generic'Unable to install packages'message. Appending the original error's message usingassertIsError(e)ensures that valuable diagnostic context is not lost.
} catch (e) {
let message = 'Unable to install packages';
if (e instanceof PackageManagerError) {
const stdout = e.stdout?.trim() ?? '';
const stderr = e.stderr?.trim() ?? '';
message += `: ${colors.bold(e.message)}${
stdout ? `\n\n${colors.bold('[STDOUT]')}: ${stdout}` : ''
}${stderr ? `\n\n${colors.bold('[STDERR]')}: ${colors.red(stderr)}` : ''}`;
} else {
assertIsError(e);
message += `: ${e.message}`;
}
throw new CommandError(message);
}There was a problem hiding this comment.
Updated to always propagate errors and use assertIsError as suggested. @alan-agius4 can you confirm that assertIsError is appropriate? The assertion error would still surface an error, but would not include "Update to install packages". I assume the assertion error is unlikely to be triggered because the codebase probably consistently uses error objects rather than primitive types / non error objects.
|
This change is related to my fix #34681 because after I patched I looked at adding tests for this, but the |
2e8d9a6 to
4e640b6
Compare
This change makes it easier to determine why a package manager is refusing to install an update.
4e640b6 to
c0adaa6
Compare
This change makes it easier to determine why a package manager is refusing to install an update.
PR Checklist
Please check to confirm your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Package manager install errors just show "Unable to install packages"
What is the new behavior?
Package manager install errors now show the error the package manager is surfacing.
Does this PR introduce a breaking change?
Other information