1const log = require('./log-shim') 2const replaceInfo = require('./replace-info.js') 3 4// print an error or just nothing if the audit report has an error 5// this is called by the audit command, and by the reify-output util 6// prints a JSON version of the error if it's --json 7// returns 'true' if there was an error, false otherwise 8 9const auditError = (npm, report) => { 10 if (!report || !report.error) { 11 return false 12 } 13 14 if (npm.command !== 'audit') { 15 return true 16 } 17 18 const { error } = report 19 20 // ok, we care about it, then 21 log.warn('audit', error.message) 22 const { body: errBody } = error 23 const body = Buffer.isBuffer(errBody) ? errBody.toString() : errBody 24 if (npm.flatOptions.json) { 25 npm.output(JSON.stringify({ 26 message: error.message, 27 method: error.method, 28 uri: replaceInfo(error.uri), 29 headers: error.headers, 30 statusCode: error.statusCode, 31 body, 32 }, null, 2)) 33 } else { 34 npm.output(body) 35 } 36 37 throw 'audit endpoint returned an error' 38} 39 40module.exports = auditError 41