audit-error.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. const { log, output } = require('proc-log')
  2. const { redactLog: replaceInfo } = require('@npmcli/redact')
  3. // Print an error or just nothing if the audit report has an error.
  4. // This is called by the audit command, and by the reify-output util prints a JSON version of the error if it's --json.
  5. // Returns 'true' if there was an error, false otherwise.
  6. const auditError = (npm, report) => {
  7. if (!report?.error) {
  8. return false
  9. }
  10. if (npm.command !== 'audit') {
  11. return true
  12. }
  13. const { error } = report
  14. // ok, we care about it, then
  15. log.warn('audit', error.message)
  16. const { body: errBody } = error
  17. const body = Buffer.isBuffer(errBody) ? errBody.toString() : errBody
  18. if (npm.flatOptions.json) {
  19. output.buffer({
  20. message: error.message,
  21. method: error.method,
  22. uri: replaceInfo(error.uri),
  23. headers: error.headers,
  24. statusCode: error.statusCode,
  25. body,
  26. })
  27. } else {
  28. output.standard(body)
  29. }
  30. // XXX we should throw a real error here
  31. throw 'audit endpoint returned an error'
  32. }
  33. module.exports = auditError