From ea8bc448c31d6c6fde12fb2d0628dca0e6edd6d3 Mon Sep 17 00:00:00 2001 From: Quinn Turner Date: Tue, 17 May 2022 16:04:50 -0400 Subject: [PATCH] fix: Handle NPM 7+'s message (error) response (#258) --- lib/npm-auditer.ts | 2 ++ package-lock.json | 14 +++++++------- package.json | 2 +- test/npm7-auditer.spec.js | 14 ++++++++++++++ 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/lib/npm-auditer.ts b/lib/npm-auditer.ts index 674de60f..37b1af5f 100644 --- a/lib/npm-auditer.ts +++ b/lib/npm-auditer.ts @@ -119,6 +119,8 @@ export async function audit(config: AuditCiConfig, reporter = reportAudit) { if ("error" in parsedOutput) { const { code, summary } = parsedOutput.error; throw new Error(`code ${code}: ${summary}`); + } else if ("message" in parsedOutput) { + throw new Error(parsedOutput.message); } return report(parsedOutput, config, reporter); } diff --git a/package-lock.json b/package-lock.json index f2859c07..92218570 100644 --- a/package-lock.json +++ b/package-lock.json @@ -33,7 +33,7 @@ "@types/yargs": "^17.0.9", "@typescript-eslint/eslint-plugin": "^5.14.0", "@typescript-eslint/parser": "^5.14.0", - "audit-types": "^0.5.0", + "audit-types": "^0.5.1", "chai": "^4.3.6", "eslint": "^8.11.0", "eslint-config-prettier": "^8.5.0", @@ -681,9 +681,9 @@ } }, "node_modules/audit-types": { - "version": "0.5.0", - "resolved": "https://registry.npmjs.org/audit-types/-/audit-types-0.5.0.tgz", - "integrity": "sha512-YyNLLGa9n+CDZT4cl98tYGEnn9Z+3Np37ZMY5kkSfb7++E9wVCOMi3N4aAsnIr+9sAYWpvE8SMHw64KfEEBJ0A==", + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/audit-types/-/audit-types-0.5.1.tgz", + "integrity": "sha512-0SOZQMD7c63vMe/LFvIqXNCrQZP2tBnMQdcDCe/9tPOqaw+6ZCQFCj49xDFBn81j18J+IznFjcSug7ZkP+/EnA==", "dev": true }, "node_modules/balanced-match": { @@ -4441,9 +4441,9 @@ "dev": true }, "audit-types": { - "version": "0.5.0", - "resolved": "https://registry.npmjs.org/audit-types/-/audit-types-0.5.0.tgz", - "integrity": "sha512-YyNLLGa9n+CDZT4cl98tYGEnn9Z+3Np37ZMY5kkSfb7++E9wVCOMi3N4aAsnIr+9sAYWpvE8SMHw64KfEEBJ0A==", + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/audit-types/-/audit-types-0.5.1.tgz", + "integrity": "sha512-0SOZQMD7c63vMe/LFvIqXNCrQZP2tBnMQdcDCe/9tPOqaw+6ZCQFCj49xDFBn81j18J+IznFjcSug7ZkP+/EnA==", "dev": true }, "balanced-match": { diff --git a/package.json b/package.json index 7512a64e..46fc4db9 100644 --- a/package.json +++ b/package.json @@ -62,7 +62,7 @@ "@types/yargs": "^17.0.9", "@typescript-eslint/eslint-plugin": "^5.14.0", "@typescript-eslint/parser": "^5.14.0", - "audit-types": "^0.5.0", + "audit-types": "^0.5.1", "chai": "^4.3.6", "eslint": "^8.11.0", "eslint-config-prettier": "^8.5.0", diff --git a/test/npm7-auditer.spec.js b/test/npm7-auditer.spec.js index 625894da..4594cdfe 100644 --- a/test/npm7-auditer.spec.js +++ b/test/npm7-auditer.spec.js @@ -277,6 +277,20 @@ describe("npm7-auditer", () => { done(); }); }); + it("fails with error code ECONNREFUSED on a live site with no registry", (done) => { + audit( + config({ + directory: testDirectory("npm-low"), + levels: { low: true }, + registry: "http://localhost", + }) + ) + .catch((error) => { + expect(error.message).to.include("ECONNREFUSED"); + done(); + }) + .catch((error) => done(error)); + }); it("reports summary with no vulnerabilities when critical devDependency and skip-dev is true", () => { const summary = report( reportNpmSkipDevelopment,