From fd05ae377c9d34512f5dee30fa7e3e72de0d9241 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Wed, 25 Dec 2024 12:17:10 -0800 Subject: [PATCH] Enhancement: Better handle some beszel errors (#4493) --- src/widgets/beszel/component.jsx | 20 +++++++++++++++----- src/widgets/beszel/proxy.js | 6 +++--- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/widgets/beszel/component.jsx b/src/widgets/beszel/component.jsx index 1d35b6e94..493a169eb 100644 --- a/src/widgets/beszel/component.jsx +++ b/src/widgets/beszel/component.jsx @@ -20,8 +20,20 @@ export default function Component({ service }) { widget.fields = widget.fields.slice(0, MAX_ALLOWED_FIELDS); } - if (systemsError) { - return ; + let system = null; + let finalError = systemsError; + + if (systems && !systems.items) { + finalError = { message: "No items returned from beszel API" }; + } else if (systems && systems.items && systemId) { + system = systems.items.find((item) => item.id === systemId); + if (!system) { + finalError = { message: `System with id ${systemId} not found` }; + } + } + + if (finalError) { + return ; } if (!systems) { @@ -33,9 +45,7 @@ export default function Component({ service }) { ); } - if (systemId) { - const system = systems.items.find((item) => item.id === systemId); - + if (system) { return ( diff --git a/src/widgets/beszel/proxy.js b/src/widgets/beszel/proxy.js index 7af70bd34..078e22c35 100644 --- a/src/widgets/beszel/proxy.js +++ b/src/widgets/beszel/proxy.js @@ -59,7 +59,7 @@ export default async function beszelProxyHandler(req, res) { if (!token) { [status, token] = await login(loginUrl, widget.username, widget.password, service); if (status !== 200) { - logger.debug(`HTTP ${status} logging into Beszel: ${token}`); + logger.debug(`HTTP ${status} logging into Beszel: ${JSON.stringify(token)}`); return res.status(status).send(token); } } @@ -72,13 +72,13 @@ export default async function beszelProxyHandler(req, res) { }, }); - if (status === 403) { + if ([400, 403].includes(status)) { logger.debug(`HTTP ${status} retrieving data from Beszel, logging in and trying again.`); cache.del(`${tokenCacheKey}.${service}`); [status, token] = await login(loginUrl, widget.username, widget.password, service); if (status !== 200) { - logger.debug(`HTTP ${status} logging into Beszel: ${data}`); + logger.debug(`HTTP ${status} logging into Beszel: ${JSON.stringify(data)}`); return res.status(status).send(data); }