From cdec83463c7030315bf1456a0ae652ea1c7554bf Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Fri, 21 Oct 2022 12:14:45 +0200
Subject: [PATCH] fix(http): improve error logging (#18454)

Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
---
 lib/config/presets/bitbucket-server/index.ts  |  5 +--
 lib/config/presets/gitea/index.ts             |  5 +--
 lib/config/presets/github/index.ts            |  5 +--
 lib/config/presets/gitlab/index.ts            |  5 +--
 lib/config/presets/local/common.ts            |  5 +--
 lib/modules/datasource/npm/get.ts             | 35 ++++---------------
 lib/util/http/github.ts                       |  5 ---
 lib/util/http/index.ts                        |  6 ++++
 .../repository/process/lookup/index.ts        |  2 +-
 9 files changed, 18 insertions(+), 55 deletions(-)

diff --git a/lib/config/presets/bitbucket-server/index.ts b/lib/config/presets/bitbucket-server/index.ts
index b79376a7c9..7bb1f1e79b 100644
--- a/lib/config/presets/bitbucket-server/index.ts
+++ b/lib/config/presets/bitbucket-server/index.ts
@@ -36,10 +36,7 @@ export async function fetchJSONFile(
     if (err instanceof ExternalHostError) {
       throw err;
     }
-    logger.debug(
-      { statusCode: err.statusCode, url: `${endpoint}${url}` },
-      `Failed to retrieve ${fileName} from repo`
-    );
+    logger.debug(`Preset file ${fileName} not found in ${repo}`);
     throw new Error(PRESET_DEP_NOT_FOUND);
   }
   if (!res.body.isLastPage) {
diff --git a/lib/config/presets/gitea/index.ts b/lib/config/presets/gitea/index.ts
index ae164daa28..dfc8884396 100644
--- a/lib/config/presets/gitea/index.ts
+++ b/lib/config/presets/gitea/index.ts
@@ -24,10 +24,7 @@ export async function fetchJSONFile(
     if (err instanceof ExternalHostError) {
       throw err;
     }
-    logger.debug(
-      { statusCode: err.statusCode, repo, fileName },
-      `Failed to retrieve ${fileName} from repo`
-    );
+    logger.debug(`Preset file ${fileName} not found in ${repo}`);
     throw new Error(PRESET_DEP_NOT_FOUND);
   }
 
diff --git a/lib/config/presets/github/index.ts b/lib/config/presets/github/index.ts
index 272e5afd1b..315c512594 100644
--- a/lib/config/presets/github/index.ts
+++ b/lib/config/presets/github/index.ts
@@ -30,10 +30,7 @@ export async function fetchJSONFile(
     if (err instanceof ExternalHostError) {
       throw err;
     }
-    logger.debug(
-      { statusCode: err.statusCode, url },
-      `Failed to retrieve ${fileName} from repo`
-    );
+    logger.debug(`Preset file ${fileName} not found in ${repo}`);
     throw new Error(PRESET_DEP_NOT_FOUND);
   }
 
diff --git a/lib/config/presets/gitlab/index.ts b/lib/config/presets/gitlab/index.ts
index c0b24f6cf8..edf601a9c2 100644
--- a/lib/config/presets/gitlab/index.ts
+++ b/lib/config/presets/gitlab/index.ts
@@ -57,10 +57,7 @@ export async function fetchJSONFile(
     if (err instanceof ExternalHostError) {
       throw err;
     }
-    logger.debug(
-      { statusCode: err.statusCode, url },
-      `Failed to retrieve ${fileName} from repo`
-    );
+    logger.debug(`Preset file ${fileName} not found in ${repo}`);
     throw new Error(PRESET_DEP_NOT_FOUND);
   }
 
diff --git a/lib/config/presets/local/common.ts b/lib/config/presets/local/common.ts
index 7648fdd56f..c4a0401d95 100644
--- a/lib/config/presets/local/common.ts
+++ b/lib/config/presets/local/common.ts
@@ -17,10 +17,7 @@ export async function fetchJSONFile(
       throw err;
     }
 
-    logger.debug(
-      { err, repo, fileName },
-      `Failed to retrieve ${fileName} from repo ${repo}`
-    );
+    logger.debug(`Preset file ${fileName} not found in ${repo}`);
 
     throw new Error(PRESET_DEP_NOT_FOUND);
   }
diff --git a/lib/modules/datasource/npm/get.ts b/lib/modules/datasource/npm/get.ts
index 21e450f379..074d53044d 100644
--- a/lib/modules/datasource/npm/get.ts
+++ b/lib/modules/datasource/npm/get.ts
@@ -157,35 +157,12 @@ export async function getDependency(
     }
     return dep;
   } catch (err) {
-    if (err.statusCode === 401 || err.statusCode === 403) {
-      logger.debug(
-        {
-          packageUrl,
-          err,
-          statusCode: err.statusCode,
-          packageName,
-        },
-        `Dependency lookup failure: unauthorized`
-      );
-      return null;
-    }
-    if (err.statusCode === 402) {
-      logger.debug(
-        {
-          packageUrl,
-          err,
-          statusCode: err.statusCode,
-          packageName,
-        },
-        `Dependency lookup failure: payment required`
-      );
-      return null;
-    }
-    if (err.statusCode === 404 || err.code === 'ENOTFOUND') {
-      logger.debug(
-        { err, packageName },
-        `Dependency lookup failure: not found`
-      );
+    const ignoredStatusCodes = [401, 402, 403, 404];
+    const ignoredResponseCodes = ['ENOTFOUND'];
+    if (
+      ignoredStatusCodes.includes(err.statusCode) ||
+      ignoredResponseCodes.includes(err.code)
+    ) {
       return null;
     }
     if (uri.host === 'registry.npmjs.org') {
diff --git a/lib/util/http/github.ts b/lib/util/http/github.ts
index 8de822fba2..0b2401bede 100644
--- a/lib/util/http/github.ts
+++ b/lib/util/http/github.ts
@@ -154,11 +154,6 @@ function handleGotError(
   ) {
     return err;
   }
-  if (err.statusCode === 404) {
-    logger.debug({ url: path }, 'GitHub 404');
-  } else {
-    logger.debug({ err }, 'Unknown GitHub error');
-  }
   return err;
 }
 
diff --git a/lib/util/http/index.ts b/lib/util/http/index.ts
index 2835a46a0a..aa9c99d392 100644
--- a/lib/util/http/index.ts
+++ b/lib/util/http/index.ts
@@ -89,6 +89,12 @@ async function gotTask<T>(
       duration =
         error.timings?.phases.total ??
         /* istanbul ignore next: can't be tested */ 0;
+      const method = options.method?.toUpperCase() ?? 'GET';
+      const code = error.code ?? 'UNKNOWN';
+      const retryCount = error.response?.retryCount ?? -1;
+      logger.debug(
+        `${method} ${url} = (code=${code}, statusCode=${statusCode} retryCount=${retryCount}, duration=${duration})`
+      );
     }
 
     throw error;
diff --git a/lib/workers/repository/process/lookup/index.ts b/lib/workers/repository/process/lookup/index.ts
index f79d040d38..517220a8e5 100644
--- a/lib/workers/repository/process/lookup/index.ts
+++ b/lib/workers/repository/process/lookup/index.ts
@@ -82,7 +82,7 @@ export async function lookupUpdates(
         // If dependency lookup fails then warn and return
         const warning: ValidationMessage = {
           topic: depName,
-          message: `Failed to look up dependency ${depName}`,
+          message: `Failed to look up ${datasource} dependency ${depName}`,
         };
         logger.debug({ dependency: depName, packageFile }, warning.message);
         // TODO: return warnings in own field
-- 
GitLab