Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Improve error handling for Glances widgets #3657

Merged
merged 6 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/widgets/glances/components/container.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
export default function Container({ children, chart = true, className = "" }) {
import { useContext } from "react";

import Error from "./error";

import { SettingsContext } from "utils/contexts/settings";

export default function Container({ children, widget, error = null, chart = true, className = "" }) {
const { settings } = useContext(SettingsContext);
const hideErrors = settings.hideErrors || widget?.hideErrors;

if (error) {
if (hideErrors) {
return null;
}

return <Error />;
}

return (
<div>
{children}
Expand Down
7 changes: 1 addition & 6 deletions src/widgets/glances/metrics/cpu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import dynamic from "next/dynamic";
import { useState, useEffect } from "react";
import { useTranslation } from "next-i18next";

import Error from "../components/error";
import Container from "../components/container";
import Block from "../components/block";

Expand Down Expand Up @@ -39,11 +38,7 @@ export default function Component({ service }) {
}, [data, pointsLimit]);

if (error) {
return (
<Container chart={chart}>
<Error error={error} />
</Container>
);
return <Container error={error} widget={widget} />;
}

if (!data) {
Expand Down
12 changes: 4 additions & 8 deletions src/widgets/glances/metrics/disk.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import dynamic from "next/dynamic";
import { useState, useEffect } from "react";
import { useTranslation } from "next-i18next";

import Error from "../components/error";
import Container from "../components/container";
import Block from "../components/block";

Expand Down Expand Up @@ -35,7 +34,7 @@ export default function Component({ service }) {
}));

useEffect(() => {
if (data) {
if (data && !data.error) {
const diskData = data.find((item) => item.disk_name === diskName);

setDataPoints((prevDataPoints) => {
Expand All @@ -52,12 +51,9 @@ export default function Component({ service }) {
setRatePoints(calculateRates(dataPoints));
}, [dataPoints]);

if (error) {
return (
<Container chart={chart}>
<Error error={error} />
</Container>
);
if (error || (data && data.error)) {
const finalError = error || data.error;
return <Container error={finalError} widget={widget} />;
}

if (!data) {
Expand Down
7 changes: 1 addition & 6 deletions src/widgets/glances/metrics/fs.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useTranslation } from "next-i18next";

import Error from "../components/error";
import Container from "../components/container";
import Block from "../components/block";

Expand All @@ -20,11 +19,7 @@ export default function Component({ service }) {
});

if (error) {
return (
<Container chart={chart}>
<Error error={error} />
</Container>
);
return <Container error={error} widget={widget} />;
}

if (!data) {
Expand Down
12 changes: 4 additions & 8 deletions src/widgets/glances/metrics/gpu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import dynamic from "next/dynamic";
import { useState, useEffect } from "react";
import { useTranslation } from "next-i18next";

import Error from "../components/error";
import Container from "../components/container";
import Block from "../components/block";

Expand All @@ -26,7 +25,7 @@ export default function Component({ service }) {
});

useEffect(() => {
if (data) {
if (data && !data.error) {
// eslint-disable-next-line eqeqeq
const gpuData = data.find((item) => item[item.key] == gpuName);

Expand All @@ -42,12 +41,9 @@ export default function Component({ service }) {
}
}, [data, gpuName, pointsLimit]);

if (error) {
return (
<Container chart={chart}>
<Error error={error} />
</Container>
);
if (error || (data && data.error)) {
const finalError = error || data.error;
return <Container error={finalError} widget={widget} />;
}

if (!data) {
Expand Down
16 changes: 4 additions & 12 deletions src/widgets/glances/metrics/info.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useTranslation } from "next-i18next";

import Error from "../components/error";
import Container from "../components/container";
import Block from "../components/block";

Expand Down Expand Up @@ -84,20 +83,13 @@ export default function Component({ service }) {
refreshInterval: defaultSystemInterval,
});

if (quicklookError) {
return (
<Container chart={chart}>
<Error error={quicklookError} />
</Container>
);
if (quicklookError || (quicklookData && quicklookData.error)) {
const qlError = quicklookError || quicklookData.error;
return <Container error={qlError} widget={widget} />;
}

if (systemError) {
return (
<Container chart={chart}>
<Error error={systemError} />
</Container>
);
return <Container error={systemError} service={service} />;
}

const dataCharts = [];
Expand Down
7 changes: 1 addition & 6 deletions src/widgets/glances/metrics/memory.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import dynamic from "next/dynamic";
import { useState, useEffect } from "react";
import { useTranslation } from "next-i18next";

import Error from "../components/error";
import Container from "../components/container";
import Block from "../components/block";

Expand Down Expand Up @@ -38,11 +37,7 @@ export default function Component({ service }) {
}, [data, pointsLimit]);

if (error) {
return (
<Container chart={chart}>
<Error error={error} />
</Container>
);
return <Container error={error} widget={widget} />;
}

if (!data) {
Expand Down
12 changes: 4 additions & 8 deletions src/widgets/glances/metrics/net.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import dynamic from "next/dynamic";
import { useState, useEffect } from "react";
import { useTranslation } from "next-i18next";

import Error from "../components/error";
import Container from "../components/container";
import Block from "../components/block";

Expand Down Expand Up @@ -31,7 +30,7 @@ export default function Component({ service }) {
});

useEffect(() => {
if (data) {
if (data && !data.error) {
const interfaceData = data.find((item) => item[item.key] === interfaceName);

if (interfaceData) {
Expand All @@ -52,12 +51,9 @@ export default function Component({ service }) {
}
}, [data, interfaceName, pointsLimit, rxKey, txKey]);

if (error) {
return (
<Container chart={chart}>
<Error error={error} />
</Container>
);
if (error || (data && data.error)) {
const finalError = error || data.error;
return <Container error={finalError} widget={widget} />;
}

if (!data) {
Expand Down
7 changes: 1 addition & 6 deletions src/widgets/glances/metrics/process.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useTranslation } from "next-i18next";

import Error from "../components/error";
import Container from "../components/container";
import Block from "../components/block";

Expand Down Expand Up @@ -31,11 +30,7 @@ export default function Component({ service }) {
});

if (error) {
return (
<Container chart={chart}>
<Error error={error} />
</Container>
);
return <Container service={service} widget={widget} />;
}

if (!data) {
Expand Down
30 changes: 15 additions & 15 deletions src/widgets/glances/metrics/sensor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import dynamic from "next/dynamic";
import { useState, useEffect } from "react";
import { useTranslation } from "next-i18next";

import Error from "../components/error";
import Container from "../components/container";
import Block from "../components/block";

Expand All @@ -26,24 +25,25 @@ export default function Component({ service }) {
});

useEffect(() => {
if (data) {
if (data && !data.error) {
const sensorData = data.find((item) => item.label === sensorName);
setDataPoints((prevDataPoints) => {
const newDataPoints = [...prevDataPoints, { value: sensorData.value }];
if (newDataPoints.length > pointsLimit) {
newDataPoints.shift();
}
return newDataPoints;
});
if (sensorData) {
setDataPoints((prevDataPoints) => {
const newDataPoints = [...prevDataPoints, { value: sensorData.value }];
if (newDataPoints.length > pointsLimit) {
newDataPoints.shift();
}
return newDataPoints;
});
} else {
data.error = true;
}
}
}, [data, sensorName, pointsLimit]);

if (error) {
return (
<Container chart={chart}>
<Error error={error} />
</Container>
);
if (error || (data && data.error)) {
const finalError = error || data.error;
return <Container error={finalError} widget={widget} />;
}

if (!data) {
Expand Down