Improve error handling for Glances widgets when host is unreachable
This commit is contained in:
parent
ec448d6c41
commit
2fae39e3ce
@ -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>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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>;
|
|
||||||
}
|
|
||||||
@ -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}
|
||||||
|
|||||||
@ -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}
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -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,32 +26,42 @@ export default function Component({ service }) {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (data) {
|
if (data) {
|
||||||
// eslint-disable-next-line eqeqeq
|
|
||||||
const gpuData = data.find((item) => item[item.key] == gpuName);
|
|
||||||
|
|
||||||
if (gpuData) {
|
if (data.hasOwnProperty("error")) {
|
||||||
setDataPoints((prevDataPoints) => {
|
return (
|
||||||
const newDataPoints = [...prevDataPoints, { a: gpuData.mem, b: gpuData.proc }];
|
<Container service={service} chart={chart} error={true}>
|
||||||
if (newDataPoints.length > pointsLimit) {
|
</Container>
|
||||||
newDataPoints.shift();
|
)
|
||||||
}
|
|
||||||
return newDataPoints;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
// eslint-disable-next-line eqeqeq
|
||||||
|
const gpuData = data.find((item) => item[item.key] == gpuName);
|
||||||
|
|
||||||
|
if (gpuData) {
|
||||||
|
setDataPoints((prevDataPoints) => {
|
||||||
|
const newDataPoints = [...prevDataPoints, { a: gpuData.mem, b: gpuData.proc }];
|
||||||
|
if (newDataPoints.length > pointsLimit) {
|
||||||
|
newDataPoints.shift();
|
||||||
|
}
|
||||||
|
return newDataPoints;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}, [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}
|
||||||
|
|||||||
@ -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,36 +85,45 @@ 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) {
|
||||||
quicklookData.percpu.forEach((cpu, index) => {
|
if (quicklookData.hasOwnProperty("error")) {
|
||||||
dataCharts.push({
|
const quicklookError = true;
|
||||||
name: `CPU ${index}`,
|
return (
|
||||||
cpu: cpu.total,
|
<Container service={service} chart={chart} error={quicklookError} >
|
||||||
mem: quicklookData.mem,
|
</Container>
|
||||||
swap: quicklookData.swap,
|
);
|
||||||
proc: quicklookData.cpu,
|
}
|
||||||
|
else {
|
||||||
|
quicklookData.percpu.forEach((cpu, index) => {
|
||||||
|
dataCharts.push({
|
||||||
|
name: `CPU ${index}`,
|
||||||
|
cpu: cpu.total,
|
||||||
|
mem: quicklookData.mem,
|
||||||
|
swap: quicklookData.swap,
|
||||||
|
proc: quicklookData.cpu,
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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>
|
||||||
|
|||||||
@ -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}
|
||||||
|
|||||||
@ -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}
|
||||||
|
|||||||
@ -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" />
|
||||||
|
|||||||
@ -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}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user