diff --git a/src/core/ErrorChannel.mjs b/src/core/ErrorChannel.mjs index 6472665..c5f3de3 100644 --- a/src/core/ErrorChannel.mjs +++ b/src/core/ErrorChannel.mjs @@ -288,6 +288,8 @@ function _forward(level, tag, args) { if (typeof tag !== "string") return; if (tag.startsWith("[ErrorChannel]") || tag.startsWith("[Error_Handling]")) return; const body = _formatArgs(args); + /* Suppress expected external not-found lookups from generating alarmist telemetry warnings */ + if (tag.includes("[LastFm]") && (/not found|error 6:/i.test(body) || /Track not found/i.test(body))) return; const now = Date.now(); const key = `${level}|${tag}|${(body || tag).slice(0, 200)}`; const last = _forwardDedup.get(key); diff --git a/src/services/lastfm/ScrobblingMixin.mjs b/src/services/lastfm/ScrobblingMixin.mjs index df4dae7..cb33190 100644 --- a/src/services/lastfm/ScrobblingMixin.mjs +++ b/src/services/lastfm/ScrobblingMixin.mjs @@ -7,7 +7,7 @@ */ import { logger } from "../../core/Logger.mjs"; -import { apiCall, isLastFmFatalLinkError, isLastFmUserNotFound, noteStaleUser } from "./constants.mjs"; +import { apiCall, isLastFmFatalLinkError, isLastFmUserNotFound, isLastFmNotFound, noteStaleUser } from "./constants.mjs"; /** * @type {object} @@ -48,6 +48,7 @@ const ScrobblingMixin = { } return; } + if (isLastFmNotFound(err)) return; logger.warn(`[LastFm] updateNowPlaying failed for ${userId}: ${err.message}`); } }, @@ -89,6 +90,7 @@ const ScrobblingMixin = { } return; } + if (isLastFmNotFound(err)) return; logger.warn(`[LastFm] Scrobble failed for ${userId}: ${err.message}`); } }, @@ -124,7 +126,7 @@ const ScrobblingMixin = { } return 0; } - if (!e?.message?.includes("fetch failed")) { + if (!isLastFmNotFound(e) && !e?.message?.includes("fetch failed")) { logger.warn("[LastFm] Error:", e?.message); } return 0; diff --git a/src/services/lastfm/ServerStatsMixin.mjs b/src/services/lastfm/ServerStatsMixin.mjs index 1e67bc9..7240935 100644 --- a/src/services/lastfm/ServerStatsMixin.mjs +++ b/src/services/lastfm/ServerStatsMixin.mjs @@ -7,7 +7,17 @@ */ import { logger } from "../../core/Logger.mjs"; -import { isLastFmUserNotFound, noteStaleUser } from "./constants.mjs"; +import { isLastFmUserNotFound, isLastFmNotFound, noteStaleUser } from "./constants.mjs"; + +/** + * @private + * Log failure only if it is an unexpected error, suppressing expected Last.fm not-found responses. + * @param {Error} e + */ +function logStatsWarning(e) { + if (isLastFmNotFound(e)) return; + logger.warn("[LastFm] Error:", e?.message); +} /** * @type {object} @@ -51,7 +61,7 @@ const ServerStatsMixin = { } return null; } - logger.warn("[LastFm] Error:", e?.message); + logStatsWarning(e); return { userId: uid, username: user.username, @@ -110,7 +120,7 @@ const ServerStatsMixin = { } return null; } - logger.warn("[LastFm] Error:", e?.message); + logStatsWarning(e); return { userId: uid, username: user.username, @@ -169,7 +179,7 @@ const ServerStatsMixin = { } return null; } - logger.warn("[LastFm] Error:", e?.message); + logStatsWarning(e); return { userId: uid, username: user.username, @@ -240,7 +250,7 @@ const ServerStatsMixin = { matchPercentage, }; } catch (e) { - logger.warn("[LastFm] Error:", e?.message); + logStatsWarning(e); return null; } }, @@ -340,7 +350,7 @@ const ServerStatsMixin = { ); return Number(rows[0]?.linked_users ?? 0); } catch (e) { - logger.warn("[LastFm] Error:", e?.message); + logStatsWarning(e); return 0; } }, @@ -506,7 +516,7 @@ const ServerStatsMixin = { } return null; } - logger.warn("[LastFm] Error:", e?.message); + logStatsWarning(e); return null; } }) @@ -586,7 +596,7 @@ const ServerStatsMixin = { crowns.sort((a, b) => b.userPlaycount - a.userPlaycount); return crowns; - } catch (e) { logger.warn("[LastFm] Error:", e?.message); return []; } + } catch (e) { logStatsWarning(e); return []; } } }; diff --git a/src/services/lastfm/TrackQueriesMixin.mjs b/src/services/lastfm/TrackQueriesMixin.mjs index 4e565d7..25f646b 100644 --- a/src/services/lastfm/TrackQueriesMixin.mjs +++ b/src/services/lastfm/TrackQueriesMixin.mjs @@ -7,7 +7,17 @@ */ import { logger } from "../../core/Logger.mjs"; -import { apiCall, normalizeTrackText } from "./constants.mjs"; +import { apiCall, normalizeTrackText, isLastFmNotFound } from "./constants.mjs"; + +/** + * @private + * Log query failure only if it is an unexpected error, suppressing expected Last.fm not-found responses. + * @param {Error} e + */ +function logQueryWarning(e) { + if (isLastFmNotFound(e)) return; + logger.warn("[LastFm] Error:", e?.message); +} /** * @type {object} @@ -34,7 +44,7 @@ const TrackQueriesMixin = { const data = await apiCall(params, this.apiSecret); return data.track; } catch (e) { - logger.warn("[LastFm] Error:", e?.message); + logQueryWarning(e); return null; } }, @@ -226,7 +236,7 @@ const TrackQueriesMixin = { url: t.url ?? "", match: parseFloat(t.match ?? 0), })).filter(t => t.match > 0.1); - } catch (e) { logger.warn("[LastFm] Error:", e?.message); return []; } + } catch (e) { logQueryWarning(e); return []; } }, /** @@ -273,7 +283,7 @@ const TrackQueriesMixin = { userplaycount: a.stats?.userplaycount ? Number(a.stats.userplaycount) : null, }; } catch (e) { - logger.warn("[LastFm] Error:", e?.message); + logQueryWarning(e); return null; } }, @@ -321,7 +331,7 @@ const TrackQueriesMixin = { userplaycount: a.userplaycount ? Number(a.userplaycount) : null, }; } catch (e) { - logger.warn("[LastFm] Error:", e?.message); + logQueryWarning(e); return null; } }, @@ -354,7 +364,7 @@ const TrackQueriesMixin = { playcount: Number(t.playcount ?? 0), image: t.image?.[2]?.["#text"] ?? t.image?.[1]?.["#text"] ?? "", })); - } catch (e) { logger.warn("[LastFm] Error:", e?.message); return []; } + } catch (e) { logQueryWarning(e); return []; } }, /** @@ -385,7 +395,7 @@ const TrackQueriesMixin = { playcount: Number(a.playcount ?? 0), image: a.image?.[2]?.["#text"] ?? a.image?.[1]?.["#text"] ?? "", })); - } catch (e) { logger.warn("[LastFm] Error:", e?.message); return []; } + } catch (e) { logQueryWarning(e); return []; } }, /** @@ -415,7 +425,7 @@ const TrackQueriesMixin = { image: a.image?.[2]?.["#text"] ?? a.image?.[1]?.["#text"] ?? "", match: parseFloat(a.match ?? 0), })); - } catch (e) { logger.warn("[LastFm] Error:", e?.message); return []; } + } catch (e) { logQueryWarning(e); return []; } }, /** @@ -448,7 +458,7 @@ const TrackQueriesMixin = { summary: t.wiki?.summary ?? "", }; } catch (e) { - logger.warn("[LastFm] Error:", e?.message); + logQueryWarning(e); return null; } }, @@ -481,7 +491,7 @@ const TrackQueriesMixin = { playcount: Number(t.playcount ?? 0), image: t.image?.[2]?.["#text"] ?? t.image?.[1]?.["#text"] ?? "", })); - } catch (e) { logger.warn("[LastFm] Error:", e?.message); return []; } + } catch (e) { logQueryWarning(e); return []; } }, /** @@ -511,7 +521,7 @@ const TrackQueriesMixin = { playcount: Number(a.playcount ?? 0), image: a.image?.[2]?.["#text"] ?? a.image?.[1]?.["#text"] ?? "", })); - } catch (e) { logger.warn("[LastFm] Error:", e?.message); return []; } + } catch (e) { logQueryWarning(e); return []; } }, /** @@ -548,7 +558,7 @@ const TrackQueriesMixin = { image: a.image?.[2]?.["#text"] ?? a.image?.[1]?.["#text"] ?? "", listeners: Number(a.listeners ?? 0), })); - } catch (e) { logger.warn("[LastFm] Error:", e?.message); return []; } + } catch (e) { logQueryWarning(e); return []; } }, /** @@ -585,7 +595,7 @@ const TrackQueriesMixin = { url: a.url ?? "", image: a.image?.[2]?.["#text"] ?? a.image?.[1]?.["#text"] ?? "", })); - } catch (e) { logger.warn("[LastFm] Error:", e?.message); return []; } + } catch (e) { logQueryWarning(e); return []; } }, /** @@ -609,7 +619,7 @@ const TrackQueriesMixin = { url: t.url ?? "", count: Number(t.count ?? 0), })); - } catch (e) { logger.warn("[LastFm] Error:", e?.message); return []; } + } catch (e) { logQueryWarning(e); return []; } }, /** @@ -635,7 +645,7 @@ const TrackQueriesMixin = { url: t.url ?? "", count: Number(t.count ?? 0), })); - } catch (e) { logger.warn("[LastFm] Error:", e?.message); return []; } + } catch (e) { logQueryWarning(e); return []; } }, /** @@ -661,7 +671,7 @@ const TrackQueriesMixin = { url: t.url ?? "", count: Number(t.count ?? 0), })); - } catch (e) { logger.warn("[LastFm] Error:", e?.message); return []; } + } catch (e) { logQueryWarning(e); return []; } }, /** @@ -687,7 +697,7 @@ const TrackQueriesMixin = { playcount: Number(a.playcount ?? 0), image: a.image?.[2]?.["#text"] ?? a.image?.[1]?.["#text"] ?? "", })); - } catch (e) { logger.warn("[LastFm] Error:", e?.message); return []; } + } catch (e) { logQueryWarning(e); return []; } }, /** @@ -712,7 +722,7 @@ const TrackQueriesMixin = { listeners: Number(a.listeners ?? 0), image: a.image?.[2]?.["#text"] ?? a.image?.[1]?.["#text"] ?? "", })); - } catch (e) { logger.warn("[LastFm] Error:", e?.message); return []; } + } catch (e) { logQueryWarning(e); return []; } }, /** @@ -738,7 +748,7 @@ const TrackQueriesMixin = { listeners: Number(t.listeners ?? 0), image: t.image?.[2]?.["#text"] ?? t.image?.[1]?.["#text"] ?? "", })); - } catch (e) { logger.warn("[LastFm] Error:", e?.message); return []; } + } catch (e) { logQueryWarning(e); return []; } }, /** @@ -763,7 +773,7 @@ const TrackQueriesMixin = { playcount: Number(t.playcount ?? 0), image: t.image?.[2]?.["#text"] ?? t.image?.[1]?.["#text"] ?? "", })); - } catch (e) { logger.warn("[LastFm] Error:", e?.message); return []; } + } catch (e) { logQueryWarning(e); return []; } }, /** @@ -787,7 +797,7 @@ const TrackQueriesMixin = { playcount: Number(a.playcount ?? 0), image: a.image?.[2]?.["#text"] ?? a.image?.[1]?.["#text"] ?? "", })); - } catch (e) { logger.warn("[LastFm] Error:", e?.message); return []; } + } catch (e) { logQueryWarning(e); return []; } } }; diff --git a/src/services/lastfm/UserQueriesMixin.mjs b/src/services/lastfm/UserQueriesMixin.mjs index 94b2772..2992d81 100644 --- a/src/services/lastfm/UserQueriesMixin.mjs +++ b/src/services/lastfm/UserQueriesMixin.mjs @@ -7,7 +7,17 @@ */ import { logger } from "../../core/Logger.mjs"; -import { apiCall } from "./constants.mjs"; +import { apiCall, isLastFmNotFound } from "./constants.mjs"; + +/** + * @private + * Log query failure only if it is an unexpected error, suppressing expected Last.fm not-found responses. + * @param {Error} e + */ +function logQueryWarning(e) { + if (isLastFmNotFound(e)) return; + logger.warn("[LastFm] Error:", e?.message); +} /** * @type {object} @@ -115,7 +125,7 @@ const UserQueriesMixin = { url: t.url ?? "", count: Number(t.count ?? 0), })); - } catch (e) { logger.warn("[LastFm] Error:", e?.message); return []; } + } catch (e) { logQueryWarning(e); return []; } }, /** @@ -144,7 +154,7 @@ const UserQueriesMixin = { from: Number(c.from ?? 0), to: Number(c.to ?? 0), })); - } catch (e) { logger.warn("[LastFm] Error:", e?.message); return []; } + } catch (e) { logQueryWarning(e); return []; } }, /** @@ -442,7 +452,7 @@ const UserQueriesMixin = { query: `${t.name} ${t.artist ?? ar.name}`, image: t.image ?? ar.image ?? "", })); - } catch (e) { logger.warn("[LastFm] Error:", e?.message); return []; } + } catch (e) { logQueryWarning(e); return []; } }) ); for (const r of artistResults) { @@ -495,7 +505,7 @@ const UserQueriesMixin = { realname: f.realname ?? "", country: f.country ?? "", })); - } catch (e) { logger.warn("[LastFm] Error:", e?.message); return []; } + } catch (e) { logQueryWarning(e); return []; } }, /** @@ -525,7 +535,7 @@ const UserQueriesMixin = { playcount: Number(a.playcount ?? 0), image: a.image?.[2]?.["#text"] ?? a.image?.[1]?.["#text"] ?? "", })); - } catch (e) { logger.warn("[LastFm] Error:", e?.message); return []; } + } catch (e) { logQueryWarning(e); return []; } }, /** @@ -556,7 +566,7 @@ const UserQueriesMixin = { playcount: Number(t.playcount ?? 0), image: t.image?.[2]?.["#text"] ?? t.image?.[1]?.["#text"] ?? "", })); - } catch (e) { logger.warn("[LastFm] Error:", e?.message); return []; } + } catch (e) { logQueryWarning(e); return []; } }, /** @@ -587,7 +597,7 @@ const UserQueriesMixin = { playcount: Number(a.playcount ?? 0), image: a.image?.[2]?.["#text"] ?? a.image?.[1]?.["#text"] ?? "", })); - } catch (e) { logger.warn("[LastFm] Error:", e?.message); return []; } + } catch (e) { logQueryWarning(e); return []; } } }; diff --git a/src/services/lastfm/constants.mjs b/src/services/lastfm/constants.mjs index 785a1c7..8c5444b 100644 --- a/src/services/lastfm/constants.mjs +++ b/src/services/lastfm/constants.mjs @@ -63,6 +63,20 @@ function isLastFmUserNotFound(err) { return /user not found/i.test(String(err?.message ?? "")); } +/** + * Check whether a Last.fm error is an expected "Not Found" response (track, artist, album, tag, or user not found, code 6). + * Common when querying streams, YouTube videos, or uncataloged tracks that do not exist in Last.fm's database. + * @param {Error|object} err + * @returns {boolean} + */ +function isLastFmNotFound(err) { + if (!err) return false; + const msg = String(err?.message ?? err ?? ""); + const code = Number(err?.code) || 0; + return code === 6 + || /track not found|artist not found|album not found|user not found|tag not found|could not be found/i.test(msg); +} + /** * Check whether an error means the stored Last.fm link is unrecoverable — * the linked account no longer exists or its session was revoked. Deliberately @@ -164,4 +178,4 @@ async function apiCall(params, apiSecret, post = false) { return data; } -export { BASE_URL, normalizeTrackText, buildSignature, apiCall, LastFmApiError, isLastFmUserNotFound, isLastFmFatalLinkError, noteStaleUser, clearStaleUserFlag }; +export { BASE_URL, normalizeTrackText, buildSignature, apiCall, LastFmApiError, isLastFmUserNotFound, isLastFmNotFound, isLastFmFatalLinkError, noteStaleUser, clearStaleUserFlag };