audit-error.js 929 B

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