From 193a5cfc2404a36149b975508ccec282307007f7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Kat=20March=C3=A1n?= <kzm@zkat.tech>
Date: Wed, 9 Jan 2019 11:48:44 -0800
Subject: [PATCH] audit: report any errors above 400 as potentially not
 supporting audit (#128)

PR-URL: https://github.com/npm/cli/pull/128
Fixes: https://npm.community/t/npm-audit-fails-with-enoaudit-on-500-response/3629
Fixes: https://npm.community/t/npm-audit-error-messaging-update-for-401s/3983
Credit: @zkat
Reviewed-By: @aeschright
---
 lib/audit.js | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/lib/audit.js b/lib/audit.js
index 076ca256b7c72..2cabef9d27d0d 100644
--- a/lib/audit.js
+++ b/lib/audit.js
@@ -189,8 +189,16 @@ function auditCmd (args, cb) {
   }).then((auditReport) => {
     return audit.submitForFullReport(auditReport)
   }).catch((err) => {
-    if (err.statusCode === 404 || err.statusCode >= 500) {
-      const ne = new Error(`Your configured registry (${opts.registry}) does not support audit requests.`)
+    if (err.statusCode >= 400) {
+      let msg
+      if (err.statusCode === 401) {
+        msg = `Either your login credentials are invalid or your registry (${opts.registry}) does not support audit.`
+      } else if (err.statusCode === 404) {
+        msg = `Your configured registry (${opts.registry}) does not support audit requests.`
+      } else {
+        msg = `Your configured registry (${opts.registry}) does not support audit requests, or the audit endpoint is temporarily unavailable.`
+      }
+      const ne = new Error(msg)
       ne.code = 'ENOAUDIT'
       ne.wrapped = err
       throw ne