Skip to content

Commit

Permalink
Merge pull request #76 from sanniassin/print-yarn-errors
Browse files Browse the repository at this point in the history
Print yarn's output when it crashes with an error
  • Loading branch information
quinnturner authored Apr 12, 2019
2 parents f90f072 + b9f6bcb commit f11a4c3
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
28 changes: 25 additions & 3 deletions lib/yarn-auditer.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ function yarnAuditSupportsRegistry(yarnVersion) {
* `registry`: the registry to resolve packages by name and version.
* `show-not-found`: show whitelisted advisories that are not found.
* `levels`: the vulnerability levels to fail on, if `moderate` is set `true`, `high` and `critical` should be as well.
* `_yarn`: a path to yarn, uses yarn from PATH if not specified.
* @returns {Promise<any>} Returns the audit report summary on resolve, `Error` on rejection.
*/
function audit(config, reporter = reportAudit) {
return Promise.resolve().then(() => {
const { registry, report, whitelist } = config;
const { registry, report, whitelist, _yarn } = config;
const yarnExec = _yarn || 'yarn';
let missingLockFile = false;
const model = new Model(config);

Expand Down Expand Up @@ -103,8 +105,22 @@ function audit(config, reporter = reportAudit) {
}
}

let isFatalError = false;
const stderrBuffer = [];
function errListener(line) {
const errorLine = JSON.parse(line);
stderrBuffer.push(line);
if (isFatalError) {
return;
}

let errorLine;
try {
errorLine = JSON.parse(line);
} catch (e) {
isFatalError = true;
return;
}

if (errorLine.type === 'error') {
throw new Error(errorLine.data);
}
Expand All @@ -122,8 +138,14 @@ function audit(config, reporter = reportAudit) {
);
}
}
return runProgram('yarn', args, options, outListener, errListener).then(
return runProgram(yarnExec, args, options, outListener, errListener).then(
() => {
if (isFatalError) {
throw new Error(
`Invocation of yarn audit failed:\n${stderrBuffer.join('\n')}`
);
}

if (missingLockFile) {
console.warn(
'\x1b[33m%s\x1b[0m',
Expand Down
19 changes: 19 additions & 0 deletions test/yarn-auditer.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,25 @@ function testDir(s) {
// eslint-disable-next-line func-names
describe('yarn-auditer', function() {
this.slow(3000);
it('prints unexpected https://registry.yarnpkg.com 503 error message', () => {
const directory = testDir('yarn-error');
const errorMessagePath = path.resolve(directory, 'error-message');
const errorMessage = require(errorMessagePath); // eslint-disable-line

return audit(
config({
directory,
_yarn: path.join(directory, 'yarn'),
})
)
.then(() => {
// Since we expect an error the promise should never resolve
throw new Error();
})
.catch(err => {
expect(err.toString()).to.contain(errorMessage);
});
});
it('reports critical severity', () => {
return audit(
config({
Expand Down
15 changes: 15 additions & 0 deletions test/yarn-error/error-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = `/usr/local/lib/node_modules/yarn/lib/cli.js:66237
throw new (_errors || _load_errors()).ResponseError(_this3.reporter.lang('requestFailed', description), res.statusCode);
^
Error: Request failed "503 Service Unavailable"
at ResponseError.ExtendableBuiltin (/usr/local/lib/node_modules/yarn/lib/cli.js:702:66)
at new ResponseError (/usr/local/lib/node_modules/yarn/lib/cli.js:808:124)
at Request.params.callback [as _callback] (/usr/local/lib/node_modules/yarn/lib/cli.js:66237:19)
at Request.self.callback (/usr/local/lib/node_modules/yarn/lib/cli.js:129397:22)
at Request.emit (events.js:193:13)
at Request.<anonymous> (/usr/local/lib/node_modules/yarn/lib/cli.js:130369:10)
at Request.emit (events.js:193:13)
at IncomingMessage.<anonymous> (/usr/local/lib/node_modules/yarn/lib/cli.js:130291:12)
at Object.onceWrapper (events.js:281:20)
at IncomingMessage.emit (events.js:198:15)
Exiting...`;
8 changes: 8 additions & 0 deletions test/yarn-error/yarn
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env node
const path = require('path');

const errorMessagePath = path.resolve(__dirname, 'error-message');
const errorMessage = require(errorMessagePath); //eslint-disable-line

console.error(errorMessage);
process.exitCode = 1;

0 comments on commit f11a4c3

Please sign in to comment.