Skip to content

Commit

Permalink
fix: add error log
Browse files Browse the repository at this point in the history
  • Loading branch information
narol1024 committed Jun 3, 2024
1 parent 8c58ee3 commit c1f3785
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
4 changes: 0 additions & 4 deletions jest-global-mock.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* This is a sample test suite.
* Replace this with your implementation.
*/
import { exec } from 'child_process';
import fetch from 'node-fetch';

Expand Down
8 changes: 8 additions & 0 deletions src/platforms/npm-package/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
*/
import { readNpmPackageDependencies, countNpmPackageDependants, analyze } from './index';

const originalConsoleError = console.error;
beforeAll(() => {
console.error = jest.fn();
});
afterAll(() => {
console.error = originalConsoleError;
});

describe('Npm package tests', () => {
it('should not read a non-existent package', async () => {
await expect(readNpmPackageDependencies('is-a-non-existent-package')).rejects.toEqual(
Expand Down
14 changes: 8 additions & 6 deletions src/platforms/npm-package/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export async function readNpmPackageDependencies(packageName: string): Promise<N
}
const resultJson = JSON.parse(resultStdout) as any;
if (!!resultJson.error) {
throw new Error(`Cannot read ${packageName}.`);
throw resultJson.error;
}
resolve(
Object.entries(resultJson as NpmDependency).map(([name, version]) => ({
Expand All @@ -39,12 +39,13 @@ export async function readNpmPackageDependencies(packageName: string): Promise<N
);
return Promise.resolve();
} catch (error) {
return Promise.reject(new Error(`Cannot read ${packageName}.`));
return Promise.reject(error);
}
},
);
Promise.race([npmViewPromise, timeoutPromise])
.catch(() => {
.catch(error => {
console.error(error);
if (retriedTimes < 5) {
retriedTimes += 1;
doReadTask();
Expand Down Expand Up @@ -84,13 +85,14 @@ export async function countNpmPackageDependants(packageName: string, version: st
resolve(parseInt(match.replace(/,/g, ''), 10));
return Promise.resolve();
}
throw new Error(`Operation failed`);
throw new Error(`Cannot matches target text`);
} catch (error) {
return Promise.reject(new Error(`Operation failed`));
return Promise.reject(error);
}
};
Promise.race([fetchPromise(), timeoutPromise])
.catch(() => {
.catch(error => {
console.error(error);
if (retriedTimes < 5) {
retriedTimes += 1;
doCountTask();
Expand Down

0 comments on commit c1f3785

Please sign in to comment.