From f8768711dab9f57cbeb87fd2cf4c828ee46c8a0f Mon Sep 17 00:00:00 2001 From: hank Date: Tue, 20 May 2025 20:40:59 -0400 Subject: [PATCH] Fix: refresh beszel token if empty list is returned (#5292) Co-authored-by: shamoon <4887959+shamoon@users.noreply.github.com> --- src/widgets/beszel/proxy.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/widgets/beszel/proxy.js b/src/widgets/beszel/proxy.js index 96dfc913b..93a2385c6 100644 --- a/src/widgets/beszel/proxy.js +++ b/src/widgets/beszel/proxy.js @@ -72,8 +72,23 @@ export default async function beszelProxyHandler(req, res) { }, }); - if ([400, 403].includes(status)) { - logger.debug(`HTTP ${status} retrieving data from Beszel, logging in and trying again.`); + const badRequest = [400, 403].includes(status); + const text = data.toString("utf-8"); + let isEmpty = false; + + try { + const json = JSON.parse(text); + isEmpty = Array.isArray(json.items) && json.items.length === 0; + } catch (err) { + logger.debug("Failed to parse Beszel response JSON:", err); + } + + if (badRequest || isEmpty) { + if (badRequest) { + logger.debug(`HTTP ${status} retrieving data from Beszel, logging in and trying again.`); + } else { + logger.debug(`Received empty list from Beszel, logging in and trying again.`); + } cache.del(`${tokenCacheKey}.${service}`); [status, token] = await login(loginUrl, widget.username, widget.password, service);