Skip to content

Commit

Permalink
Merge pull request #29459 from storybookjs/yann/fix-npm-error-parsing
Browse files Browse the repository at this point in the history
CLI: Refactor NPMProxy error parsing logic
  • Loading branch information
yannbf authored Oct 30, 2024
2 parents 72b24f8 + 13c9a20 commit a4b4267
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
16 changes: 15 additions & 1 deletion code/core/src/common/js-package-manager/NPMProxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ describe('NPM Proxy', () => {

describe('parseErrors', () => {
it('should parse npm errors', () => {
const NPM_RESOLVE_ERROR_SAMPLE = `
const NPM_LEGACY_RESOLVE_ERROR_SAMPLE = `
npm ERR!
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
Expand All @@ -439,6 +439,17 @@ describe('NPM Proxy', () => {
npm ERR! react@"30" from the root project
`;

const NPM_RESOLVE_ERROR_SAMPLE = `
npm error
npm error code ERESOLVE
npm error ERESOLVE unable to resolve dependency tree
npm error
npm error While resolving: before-storybook@1.0.0
npm error Found: react@undefined
npm error node_modules/react
npm error react@"30" from the root project
`;

const NPM_TIMEOUT_ERROR_SAMPLE = `
npm notice
npm notice New major version of npm available! 8.5.0 -> 9.6.7
Expand All @@ -451,6 +462,9 @@ describe('NPM Proxy', () => {
npm ERR! network This is a problem related to network connectivity.
`;

expect(npmProxy.parseErrorFromLogs(NPM_LEGACY_RESOLVE_ERROR_SAMPLE)).toEqual(
'NPM error ERESOLVE - Dependency resolution error.'
);
expect(npmProxy.parseErrorFromLogs(NPM_RESOLVE_ERROR_SAMPLE)).toEqual(
'NPM error ERESOLVE - Dependency resolution error.'
);
Expand Down
4 changes: 2 additions & 2 deletions code/core/src/common/js-package-manager/NPMProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ type NpmDependencies = {
export type NpmListOutput = {
dependencies: NpmDependencies;
};
const NPM_ERROR_REGEX = /npm (ERR!|error) (code|errno) (\w+)/i;

const NPM_ERROR_REGEX = /npm ERR! code (\w+)/;
const NPM_ERROR_CODES = {
E401: 'Authentication failed or is required.',
E403: 'Access to the resource is forbidden.',
Expand Down Expand Up @@ -320,7 +320,7 @@ export class NPMProxy extends JsPackageManager {
const match = logs.match(NPM_ERROR_REGEX);

if (match) {
const errorCode = match[1] as keyof typeof NPM_ERROR_CODES;
const errorCode = match[3] as keyof typeof NPM_ERROR_CODES;
if (errorCode) {
finalMessage = `${finalMessage} ${errorCode}`;
}
Expand Down

0 comments on commit a4b4267

Please sign in to comment.