马宇豪
2024-07-16 f591c27b57e2418c9495bc02ae8cfff84d35bc18
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
38
39
40
const { log, output } = require('proc-log')
const { redactLog: replaceInfo } = require('@npmcli/redact')
 
// 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 auditError = (npm, report) => {
  if (!report || !report.error) {
    return false
  }
 
  if (npm.command !== 'audit') {
    return true
  }
 
  const { error } = report
 
  // ok, we care about it, then
  log.warn('audit', error.message)
  const { body: errBody } = error
  const body = Buffer.isBuffer(errBody) ? errBody.toString() : errBody
  if (npm.flatOptions.json) {
    output.buffer({
      message: error.message,
      method: error.method,
      uri: replaceInfo(error.uri),
      headers: error.headers,
      statusCode: error.statusCode,
      body,
    })
  } else {
    output.standard(body)
  }
 
  throw 'audit endpoint returned an error'
}
 
module.exports = auditError