2022-03-10 05:28:55 +01:00
|
|
|
import * as React from "react";
|
2022-03-10 21:37:50 +01:00
|
|
|
import StackTrace from "stacktrace-js";
|
2023-05-24 12:25:20 +02:00
|
|
|
import { CircularProgress, Link, Button } from "@mui/material";
|
2023-05-23 21:13:01 +02:00
|
|
|
import { Trans, withTranslation } from "react-i18next";
|
2022-03-10 05:28:55 +01:00
|
|
|
|
2022-04-08 16:44:35 +02:00
|
|
|
class ErrorBoundaryImpl extends React.Component {
|
2023-05-23 21:13:01 +02:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
error: false,
|
|
|
|
originalStack: null,
|
|
|
|
niceStack: null,
|
|
|
|
unsupportedIndexedDB: false,
|
|
|
|
};
|
|
|
|
}
|
2022-03-10 05:28:55 +01:00
|
|
|
|
2023-05-23 21:13:01 +02:00
|
|
|
componentDidCatch(error, info) {
|
|
|
|
console.error("[ErrorBoundary] Error caught", error, info);
|
2022-03-11 20:43:54 +01:00
|
|
|
|
2023-05-23 21:13:01 +02:00
|
|
|
// Special case for unsupported IndexedDB in Private Browsing mode (Firefox, Safari), see
|
|
|
|
// - https://github.com/dexie/Dexie.js/issues/312
|
|
|
|
// - https://bugzilla.mozilla.org/show_bug.cgi?id=781982
|
|
|
|
const isUnsupportedIndexedDB =
|
2023-05-24 01:29:47 +02:00
|
|
|
error?.name === "InvalidStateError" || (error?.name === "DatabaseClosedError" && error?.message?.indexOf("InvalidStateError") !== -1);
|
2022-04-30 02:51:26 +02:00
|
|
|
|
2023-05-23 21:13:01 +02:00
|
|
|
if (isUnsupportedIndexedDB) {
|
|
|
|
this.handleUnsupportedIndexedDB();
|
|
|
|
} else {
|
|
|
|
this.handleError(error, info);
|
2022-04-30 02:51:26 +02:00
|
|
|
}
|
2023-05-23 21:13:01 +02:00
|
|
|
}
|
2022-04-30 02:51:26 +02:00
|
|
|
|
2023-05-23 21:13:01 +02:00
|
|
|
handleError(error, info) {
|
|
|
|
// Immediately render original stack trace
|
|
|
|
const prettierOriginalStack = info.componentStack
|
|
|
|
.trim()
|
|
|
|
.split("\n")
|
|
|
|
.map((line) => ` at ${line}`)
|
|
|
|
.join("\n");
|
|
|
|
this.setState({
|
|
|
|
error: true,
|
|
|
|
originalStack: `${error.toString()}\n${prettierOriginalStack}`,
|
|
|
|
});
|
2022-03-11 20:43:54 +01:00
|
|
|
|
2023-05-23 21:13:01 +02:00
|
|
|
// Fetch additional info and a better stack trace
|
|
|
|
StackTrace.fromError(error).then((stack) => {
|
|
|
|
console.error("[ErrorBoundary] Stacktrace fetched", stack);
|
2023-05-25 03:44:12 +02:00
|
|
|
const stackString = stack.map((el) => ` at ${el.functionName} (${el.fileName}:${el.columnNumber}:${el.lineNumber})`).join("\n");
|
2023-05-25 03:32:15 +02:00
|
|
|
const niceStack = `${error.toString()}\n${stackString}`;
|
2023-05-23 21:13:01 +02:00
|
|
|
this.setState({ niceStack });
|
|
|
|
});
|
|
|
|
}
|
2022-03-10 05:28:55 +01:00
|
|
|
|
2023-05-23 21:13:01 +02:00
|
|
|
handleUnsupportedIndexedDB() {
|
|
|
|
this.setState({
|
|
|
|
error: true,
|
|
|
|
unsupportedIndexedDB: true,
|
|
|
|
});
|
|
|
|
}
|
2022-04-30 02:51:26 +02:00
|
|
|
|
2023-05-23 21:13:01 +02:00
|
|
|
copyStack() {
|
|
|
|
let stack = "";
|
|
|
|
if (this.state.niceStack) {
|
|
|
|
stack += `${this.state.niceStack}\n\n`;
|
2022-03-10 05:28:55 +01:00
|
|
|
}
|
2023-05-23 21:13:01 +02:00
|
|
|
stack += `${this.state.originalStack}\n`;
|
|
|
|
navigator.clipboard.writeText(stack);
|
|
|
|
}
|
2022-03-10 05:28:55 +01:00
|
|
|
|
2023-05-23 21:13:01 +02:00
|
|
|
renderUnsupportedIndexedDB() {
|
|
|
|
const { t } = this.props;
|
|
|
|
return (
|
|
|
|
<div style={{ margin: "20px" }}>
|
|
|
|
<h2>{t("error_boundary_unsupported_indexeddb_title")} 😮</h2>
|
|
|
|
<p style={{ maxWidth: "600px" }}>
|
|
|
|
<Trans
|
|
|
|
i18nKey="error_boundary_unsupported_indexeddb_description"
|
|
|
|
components={{
|
2023-05-24 01:29:47 +02:00
|
|
|
githubLink: <Link href="https://github.com/binwiederhier/ntfy/issues/208" />,
|
2023-05-23 21:13:01 +02:00
|
|
|
discordLink: <Link href="https://discord.gg/cT7ECsZj9w" />,
|
|
|
|
matrixLink: <Link href="https://matrix.to/#/#ntfy:matrix.org" />,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2022-04-30 02:51:26 +02:00
|
|
|
|
2023-05-23 21:13:01 +02:00
|
|
|
renderError() {
|
|
|
|
const { t } = this.props;
|
|
|
|
return (
|
|
|
|
<div style={{ margin: "20px" }}>
|
|
|
|
<h2>{t("error_boundary_title")} 😮</h2>
|
|
|
|
<p>
|
|
|
|
<Trans
|
|
|
|
i18nKey="error_boundary_description"
|
|
|
|
components={{
|
2023-05-24 01:29:47 +02:00
|
|
|
githubLink: <Link href="https://github.com/binwiederhier/ntfy/issues" />,
|
2023-05-23 21:13:01 +02:00
|
|
|
discordLink: <Link href="https://discord.gg/cT7ECsZj9w" />,
|
|
|
|
matrixLink: <Link href="https://matrix.to/#/#ntfy:matrix.org" />,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</p>
|
2023-06-17 22:15:02 +02:00
|
|
|
<div style={{ display: "flex", gap: 5 }}>
|
2023-05-23 21:13:01 +02:00
|
|
|
<Button variant="outlined" onClick={() => this.copyStack()}>
|
|
|
|
{t("error_boundary_button_copy_stack_trace")}
|
|
|
|
</Button>
|
2023-06-17 22:15:02 +02:00
|
|
|
|
|
|
|
<Button variant="outlined" onClick={() => window.location.reload()}>
|
|
|
|
{t("error_boundary_button_reload_ntfy")}
|
|
|
|
</Button>
|
|
|
|
</div>
|
2023-05-23 21:13:01 +02:00
|
|
|
<h3>{t("error_boundary_stack_trace")}</h3>
|
|
|
|
{this.state.niceStack ? (
|
|
|
|
<pre>{this.state.niceStack}</pre>
|
|
|
|
) : (
|
|
|
|
<>
|
2023-05-24 01:29:47 +02:00
|
|
|
<CircularProgress size="20px" sx={{ verticalAlign: "text-bottom" }} /> {t("error_boundary_gathering_info")}
|
2023-05-23 21:13:01 +02:00
|
|
|
</>
|
|
|
|
)}
|
|
|
|
<pre>{this.state.originalStack}</pre>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2023-05-24 10:20:15 +02:00
|
|
|
|
|
|
|
render() {
|
|
|
|
if (this.state.error) {
|
|
|
|
if (this.state.unsupportedIndexedDB) {
|
|
|
|
return this.renderUnsupportedIndexedDB();
|
|
|
|
}
|
|
|
|
return this.renderError();
|
|
|
|
}
|
|
|
|
return this.props.children;
|
|
|
|
}
|
2022-03-10 05:28:55 +01:00
|
|
|
}
|
|
|
|
|
2022-04-08 16:44:35 +02:00
|
|
|
const ErrorBoundary = withTranslation()(ErrorBoundaryImpl); // Adds props.t
|
2022-03-10 05:28:55 +01:00
|
|
|
export default ErrorBoundary;
|