Improve error handling for Glances widgets when host is unreachable

This commit is contained in:
Matt Sullivan 2024-06-21 09:04:50 +01:00
parent ec448d6c41
commit 2fae39e3ce
11 changed files with 82 additions and 76 deletions

View File

@ -1,10 +1,19 @@
export default function Container({ children, chart = true, className = "" }) { import { useContext } from "react";
import { useTranslation } from "next-i18next";
import { SettingsContext } from "utils/contexts/settings";
import { Settings } from "luxon";
export default function Container({ service, children, chart = true, error = false, className = "" }) {
const { t } = useTranslation();
const { settings } = useContext(SettingsContext);
const hideErrors = (service.widget.hide_errors || settings.hideErrors)
return ( return (
<div> <div>
{children} {children}
<div className={`absolute top-0 right-0 bottom-0 left-0 overflow-clip pointer-events-none ${className}`} /> <div className={`absolute top-0 right-0 bottom-0 left-0 overflow-clip pointer-events-none ${className}`} />
{chart && <div className="h-[68px] overflow-clip" />} {chart && <div className="h-[68px] overflow-clip" />}
{!chart && <div className="h-[16px] overflow-clip" />} {!chart && <div className="h-[16px] overflow-clip" />}
{error && !hideErrors && <div className="absolute bottom-2 left-2 z-20 text-red-400 text-xs opacity-75">{t("widget.api_error")}</div>}
</div> </div>
); );
} }

View File

@ -1,7 +0,0 @@
import { useTranslation } from "next-i18next";
export default function Error() {
const { t } = useTranslation();
return <div className="absolute bottom-2 left-2 z-20 text-red-400 text-xs opacity-75">{t("widget.api_error")}</div>;
}

View File

@ -2,7 +2,6 @@ import dynamic from "next/dynamic";
import { useState, useEffect } from "react"; import { useState, useEffect } from "react";
import { useTranslation } from "next-i18next"; import { useTranslation } from "next-i18next";
import Error from "../components/error";
import Container from "../components/container"; import Container from "../components/container";
import Block from "../components/block"; import Block from "../components/block";
@ -40,7 +39,7 @@ export default function Component({ service }) {
if (error) { if (error) {
return ( return (
<Container chart={chart}> <Container service={service} chart={chart} error={error}>
<Error error={error} /> <Error error={error} />
</Container> </Container>
); );
@ -48,14 +47,14 @@ export default function Component({ service }) {
if (!data) { if (!data) {
return ( return (
<Container chart={chart}> <Container service={service} chart={chart}>
<Block position="bottom-3 left-3">-</Block> <Block position="bottom-3 left-3">-</Block>
</Container> </Container>
); );
} }
return ( return (
<Container chart={chart}> <Container service={service} chart={chart}>
{chart && ( {chart && (
<Chart <Chart
dataPoints={dataPoints} dataPoints={dataPoints}

View File

@ -2,7 +2,6 @@ import dynamic from "next/dynamic";
import { useState, useEffect } from "react"; import { useState, useEffect } from "react";
import { useTranslation } from "next-i18next"; import { useTranslation } from "next-i18next";
import Error from "../components/error";
import Container from "../components/container"; import Container from "../components/container";
import Block from "../components/block"; import Block from "../components/block";
@ -54,15 +53,14 @@ export default function Component({ service }) {
if (error) { if (error) {
return ( return (
<Container chart={chart}> <Container service={service} chart={chart} error={error}>
<Error error={error} />
</Container> </Container>
); );
} }
if (!data) { if (!data) {
return ( return (
<Container chart={chart}> <Container service={service} chart={chart}>
<Block position="bottom-3 left-3">-</Block> <Block position="bottom-3 left-3">-</Block>
</Container> </Container>
); );
@ -72,7 +70,7 @@ export default function Component({ service }) {
if (!diskData) { if (!diskData) {
return ( return (
<Container chart={chart}> <Container service={service} chart={chart}>
<Block position="bottom-3 left-3">-</Block> <Block position="bottom-3 left-3">-</Block>
</Container> </Container>
); );
@ -82,7 +80,7 @@ export default function Component({ service }) {
const currentRate = diskRates[diskRates.length - 1]; const currentRate = diskRates[diskRates.length - 1];
return ( return (
<Container chart={chart}> <Container service={service} chart={chart}>
{chart && ( {chart && (
<ChartDual <ChartDual
dataPoints={ratePoints} dataPoints={ratePoints}

View File

@ -1,6 +1,5 @@
import { useTranslation } from "next-i18next"; import { useTranslation } from "next-i18next";
import Error from "../components/error";
import Container from "../components/container"; import Container from "../components/container";
import Block from "../components/block"; import Block from "../components/block";
@ -21,15 +20,14 @@ export default function Component({ service }) {
if (error) { if (error) {
return ( return (
<Container chart={chart}> <Container service={service} chart={chart} error={error}>
<Error error={error} />
</Container> </Container>
); );
} }
if (!data) { if (!data) {
return ( return (
<Container chart={chart}> <Container service={service} chart={chart}>
<Block position="bottom-3 left-3">-</Block> <Block position="bottom-3 left-3">-</Block>
</Container> </Container>
); );
@ -39,14 +37,14 @@ export default function Component({ service }) {
if (!fsData) { if (!fsData) {
return ( return (
<Container chart={chart}> <Container service={service} chart={chart}>
<Block position="bottom-3 left-3">-</Block> <Block position="bottom-3 left-3">-</Block>
</Container> </Container>
); );
} }
return ( return (
<Container chart={chart}> <Container service={service} chart={chart}>
{chart && ( {chart && (
<div className="absolute top-0 left-0 right-0 bottom-0"> <div className="absolute top-0 left-0 right-0 bottom-0">
<div <div

View File

@ -2,7 +2,6 @@ import dynamic from "next/dynamic";
import { useState, useEffect } from "react"; import { useState, useEffect } from "react";
import { useTranslation } from "next-i18next"; import { useTranslation } from "next-i18next";
import Error from "../components/error";
import Container from "../components/container"; import Container from "../components/container";
import Block from "../components/block"; import Block from "../components/block";
@ -27,6 +26,15 @@ export default function Component({ service }) {
useEffect(() => { useEffect(() => {
if (data) { if (data) {
if (data.hasOwnProperty("error")) {
return (
<Container service={service} chart={chart} error={true}>
</Container>
)
}
else {
// eslint-disable-next-line eqeqeq // eslint-disable-next-line eqeqeq
const gpuData = data.find((item) => item[item.key] == gpuName); const gpuData = data.find((item) => item[item.key] == gpuName);
@ -40,19 +48,20 @@ export default function Component({ service }) {
}); });
} }
} }
}
}, [data, gpuName, pointsLimit]); }, [data, gpuName, pointsLimit]);
if (error) { if (error) {
return ( return (
<Container chart={chart}> <Container service={service} chart={chart} error={error}>
<Error error={error} />
</Container> </Container>
); );
} }
if (!data) { if (!data) {
return ( return (
<Container chart={chart}> <Container service={service} chart={chart}>
<Block position="bottom-3 left-3">-</Block> <Block position="bottom-3 left-3">-</Block>
</Container> </Container>
); );
@ -63,14 +72,14 @@ export default function Component({ service }) {
if (!gpuData) { if (!gpuData) {
return ( return (
<Container chart={chart}> <Container service={service} chart={chart}>
<Block position="bottom-3 left-3">-</Block> <Block position="bottom-3 left-3">-</Block>
</Container> </Container>
); );
} }
return ( return (
<Container chart={chart}> <Container service={service} chart={chart}>
{chart && ( {chart && (
<ChartDual <ChartDual
dataPoints={dataPoints} dataPoints={dataPoints}

View File

@ -1,6 +1,5 @@
import { useTranslation } from "next-i18next"; import { useTranslation } from "next-i18next";
import Error from "../components/error";
import Container from "../components/container"; import Container from "../components/container";
import Block from "../components/block"; import Block from "../components/block";
@ -86,23 +85,30 @@ export default function Component({ service }) {
if (quicklookError) { if (quicklookError) {
return ( return (
<Container chart={chart}> <Container service={service} chart={chart} error={quicklookError}>
<Error error={quicklookError} />
</Container> </Container>
); );
} }
if (systemError) { if (systemError) {
return ( return (
<Container chart={chart}> <Container service={service} chart={chart} error={systemError}>
<Error error={systemError} />
</Container> </Container>
); );
} }
const dataCharts = []; const dataCharts = [];
if (quicklookData) { if (quicklookData) {
if (quicklookData.hasOwnProperty("error")) {
const quicklookError = true;
return (
<Container service={service} chart={chart} error={quicklookError} >
</Container>
);
}
else {
quicklookData.percpu.forEach((cpu, index) => { quicklookData.percpu.forEach((cpu, index) => {
dataCharts.push({ dataCharts.push({
name: `CPU ${index}`, name: `CPU ${index}`,
@ -114,8 +120,10 @@ export default function Component({ service }) {
}); });
} }
}
return ( return (
<Container chart={chart} className="bg-gradient-to-br from-theme-500/30 via-theme-600/20 to-theme-700/10"> <Container service={service} chart={chart} className="bg-gradient-to-br from-theme-500/30 via-theme-600/20 to-theme-700/10">
<Block position="top-3 right-3"> <Block position="top-3 right-3">
{quicklookData && quicklookData.cpu_name && chart && ( {quicklookData && quicklookData.cpu_name && chart && (
<div className="text-[0.6rem] opacity-50">{quicklookData.cpu_name}</div> <div className="text-[0.6rem] opacity-50">{quicklookData.cpu_name}</div>

View File

@ -2,7 +2,6 @@ import dynamic from "next/dynamic";
import { useState, useEffect } from "react"; import { useState, useEffect } from "react";
import { useTranslation } from "next-i18next"; import { useTranslation } from "next-i18next";
import Error from "../components/error";
import Container from "../components/container"; import Container from "../components/container";
import Block from "../components/block"; import Block from "../components/block";
@ -39,22 +38,21 @@ export default function Component({ service }) {
if (error) { if (error) {
return ( return (
<Container chart={chart}> <Container service={service} chart={chart} error={error}>
<Error error={error} />
</Container> </Container>
); );
} }
if (!data) { if (!data) {
return ( return (
<Container chart={chart}> <Container service={service} chart={chart}>
<Block position="bottom-3 left-3">-</Block> <Block position="bottom-3 left-3">-</Block>
</Container> </Container>
); );
} }
return ( return (
<Container chart={chart}> <Container service={service} chart={chart}>
{chart && ( {chart && (
<ChartDual <ChartDual
dataPoints={dataPoints} dataPoints={dataPoints}

View File

@ -2,7 +2,6 @@ import dynamic from "next/dynamic";
import { useState, useEffect } from "react"; import { useState, useEffect } from "react";
import { useTranslation } from "next-i18next"; import { useTranslation } from "next-i18next";
import Error from "../components/error";
import Container from "../components/container"; import Container from "../components/container";
import Block from "../components/block"; import Block from "../components/block";
@ -54,15 +53,14 @@ export default function Component({ service }) {
if (error) { if (error) {
return ( return (
<Container chart={chart}> <Container service={service} chart={chart} error={error}>
<Error error={error} />
</Container> </Container>
); );
} }
if (!data) { if (!data) {
return ( return (
<Container chart={chart}> <Container service={service} chart={chart}>
<Block position="bottom-3 left-3">-</Block> <Block position="bottom-3 left-3">-</Block>
</Container> </Container>
); );
@ -72,14 +70,14 @@ export default function Component({ service }) {
if (!interfaceData) { if (!interfaceData) {
return ( return (
<Container chart={chart}> <Container service={service} chart={chart}>
<Block position="bottom-3 left-3">-</Block> <Block position="bottom-3 left-3">-</Block>
</Container> </Container>
); );
} }
return ( return (
<Container chart={chart}> <Container service={service} chart={chart}>
{chart && ( {chart && (
<ChartDual <ChartDual
dataPoints={dataPoints} dataPoints={dataPoints}

View File

@ -1,6 +1,5 @@
import { useTranslation } from "next-i18next"; import { useTranslation } from "next-i18next";
import Error from "../components/error";
import Container from "../components/container"; import Container from "../components/container";
import Block from "../components/block"; import Block from "../components/block";
@ -32,15 +31,14 @@ export default function Component({ service }) {
if (error) { if (error) {
return ( return (
<Container chart={chart}> <Container service={service} chart={chart} error={true}>
<Error error={error} />
</Container> </Container>
); );
} }
if (!data) { if (!data) {
return ( return (
<Container chart={chart}> <Container service={service} chart={chart}>
<Block position="bottom-3 left-3">-</Block> <Block position="bottom-3 left-3">-</Block>
</Container> </Container>
); );
@ -49,7 +47,7 @@ export default function Component({ service }) {
data.splice(chart ? 5 : 1); data.splice(chart ? 5 : 1);
return ( return (
<Container chart={chart}> <Container service={service} chart={chart}>
<Block position="top-4 right-3 left-3"> <Block position="top-4 right-3 left-3">
<div className="flex items-center text-xs"> <div className="flex items-center text-xs">
<div className="grow" /> <div className="grow" />

View File

@ -2,7 +2,6 @@ import dynamic from "next/dynamic";
import { useState, useEffect } from "react"; import { useState, useEffect } from "react";
import { useTranslation } from "next-i18next"; import { useTranslation } from "next-i18next";
import Error from "../components/error";
import Container from "../components/container"; import Container from "../components/container";
import Block from "../components/block"; import Block from "../components/block";
@ -40,15 +39,14 @@ export default function Component({ service }) {
if (error) { if (error) {
return ( return (
<Container chart={chart}> <Container service={service} chart={chart} error={error}>
<Error error={error} />
</Container> </Container>
); );
} }
if (!data) { if (!data) {
return ( return (
<Container chart={chart}> <Container service={service} chart={chart}>
<Block position="bottom-3 left-3">-</Block> <Block position="bottom-3 left-3">-</Block>
</Container> </Container>
); );
@ -58,14 +56,14 @@ export default function Component({ service }) {
if (!sensorData) { if (!sensorData) {
return ( return (
<Container chart={chart}> <Container service={service} chart={chart}>
<Block position="bottom-3 left-3">-</Block> <Block position="bottom-3 left-3">-</Block>
</Container> </Container>
); );
} }
return ( return (
<Container chart={chart}> <Container service={service} chart={chart}>
{chart && ( {chart && (
<Chart <Chart
dataPoints={dataPoints} dataPoints={dataPoints}