1
0
mirror of https://github.com/karl0ss/homepage.git synced 2025-05-19 21:06:19 +01:00

60 lines
2.0 KiB
React
Raw Normal View History

2022-10-22 22:48:25 -07:00
import { useTranslation } from "react-i18next";
2022-10-23 01:48:46 -07:00
import { IoAlertCircle } from "react-icons/io5";
2022-10-22 22:48:25 -07:00
function displayError(error) {
return JSON.stringify(error[1] ? error[1] : error, null, 4);
}
function displayData(data) {
return data.type === "Buffer" ? Buffer.from(data).toString() : JSON.stringify(data, 4);
2022-10-22 22:48:25 -07:00
}
export default function Error({ error }) {
2022-10-22 22:48:25 -07:00
const { t } = useTranslation();
2022-10-23 23:45:43 -07:00
2024-08-13 00:07:16 -07:00
if (typeof error === "string") {
error = { message: error }; // eslint-disable-line no-param-reassign
}
if (error?.data?.error) {
error = error.data.error; // eslint-disable-line no-param-reassign
}
2022-10-22 22:48:25 -07:00
return (
2022-10-23 01:48:46 -07:00
<details className="px-1 pb-1">
2025-03-02 08:42:57 -08:00
<summary className="block text-center mt-1 mb-0 mx-auto p-3 rounded-sm bg-rose-900/80 hover:bg-rose-900/95 text-theme-900 cursor-pointer">
2022-10-23 01:48:46 -07:00
<div className="flex items-center justify-center text-xs font-bold">
<IoAlertCircle className="mr-1 w-5 h-5" />
{t("widget.api_error")} {error.message && t("widget.information")}
2022-10-22 22:48:25 -07:00
</div>
2022-10-23 01:48:46 -07:00
</summary>
2025-03-02 08:42:57 -08:00
<div className="bg-white dark:bg-theme-200/50 mt-2 rounded-sm text-rose-900 text-xs font-mono whitespace-pre-wrap break-all">
2022-10-23 01:48:46 -07:00
<ul className="p-4">
{error.message && (
<li>
<span className="text-black">{t("widget.api_error")}:</span> {error.message}
</li>
)}
{error.url && (
<li className="mt-2">
<span className="text-black">{t("widget.url")}:</span> {error.url}
</li>
)}
{error.rawError && (
<li className="mt-2">
<span className="text-black">{t("widget.raw_error")}:</span>
<div className="ml-2">{displayError(error.rawError)}</div>
</li>
)}
{error.data && (
<li className="mt-2">
<span className="text-black">{t("widget.response_data")}:</span>
<div className="ml-2">{displayData(error.data)}</div>
</li>
)}
2022-10-23 01:48:46 -07:00
</ul>
</div>
</details>
2022-10-22 22:48:25 -07:00
);
}