All files / lib/utils audit-error.js

100% Statements 16/16
100% Branches 10/10
100% Functions 1/1
100% Lines 16/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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